Register   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 6
eloc 28
dl 0
loc 53
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 51 6
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 Application\Service\MessageQueuer;
12
use Ecodev\Felix\Api\ExceptionWithoutMailLogging;
13
use Ecodev\Felix\Api\Field\FieldInterface;
14
use Ecodev\Felix\Api\Scalar\EmailType;
15
use Ecodev\Felix\Service\Mailer;
16
use GraphQL\Type\Definition\Type;
17
use Mezzio\Session\SessionInterface;
18
19
abstract class Register implements FieldInterface
20
{
21 1
    public static function build(): iterable
22
    {
23 1
        yield 'register' => fn () => [
24 1
            'type' => Type::nonNull(Type::boolean()),
25 1
            'description' => 'First step to register as a new user.',
26 1
            'args' => [
27 1
                'email' => Type::nonNull(_types()->get(EmailType::class)),
28 1
            ],
29 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
30
                global $container;
31
32
                /** @var LogRepository $logRepository */
33 1
                $logRepository = _em()->getRepository(Log::class);
34
35
                // We don't want to disclose whether an user already exists, so we show
36
                // the same error for both too many register and password reset attempts
37 1
                if ($logRepository->updatePasswordFailedOften() || $logRepository->registerOften()) {
38
                    throw new ExceptionWithoutMailLogging('Trop de tentatives. Veuillez réessayer plus tard.');
39
                }
40
41
                /** @var Mailer $mailer */
42 1
                $mailer = $container->get(Mailer::class);
43
44
                /** @var MessageQueuer $messageQueuer */
45 1
                $messageQueuer = $container->get(MessageQueuer::class);
46
47
                /** @var UserRepository $repository */
48 1
                $repository = _em()->getRepository(User::class);
49
50
                /** @var null|User $user */
51 1
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByEmail($args['email']));
52
53 1
                $existingUser = (bool) $user;
54 1
                if (!$existingUser) {
55 1
                    $user = new User();
56 1
                    _em()->persist($user);
57
                }
58
59 1
                $user->setEmail($args['email']);
60
61 1
                if ($existingUser && $user->getPassword()) {
62
                    _log()->info(LogRepository::REQUEST_PASSWORD_RESET);
63
                    $message = $messageQueuer->queueResetPassword($user, $user->getEmail());
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of Application\Service\Mess...r::queueResetPassword() does only seem to accept Application\Model\User, maybe add an additional type check? ( Ignorable by Annotation )

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

63
                    $message = $messageQueuer->queueResetPassword(/** @scrutinizer ignore-type */ $user, $user->getEmail());
Loading history...
Bug introduced by
It seems like $user->getEmail() can also be of type null; however, parameter $email of Application\Service\Mess...r::queueResetPassword() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
                    $message = $messageQueuer->queueResetPassword($user, /** @scrutinizer ignore-type */ $user->getEmail());
Loading history...
64
                } else {
65 1
                    _log()->info(LogRepository::REGISTER);
66 1
                    $message = $messageQueuer->queueRegister($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of Application\Service\MessageQueuer::queueRegister() does only seem to accept Application\Model\User, maybe add an additional type check? ( Ignorable by Annotation )

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

66
                    $message = $messageQueuer->queueRegister(/** @scrutinizer ignore-type */ $user);
Loading history...
67
                }
68
69 1
                $mailer->sendMessageAsync($message);
70
71 1
                return true;
72 1
            },
73 1
        ];
74
    }
75
}
76