Failed Conditions
Push — master ( a427b2...509ea4 )
by Adrien
16:44
created

ConfirmRegistration::build()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 72
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7.0084

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 72
ccs 34
cts 36
cp 0.9444
rs 8.3946
c 0
b 0
f 0
cc 7
nc 1
nop 0
crap 7.0084

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\BookingStatusType;
9
use Application\DBAL\Types\BookingTypeType;
10
use Application\Model\Account;
11
use Application\Model\Bookable;
12
use Application\Model\Booking;
13
use Application\Model\User;
14
use Application\Repository\AccountRepository;
15
use Application\Repository\BookableRepository;
16
use Application\Repository\UserRepository;
17
use Cake\Chronos\Chronos;
18
use Ecodev\Felix\Api\Exception;
19
use Ecodev\Felix\Api\ExceptionWithoutMailLogging;
20
use Ecodev\Felix\Api\Field\FieldInterface;
21
use GraphQL\Type\Definition\Type;
22
use Mezzio\Session\SessionInterface;
23
24
abstract class ConfirmRegistration implements FieldInterface
25
{
26 2
    public static function build(): array
27
    {
28
        return [
29 1
            'name' => 'confirmRegistration',
30 1
            'type' => Type::nonNull(Type::boolean()),
31 1
            'description' => 'Second step to register as a new user.',
32
            'args' => [
33 1
                'token' => Type::nonNull(_types()->get('Token')),
34 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
35
            ],
36 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
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. Effectue une nouvelle demande.');
46
                }
47
48 1
                if (!$user->isTokenValid()) {
49
                    throw new ExceptionWithoutMailLogging('Le lien que tu as suivi est périmé. Effectue une nouvelle demande.');
50
                }
51
52 1
                $input = $args['input'];
53
54 1
                $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $input): void {
55 1
                    if ($repository->findOneByLogin($input['login'])) {
56
                        throw new Exception('Ce nom d\'utilisateur est déjà attribué et ne peut être utilisé');
57
                    }
58
                });
59
60
                // Do it
61 1
                Helper::hydrate($user, $input);
62
63
                // Active the member
64 1
                $user->initialize();
65
66
                // Create mandatory booking for him
67 1
                User::setCurrent($user);
68
69
                // Create account so the user can top-up money and start purchasing services
70
                // but only if it's head of the family
71 1
                if ($user->isFamilyOwner()) {
72
                    /** @var AccountRepository $accountRepository */
73 1
                    $accountRepository = _em()->getRepository(Account::class);
74 1
                    $accountRepository->getOrCreate($user);
75
                }
76
77
                /** @var BookableRepository $bookableRepository */
78 1
                $bookableRepository = _em()->getRepository(Bookable::class);
79 1
                $mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY);
80 1
                foreach ($mandatoryBookables as $bookable) {
81 1
                    $booking = new Booking();
82 1
                    _em()->persist($booking);
83
84 1
                    $booking->setOwner($user);
85 1
                    $booking->setStatus(BookingStatusType::BOOKED);
86 1
                    $booking->setStartDate(new Chronos());
87 1
                    $booking->setBookable($bookable);
88
89
                    // Non-periodic bookable must be terminated immediately
90 1
                    if ($bookable->getPeriodicPrice()->isZero()) {
91 1
                        $booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement');
92
                    }
93
                }
94
95 1
                _em()->flush();
96
97 1
                return true;
98
            },
99
        ];
100
    }
101
}
102