UserRolesAvailable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 30
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Query;
6
7
use Application\Api\Enum\UserRoleType;
8
use Application\Model\User;
9
use Application\Service\Role;
10
use Ecodev\Felix\Api\Field\FieldInterface;
11
use GraphQL\Type\Definition\Type;
12
13
abstract class UserRolesAvailable implements FieldInterface
14
{
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
            },
44 1
        ];
45
    }
46
}
47