Failed Conditions
Push — master ( 32d930...704890 )
by Adrien
08:40
created

UserByToken   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
ccs 5
cts 12
cp 0.4167
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 28 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Query;
6
7
use Application\Api\Exception;
8
use Application\Api\Field\FieldInterface;
9
use Application\Model\User;
10
use Application\Repository\UserRepository;
11
use GraphQL\Type\Definition\Type;
12
13
abstract class UserByToken implements FieldInterface
14
{
15 1
    public static function build(): array
16
    {
17
        return
18
            [
19 1
                'name' => 'userByToken',
20 1
                'type' => Type::nonNull(_types()->getOutput(User::class)),
21 1
                'description' => 'Get a user by its temporary token',
22
                'args' => [
23 1
                    'token' => Type::nonNull(_types()->get('Token')),
1 ignored issue
show
Bug introduced by
_types()->get('Token') of type GraphQL\Type\Definition\Type is incompatible with the type GraphQL\Type\Definition\NullableType expected by parameter $wrappedType of GraphQL\Type\Definition\Type::nonNull(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
                    'token' => Type::nonNull(/** @scrutinizer ignore-type */ _types()->get('Token')),
Loading history...
24
                ],
25
                'resolve' => function ($root, array $args): User {
26
                    /** @var UserRepository $repository */
27
                    $repository = _em()->getRepository(User::class);
28
29
                    /** @var User $user */
30
                    $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) {
31
                        return $repository->findOneByToken($args['token']);
1 ignored issue
show
Bug introduced by
The method findOneByToken() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
                        return $repository->/** @scrutinizer ignore-call */ findOneByToken($args['token']);
Loading history...
32
                    });
33
34
                    if (!$user || !$user->isTokenValid()) {
35
                        throw new Exception('User not found for token `' . $args['token'] . '`.');
36
                    }
37
38
                    // Set current user for his ACL, but not in persisted session, only for the remaining execution time.
39
                    // He will have to go through a proper login to persist the session.
40
                    User::setCurrent($user);
41
42
                    return $user;
43 1
                },
44
            ];
45
    }
46
}
47