Register   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
wmc 7
eloc 33
dl 0
loc 59
ccs 29
cts 32
cp 0.9063
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 57 7
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
                'hasInsurance' => Type::nonNull(Type::boolean()),
29 1
                'termsAgreement' => Type::nonNull(Type::boolean()),
30 1
            ],
31 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
32
                global $container;
33
34
                /** @var LogRepository $logRepository */
35 1
                $logRepository = _em()->getRepository(Log::class);
36
37
                // We don't want to disclose whether an user already exists, so we show
38
                // the same error for both too many register and password reset attempts
39 1
                if ($logRepository->updatePasswordFailedOften() || $logRepository->registerOften()) {
40
                    throw new ExceptionWithoutMailLogging('Trop de tentatives. Veuillez réessayer plus tard.');
41
                }
42
43
                /** @var Mailer $mailer */
44 1
                $mailer = $container->get(Mailer::class);
45
46
                /** @var MessageQueuer $messageQueuer */
47 1
                $messageQueuer = $container->get(MessageQueuer::class);
48
49
                /** @var UserRepository $repository */
50 1
                $repository = _em()->getRepository(User::class);
51
52
                /** @var null|User $user */
53 1
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByEmail($args['email']));
54
55 1
                $existingUser = (bool) $user;
56 1
                if (!$existingUser) {
57 1
                    $user = new User();
58 1
                    _em()->persist($user);
59
                }
60
61 1
                $user->setEmail($args['email']);
62 1
                $user->setHasInsurance($args['hasInsurance']);
63 1
                $user->setTermsAgreement($args['termsAgreement']);
64
65 1
                if ($existingUser && $user->getLogin()) {
66
                    _log()->info(LogRepository::REQUEST_PASSWORD_RESET);
67
                    $message = $messageQueuer->queueResetPassword($user);
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

67
                    $message = $messageQueuer->queueResetPassword(/** @scrutinizer ignore-type */ $user);
Loading history...
68
                } else {
69 1
                    _log()->info(LogRepository::REGISTER);
70 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

70
                    $message = $messageQueuer->queueRegister(/** @scrutinizer ignore-type */ $user);
Loading history...
71
                }
72
73 1
                if ($message) {
74 1
                    $mailer->sendMessageAsync($message);
75
                }
76
77 1
                return true;
78 1
            },
79 1
        ];
80
    }
81
}
82