Failed Conditions
Push — master ( 2bc238...84f6c0 )
by Adrien
08:48
created

UserByToken::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.0975

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 1
nop 0
dl 0
loc 28
ccs 5
cts 13
cp 0.3846
crap 5.0975
rs 9.7666
c 0
b 0
f 0
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
                    $repository->getAclFilter()->setEnabled(false);
31
                    $user = $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
                    /** @scrutinizer ignore-call */ 
32
                    $user = $repository->findOneByToken($args['token']);
Loading history...
32
                    $repository->getAclFilter()->setEnabled(true);
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