Failed Conditions
Push — master ( 6a8bd6...5def7d )
by Adrien
08:40
created

UpdatePassword::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.0023

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 1
nop 0
dl 0
loc 36
ccs 18
cts 19
cp 0.9474
crap 4.0023
rs 9.584
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\Api\Exception;
8
use Application\Api\Field\FieldInterface;
9
use Application\Api\Scalar\PasswordType;
10
use Application\Api\Scalar\TokenType;
11
use Application\Model\Log;
12
use Application\Model\User;
13
use Application\Repository\LogRepository;
14
use Application\Repository\UserRepository;
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 1
            '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)),
1 ignored issue
show
Bug introduced by
_types()->get(Applicatio...calar\TokenType::class) of type GraphQL\Type\Definition\Type is incompatible with the type GraphQL\Type\Definition\NullableType expected by parameter $wrappedType of GraphQL\Type\Definition\Type::nonNull(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
                'token' => Type::nonNull(/** @scrutinizer ignore-type */ _types()->get(TokenType::class)),
Loading history...
28 1
                'password' => Type::nonNull(_types()->get(PasswordType::class)),
29
            ],
30
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
1 ignored issue
show
Unused Code introduced by
The parameter $session is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
            'resolve' => function ($root, array $args, /** @scrutinizer ignore-unused */ SessionInterface $session): bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
                /** @var LogRepository $logRepository */
32 2
                $logRepository = _em()->getRepository(Log::class);
33 2
                if ($logRepository->updatePasswordFailedOften()) {
34
                    throw new Exception('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 User $user */
41
                $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) {
42 2
                    return $repository->findOneByToken($args['token']);
1 ignored issue
show
Bug introduced by
The method findOneByToken() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
                    return $repository->/** @scrutinizer ignore-call */ findOneByToken($args['token']);
Loading history...
43 2
                });
44
45 2
                if (!$user || !$user->isTokenValid()) {
46 1
                    _log()->info(LogRepository::UPDATE_PASSWORD_FAILED);
47
48 1
                    return false;
49
                }
50
51 1
                $user->setPassword($args['password']);
52 1
                _em()->flush();
53 1
                _log()->info(LogRepository::UPDATE_PASSWORD);
54
55 1
                return true;
56 1
            },
57
        ];
58
    }
59
}
60