Failed Conditions
Push — master ( 044ed2...482608 )
by Adrien
03:59
created

ConfirmRegistration   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
wmc 7
eloc 37
dl 0
loc 73
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 71 7
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(): iterable
27
    {
28 1
        yield 'confirmRegistration' => fn () => [
29 1
            'type' => Type::nonNull(Type::boolean()),
30 1
            'description' => 'Second step to register as a new user.',
31 1
            'args' => [
32 1
                'token' => Type::nonNull(_types()->get('Token')),
33 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
34 1
            ],
35 1
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
36
                /** @var UserRepository $repository */
37 2
                $repository = _em()->getRepository(User::class);
38
39
                /** @var null|User $user */
40 2
                $user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token']));
41
42 2
                if (!$user) {
43 1
                    throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Effectue une nouvelle demande.');
44
                }
45
46 1
                if (!$user->isTokenValid()) {
47
                    throw new ExceptionWithoutMailLogging('Le lien que tu as suivi est périmé. Effectue une nouvelle demande.');
48
                }
49
50 1
                $input = $args['input'];
51
52 1
                $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $input): void {
53 1
                    if ($repository->findOneByLogin($input['login'])) {
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
                // Login
65 1
                Login::doLogin($session, $user);
66
67
                // Create account so the user can top-up money and start purchasing services
68
                // but only if it's head of the family
69 1
                if ($user->isFamilyOwner()) {
70
                    /** @var AccountRepository $accountRepository */
71 1
                    $accountRepository = _em()->getRepository(Account::class);
72 1
                    $accountRepository->getOrCreate($user);
73
                }
74
75
                // Create mandatory booking for him
76
                /** @var BookableRepository $bookableRepository */
77 1
                $bookableRepository = _em()->getRepository(Bookable::class);
78 1
                $mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY);
79 1
                foreach ($mandatoryBookables as $bookable) {
80 1
                    $booking = new Booking();
81 1
                    _em()->persist($booking);
82
83 1
                    $booking->setOwner($user);
84 1
                    $booking->setStatus(BookingStatusType::BOOKED);
85 1
                    $booking->setStartDate(new Chronos());
86 1
                    $booking->setBookable($bookable);
87
88
                    // Non-periodic bookable must be terminated immediately
89 1
                    if ($bookable->getPeriodicPrice()->isZero()) {
90 1
                        $booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement');
91
                    }
92
                }
93
94 1
                _em()->flush();
95
96 1
                return true;
97 1
            },
98 1
        ];
99
    }
100
}
101