| Conditions | 4 |
| Paths | 1 |
| Total Lines | 28 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 20 |
| CRAP Score | 4 |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | 3 | public static function build(): iterable |
|
| 16 | { |
||
| 17 | 1 | yield 'userRolesAvailable' => fn () => [ |
|
| 18 | 1 | 'type' => Type::nonNull(Type::listOf(Type::nonNull(_types()->get(UserRoleType::class)))), |
|
| 19 | 1 | 'description' => 'List of roles available for the given user', |
|
| 20 | 1 | 'args' => [ |
|
| 21 | 1 | [ |
|
| 22 | 1 | 'name' => 'user', |
|
| 23 | 1 | 'type' => _types()->getId(User::class), |
|
| 24 | 1 | ], |
|
| 25 | 1 | ], |
|
| 26 | 1 | 'resolve' => function ($root, array $args): array { |
|
| 27 | 3 | $currentUser = User::getCurrent(); |
|
| 28 | 3 | $oldRole = isset($args['user']) ? $args['user']->getEntity()->getRole() : User::ROLE_MEMBER; |
|
| 29 | |||
| 30 | /** @var UserRoleType $type */ |
||
| 31 | 3 | $type = _types()->get(UserRoleType::class); |
|
| 32 | |||
| 33 | // Check each roles that exist in our API (which is different from the roles that exist in our DB) |
||
| 34 | 3 | $available = []; |
|
| 35 | 3 | foreach ($type->getValues() as $valueDefinition) { |
|
| 36 | 3 | $newRole = $valueDefinition->value; |
|
| 37 | 3 | if (Role::canUpdate($currentUser, $oldRole, $newRole)) { |
|
| 38 | 3 | $available[] = $newRole; |
|
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | 3 | return $available; |
|
| 43 | 1 | }, |
|
| 47 |