Failed Conditions
Push — master ( 6a8bd6...5def7d )
by Adrien
08:40
created

ConfirmRegistration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 68
ccs 31
cts 33
cp 0.9394
rs 10
c 4
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 66 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Exception;
8
use Application\Api\Field\FieldInterface;
9
use Application\Api\Helper;
10
use Application\DBAL\Types\BookingStatusType;
11
use Application\DBAL\Types\BookingTypeType;
12
use Application\Model\Bookable;
13
use Application\Model\Booking;
14
use Application\Model\User;
15
use Application\Repository\BookableRepository;
16
use Application\Repository\UserRepository;
17
use Cake\Chronos\Chronos;
18
use GraphQL\Type\Definition\Type;
19
use Mezzio\Session\SessionInterface;
20
21
abstract class ConfirmRegistration implements FieldInterface
22
{
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
Bug introduced by
_types()->get('Token') of type GraphQL\Type\Definition\Type is incompatible with the type GraphQL\Type\Definition\NullableType expected by parameter $wrappedType of GraphQL\Type\Definition\Type::nonNull(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
                'token' => Type::nonNull(/** @scrutinizer ignore-type */ _types()->get('Token')),
Loading history...
31 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
32
            ],
33
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
1 ignored issue
show
Unused Code introduced by
The parameter $session is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
            'resolve' => function ($root, array $args, /** @scrutinizer ignore-unused */ SessionInterface $session): bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
Bug introduced by
The method findOneByToken() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
                    return $repository->/** @scrutinizer ignore-call */ findOneByToken($args['token']);
Loading history...
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
Bug introduced by
The method findOneByLogin() does not exist on Application\Repository\UserRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
                    if ($repository->/** @scrutinizer ignore-call */ findOneByLogin($input['login'])) {
Loading history...
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
Bug introduced by
The method findByBookingType() does not exist on Application\Repository\BookableRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                /** @scrutinizer ignore-call */ 
71
                $mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY);
Loading history...
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
            },
90
        ];
91
    }
92
}
93