Failed Conditions
Push — master ( 1d36b7...08ff7a )
by Sam
07:43
created

UpdatePassword   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 36
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 34 4
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 Zend\Expressive\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 2
                if (_em()->getRepository(Log::class)->updatePasswordFailedOften()) {
32
                    throw new Exception('Trop de tentatives de changement de mot de passe ont échouées. Veuillez ressayer plus tard.');
33
                }
34
35
                /** @var UserRepository $repository */
36 2
                $repository = _em()->getRepository(User::class);
37
38
                /** @var User $user */
39 2
                $repository->getAclFilter()->setEnabled(false);
40 2
                $user = $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

40
                /** @scrutinizer ignore-call */ 
41
                $user = $repository->findOneByToken($args['token']);
Loading history...
41 2
                $repository->getAclFilter()->setEnabled(true);
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 1
            },
55
        ];
56
    }
57
}
58