Passed
Push — master ( 3ba489...3505e6 )
by Adrien
11:36
created

UserByToken   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 34
ccs 6
cts 16
cp 0.375
rs 10
c 2
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 32 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Query;
6
7
use Application\Model\User;
8
use Application\Repository\UserRepository;
9
use Ecodev\Felix\Api\Exception;
10
use Ecodev\Felix\Api\Field\FieldInterface;
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')),
24
                ],
25 1
                'resolve' => function ($root, array $args): User {
26
                    /** @var UserRepository $repository */
27
                    $repository = _em()->getRepository(User::class);
28
29
                    /** @var null|User $user */
30
                    $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) {
31
                        return $repository->findOneByToken($args['token']);
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