Passed
Push — master ( 1d5fb4...e5c3c4 )
by Sam
06:33 queued 01:55
created

UpdatePassword::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0027

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 1
nop 0
dl 0
loc 34
ccs 17
cts 18
cp 0.9444
crap 4.0027
rs 9.6
c 0
b 0
f 0
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(): array
21
    {
22
        return [
23 1
            'name' => 'updatePassword',
24 1
            'type' => Type::nonNull(Type::boolean()),
25
            'description' => 'Update the password for the user identified by the token. Return false if token is invalid',
26
            'args' => [
27 1
                'token' => Type::nonNull(_types()->get(TokenType::class)),
28 1
                'password' => Type::nonNull(_types()->get(PasswordType::class)),
29
            ],
30 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
31
                /** @var LogRepository $logRepository */
32 2
                $logRepository = _em()->getRepository(Log::class);
33 2
                if ($logRepository->updatePasswordFailedOften()) {
34
                    throw new ExceptionWithoutMailLogging('Trop de tentatives de changement de mot de passe ont échouées. Veuillez ressayer plus tard.');
35
                }
36
37
                /** @var UserRepository $repository */
38 2
                $repository = _em()->getRepository(User::class);
39
40
                /** @var null|User $user */
41 2
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token']));
42
43 2
                if (!$user || !$user->isTokenValid()) {
44 1
                    _log()->info(LogRepository::UPDATE_PASSWORD_FAILED);
45
46 1
                    return false;
47
                }
48
49 1
                $user->setPassword($args['password']);
50 1
                _em()->flush();
51 1
                _log()->info(LogRepository::UPDATE_PASSWORD);
52
53 1
                return true;
54
            },
55
        ];
56
    }
57
}
58