Passed
Push — master ( 1d5fb4...e5c3c4 )
by Sam
06:33 queued 01:55
created

ConfirmRegistration::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0275

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 56
ccs 22
cts 25
cp 0.88
rs 9.472
c 1
b 0
f 0
cc 4
nc 1
nop 0
crap 4.0275

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(): array
22
    {
23
        return [
24 1
            'name' => 'confirmRegistration',
25 1
            'type' => Type::nonNull(Type::boolean()),
26
            'description' => 'Second step to register as a new user.',
27
            'args' => [
28 1
                'token' => Type::nonNull(_types()->get('Token')),
29 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
30
            ],
31 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
32
                global $container;
33
                /** @var Mailer $mailer */
34 2
                $mailer = $container->get(Mailer::class);
35
36
                /** @var MessageQueuer $messageQueuer */
37 2
                $messageQueuer = $container->get(MessageQueuer::class);
38
39
                /** @var UserRepository $repository */
40 2
                $repository = _em()->getRepository(User::class);
41
42
                /** @var null|User $user */
43 2
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token']));
44
45 2
                if (!$user) {
46 1
                    throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Veuillez effectuer une nouvelle demande.');
47
                }
48
49 1
                if (!$user->isTokenValid()) {
50
                    throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
51
                }
52
53 1
                $input = $args['input'];
54
55
                // Do it
56 1
                Helper::hydrate($user, $input);
57
58
                // Active the member
59 1
                $user->initialize();
60
61
                // Login
62 1
                User::setCurrent($user);
63
64 1
                _em()->flush();
65
66
                // Give user automatic access via organization
67
                /** @var OrganizationRepository $organizationRepository */
68 1
                $organizationRepository = _em()->getRepository(Organization::class);
69 1
                $organizationRepository->applyOrganizationAccesses();
70
71 1
                foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
72
                    $message = $messageQueuer->queueConfirmedRegistration($adminEmail, $user);
73
                    $mailer->sendMessageAsync($message);
74
                }
75
76 1
                return true;
77
            },
78
        ];
79
    }
80
}
81