Failed Conditions
Push — master ( ce2a97...d73709 )
by Adrien
13:38 queued 11:33
created

UpdatePassword   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 4
eloc 20
dl 0
loc 35
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 33 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Model\Log;
8
use Application\Model\User;
9
use Application\Repository\LogRepository;
10
use Application\Repository\UserRepository;
11
use Ecodev\Felix\Api\ExceptionWithoutMailLogging;
12
use Ecodev\Felix\Api\Field\FieldInterface;
13
use Ecodev\Felix\Api\Scalar\PasswordType;
14
use Ecodev\Felix\Api\Scalar\TokenType;
15
use GraphQL\Type\Definition\Type;
16
use Mezzio\Session\SessionInterface;
17
18
abstract class UpdatePassword implements FieldInterface
19
{
20 2
    public static function build(): iterable
21
    {
22 1
        yield 'updatePassword' => fn () => [
23 1
            'type' => Type::nonNull(Type::boolean()),
24 1
            'description' => 'Update the password for the user identified by the token. Return false if token is invalid',
25 1
            'args' => [
26 1
                'token' => Type::nonNull(_types()->get(TokenType::class)),
27 1
                'password' => Type::nonNull(_types()->get(PasswordType::class)),
28 1
            ],
29 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
30
                /** @var LogRepository $logRepository */
31 2
                $logRepository = _em()->getRepository(Log::class);
32 2
                if ($logRepository->updatePasswordFailedOften()) {
33
                    throw new ExceptionWithoutMailLogging('Trop de tentatives de changement de mot de passe ont échouées. Veuillez ressayer plus tard.');
34
                }
35
36
                /** @var UserRepository $repository */
37 2
                $repository = _em()->getRepository(User::class);
38
39
                /** @var null|User $user */
40 2
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token']));
41
42 2
                if (!$user || !$user->isTokenValid()) {
43 1
                    _log()->info(LogRepository::UPDATE_PASSWORD_FAILED);
44
45 1
                    return false;
46
                }
47
48 1
                $user->setPassword($args['password']);
49 1
                _em()->flush();
50 1
                _log()->info(LogRepository::UPDATE_PASSWORD);
51
52 1
                return true;
53 1
            },
54 1
        ];
55
    }
56
}
57