Passed
Push — master ( abcdc1...a80a3a )
by Sam
06:58
created

ConfirmRegistration::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 58
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4.0195

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 58
ccs 25
cts 28
cp 0.8929
crap 4.0195
rs 9.456

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 1
            '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(function () use ($repository, $args) {
44 2
                    return $repository->findOneByToken($args['token']);
45 2
                });
46
47 2
                if (!$user) {
48 1
                    throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Veuillez effectuer une nouvelle demande.');
49
                }
50
51 1
                if (!$user->isTokenValid()) {
52
                    throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
53
                }
54
55 1
                $input = $args['input'];
56
57
                // Do it
58 1
                Helper::hydrate($user, $input);
59
60
                // Active the member
61 1
                $user->initialize();
62
63
                // Login
64 1
                User::setCurrent($user);
65
66 1
                _em()->flush();
67
68
                // Give user automatic access via organization
69
                /** @var OrganizationRepository $organizationRepository */
70 1
                $organizationRepository = _em()->getRepository(Organization::class);
71 1
                $organizationRepository->applyOrganizationAccesses();
72
73 1
                foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
74
                    $message = $messageQueuer->queueConfirmedRegistration($adminEmail, $user);
75
                    $mailer->sendMessageAsync($message);
76
                }
77
78 1
                return true;
79 1
            },
80
        ];
81
    }
82
}
83