Passed
Push — master ( 3779cb...2be227 )
by Sam
06:31
created

UserRolesAvailable::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 9.7
cc 4
nc 1
nop 0
crap 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(): array
16
    {
17
        return
18
            [
19 1
                'name' => 'userRolesAvailable',
20 1
                'type' => Type::nonNull(Type::listOf(Type::nonNull(_types()->get(UserRoleType::class)))),
21 1
                'description' => 'List of roles available for the given user',
22
                'args' => [
23
                    [
24 1
                        'name' => 'user',
25 1
                        'type' => _types()->getId(User::class),
26
                    ],
27
                ],
28 1
                'resolve' => function ($root, array $args): array {
29 3
                    $currentUser = User::getCurrent();
30 3
                    $oldRole = isset($args['user']) ? $args['user']->getEntity()->getRole() : User::ROLE_MEMBER;
31
32
                    /** @var UserRoleType $type */
33 3
                    $type = _types()->get(UserRoleType::class);
34
35
                    // Check each roles that exist in our API (which is different from the roles that exist in our DB)
36 3
                    $available = [];
37 3
                    foreach ($type->getValues() as $valueDefinition) {
38 3
                        $newRole = $valueDefinition->value;
39 3
                        if (Role::canUpdate($currentUser, $oldRole, $newRole)) {
40 3
                            $available[] = $newRole;
41
                        }
42
                    }
43
44 3
                    return $available;
45
                },
46
            ];
47
    }
48
}
49