Conditions | 6 |
Paths | 1 |
Total Lines | 66 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 6.008 |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
23 | 2 | public static function build(): array |
|
24 | { |
||
25 | return [ |
||
26 | 1 | 'name' => 'confirmRegistration', |
|
27 | 1 | 'type' => Type::nonNull(Type::boolean()), |
|
28 | 1 | 'description' => 'First step to register as a new user.', |
|
29 | 'args' => [ |
||
30 | 1 | 'token' => Type::nonNull(_types()->get('Token')), |
|
1 ignored issue
–
show
|
|||
31 | 1 | 'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')), |
|
32 | ], |
||
33 | 'resolve' => function ($root, array $args, SessionInterface $session): bool { |
||
1 ignored issue
–
show
|
|||
34 | |||
35 | /** @var UserRepository $repository */ |
||
36 | 2 | $repository = _em()->getRepository(User::class); |
|
37 | |||
38 | /** @var User $user */ |
||
39 | $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) { |
||
40 | 2 | return $repository->findOneByToken($args['token']); |
|
1 ignored issue
–
show
|
|||
41 | 2 | }); |
|
42 | |||
43 | 2 | if (!$user) { |
|
44 | 1 | throw new Exception('Cannot confirm registration with an invalid token'); |
|
45 | } |
||
46 | |||
47 | 1 | if (!$user->isTokenValid()) { |
|
48 | throw new Exception('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.'); |
||
49 | } |
||
50 | |||
51 | 1 | $input = $args['input']; |
|
52 | |||
53 | $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $input): void { |
||
54 | 1 | if ($repository->findOneByLogin($input['login'])) { |
|
1 ignored issue
–
show
|
|||
55 | throw new Exception('Ce nom d\'utilisateur est déjà attribué et ne peut être utilisé'); |
||
56 | } |
||
57 | 1 | }); |
|
58 | |||
59 | // Do it |
||
60 | 1 | Helper::hydrate($user, $input); |
|
61 | |||
62 | // Active the member |
||
63 | 1 | $user->initialize(); |
|
64 | |||
65 | // Create mandatory booking for him |
||
66 | 1 | User::setCurrent($user); |
|
67 | |||
68 | /** @var BookableRepository $bookableRepository */ |
||
69 | 1 | $bookableRepository = _em()->getRepository(Bookable::class); |
|
70 | 1 | $mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY); |
|
1 ignored issue
–
show
|
|||
71 | 1 | foreach ($mandatoryBookables as $bookable) { |
|
72 | 1 | $booking = new Booking(); |
|
73 | 1 | _em()->persist($booking); |
|
74 | |||
75 | 1 | $booking->setOwner($user); |
|
76 | 1 | $booking->setStatus(BookingStatusType::BOOKED); |
|
77 | 1 | $booking->setStartDate(new Chronos()); |
|
78 | 1 | $booking->setBookable($bookable); |
|
79 | |||
80 | // Non-periodic bookable must be terminated immediately |
||
81 | 1 | if ($bookable->getPeriodicPrice()->isZero()) { |
|
82 | 1 | $booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement'); |
|
83 | } |
||
84 | } |
||
85 | |||
86 | 1 | _em()->flush(); |
|
87 | |||
88 | 1 | return true; |
|
89 | 1 | }, |
|
93 |