Conditions | 3 |
Paths | 1 |
Total Lines | 32 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 6 |
CRAP Score | 5.1971 |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | }, |
|
51 |