Failed Conditions
Push — master ( 5a6ec0...06e1a1 )
by Sylvain
11:24 queued 11s
created

UserByToken::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.3915

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 1
nop 0
dl 0
loc 32
ccs 5
cts 14
cp 0.357
crap 5.3915
rs 9.7333
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
                    $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) {
35
                        throw new Exception('User not found for token `' . $args['token'] . '`.');
36
                    }
37
38
                    if (!$user->isTokenValid()) {
39
                        throw new Exception('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
40
                    }
41
42
                    // Set current user for his ACL, but not in persisted session, only for the remaining execution time.
43
                    // He will have to go through a proper login to persist the session.
44
                    User::setCurrent($user);
45
46
                    return $user;
47 1
                },
48
            ];
49
    }
50
}
51