ConfirmRegistration::build()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4.0195

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 55
ccs 25
cts 28
cp 0.8929
rs 9.488
c 0
b 0
f 0
cc 4
nc 1
nop 0
crap 4.0195

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Model\Organization;
9
use Application\Model\User;
10
use Application\Repository\OrganizationRepository;
11
use Application\Repository\UserRepository;
12
use Application\Service\MessageQueuer;
13
use Ecodev\Felix\Api\ExceptionWithoutMailLogging;
14
use Ecodev\Felix\Api\Field\FieldInterface;
15
use Ecodev\Felix\Service\Mailer;
16
use GraphQL\Type\Definition\Type;
17
use Mezzio\Session\SessionInterface;
18
19
abstract class ConfirmRegistration implements FieldInterface
20
{
21 2
    public static function build(): iterable
22
    {
23 1
        yield 'confirmRegistration' => fn () => [
24 1
            'type' => Type::nonNull(Type::boolean()),
25 1
            'description' => 'Second step to register as a new user.',
26 1
            'args' => [
27 1
                'token' => Type::nonNull(_types()->get('Token')),
28 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
29 1
            ],
30 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
31
                global $container;
32
                /** @var Mailer $mailer */
33 2
                $mailer = $container->get(Mailer::class);
34
35
                /** @var MessageQueuer $messageQueuer */
36 2
                $messageQueuer = $container->get(MessageQueuer::class);
37
38
                /** @var UserRepository $repository */
39 2
                $repository = _em()->getRepository(User::class);
40
41
                /** @var null|User $user */
42 2
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token']));
43
44 2
                if (!$user) {
45 1
                    throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Veuillez effectuer une nouvelle demande.');
46
                }
47
48 1
                if (!$user->isTokenValid()) {
49
                    throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
50
                }
51
52 1
                $input = $args['input'];
53
54
                // Do it
55 1
                Helper::hydrate($user, $input);
56
57
                // Active the member
58 1
                $user->initialize();
59
60
                // Login
61 1
                Login::doLogin($session, $user);
62
63 1
                _em()->flush();
64
65
                // Give user automatic access via organization
66
                /** @var OrganizationRepository $organizationRepository */
67 1
                $organizationRepository = _em()->getRepository(Organization::class);
68 1
                $organizationRepository->applyOrganizationAccesses();
69
70 1
                foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
71
                    $message = $messageQueuer->queueConfirmedRegistration($adminEmail, $user);
72
                    $mailer->sendMessageAsync($message);
73
                }
74
75 1
                return true;
76 1
            },
77 1
        ];
78
    }
79
}
80