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