Passed
Push — master ( abcdc1...a80a3a )
by Sam
06:58
created

UserByToken   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 34
ccs 6
cts 16
cp 0.375
rs 10
c 0
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\ExceptionWithoutMailLogging;
11
use Ecodev\Felix\Api\Field\FieldInterface;
12
use GraphQL\Type\Definition\Type;
13
14
abstract class UserByToken implements FieldInterface
15
{
16 1
    public static function build(): array
17
    {
18
        return
19
            [
20 1
                'name' => 'userByToken',
21 1
                'type' => Type::nonNull(_types()->getOutput(User::class)),
22 1
                'description' => 'Get a user by its temporary token',
23
                'args' => [
24 1
                    'token' => Type::nonNull(_types()->get('Token')),
25
                ],
26 1
                'resolve' => function ($root, array $args): User {
27
                    /** @var UserRepository $repository */
28
                    $repository = _em()->getRepository(User::class);
29
30
                    /** @var null|User $user */
31
                    $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) {
32
                        return $repository->findOneByToken($args['token']);
33
                    });
34
35
                    if (!$user) {
36
                        throw new ExceptionWithoutMailLogging("Le lien que vous avez suivi n'est pas valable ou a déjà été utilisé. Veuillez effectuer une nouvelle demande de changement de mot de passe ou création de compte. Une nouvelle demande invalide les précédentes.");
37
                    }
38
39
                    if (!$user->isTokenValid()) {
40
                        throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
41
                    }
42
43
                    // Set current user for his ACL, but not in persisted session, only for the remaining execution time.
44
                    // He will have to go through a proper login to persist the session.
45
                    User::setCurrent($user);
46
47
                    return $user;
48 1
                },
49
            ];
50
    }
51
}
52