Failed Conditions
Push — master ( 0a5f39...49863b )
by Adrien
09:52
created

ConfirmRegistration::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 61
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 31
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 61
ccs 27
cts 30
cp 0.9
crap 4.016
rs 9.424

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