Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
36 / 36 |
| ExternalUsers | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
36 / 36 |
| handleExternalUsers | |
100.00% |
1 / 1 |
3 | |
100.00% |
19 / 19 |
|||
| manageExternalUsersEntities | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| createExternalUser | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| createExternalSite | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Entities\Services\ExternalUsers; |
| 4 | |
| 5 | use Illuminate\Support\Collection; |
| 6 | use Illuminate\Support\Str; |
| 7 | use Qmp\Laravel\MicroService\Client\Client; |
| 8 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
| 9 | |
| 10 | class ExternalUsers |
| 11 | { |
| 12 | const SERVICE_NAME = 'service_external_users'; |
| 13 | |
| 14 | /** |
| 15 | * Managing entity's external users with service-external-users-laravel |
| 16 | * |
| 17 | * @param array $externalUsers |
| 18 | * @return Collection |
| 19 | */ |
| 20 | public static function handleExternalUsers(array $externalUsers): Collection |
| 21 | { |
| 22 | return collect($externalUsers)->map(function ($user) { |
| 23 | $sites = collect($user['sites_selected']); |
| 24 | |
| 25 | $newSites = $sites->filter(function ($id) { |
| 26 | return !is_numeric($id); |
| 27 | })->map(function ($text) { |
| 28 | $array = explode('||', $text); |
| 29 | return self::createExternalSite($array); |
| 30 | }); |
| 31 | |
| 32 | $user['sites_selected'] = array_values($sites->merge($newSites)->filter(function ($id) { |
| 33 | return is_numeric($id); |
| 34 | })->toArray()); |
| 35 | |
| 36 | if (isset($user['isNew']) && $user['isNew']) { |
| 37 | $user['id'] = self::createExternalUser([ |
| 38 | 'name' => $user['name'], |
| 39 | 'email' => $user['email'], |
| 40 | 'type' => $user['type'], |
| 41 | 'active' => $user['active'] |
| 42 | ]); |
| 43 | } |
| 44 | return collect($user)->only('id', 'sites_selected')->toArray(); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Manage relationships entity users in service-external-users-laravel |
| 50 | * |
| 51 | * @param array $data |
| 52 | * @return mixed|null |
| 53 | * @throws \Exception |
| 54 | */ |
| 55 | public static function manageExternalUsersEntities(array $data) |
| 56 | { |
| 57 | $request = ClientRequest::createObject(self::SERVICE_NAME, 'users/manage-entites', [ 'body' => $data, 'prefix_url' => ClientRequest::PERFORMANCE_STACK]); |
| 58 | $response = Client::systemSend('post', $request); |
| 59 | |
| 60 | if(!Str::startsWith($response->code, '2')) { |
| 61 | throw new \Exception('Unable to manage entities'); |
| 62 | } |
| 63 | |
| 64 | return $response->content; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param array $user |
| 69 | * @return mixed |
| 70 | * @throws \Exception |
| 71 | */ |
| 72 | public static function createExternalUser(array $user) |
| 73 | { |
| 74 | $request = ClientRequest::createObject(self::SERVICE_NAME, 'users', [ 'body' => $user, 'prefix_url' => ClientRequest::PERFORMANCE_STACK]); |
| 75 | $response = Client::systemSend('post', $request); |
| 76 | |
| 77 | if(!Str::startsWith($response->code, '2')) { |
| 78 | throw new \Exception('Unable to store external user: ' . $user['name']); |
| 79 | } |
| 80 | |
| 81 | return $response->content['user']['id']; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Create External site in service-external-users-laravel |
| 86 | * |
| 87 | * @param array $data |
| 88 | * @return mixed |
| 89 | * @throws \Exception |
| 90 | */ |
| 91 | public static function createExternalSite(array $data) |
| 92 | { |
| 93 | $request = ClientRequest::createObject(self::SERVICE_NAME, 'sites', [ |
| 94 | 'body' => [ |
| 95 | 'name' => $data[0], |
| 96 | 'url' => $data[1] |
| 97 | ], |
| 98 | 'prefix_url' => ClientRequest::PERFORMANCE_STACK |
| 99 | ]); |
| 100 | |
| 101 | $response = Client::systemSend('post', $request); |
| 102 | |
| 103 | if(!Str::startsWith($response->code, '2')) { |
| 104 | throw new \Exception('Unable to store site: ' . $data[0]); |
| 105 | } |
| 106 | |
| 107 | return $response->content['site']['id']; |
| 108 | } |
| 109 | } |