Failed Conditions
Push — master ( 15fc02...10b36b )
by Sylvain
09:13
created

ConfirmRegistration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 18.75%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 35
c 5
b 0
f 0
dl 0
loc 66
ccs 6
cts 32
cp 0.1875
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 64 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\UserRepository;
16
use Cake\Chronos\Chronos;
17
use GraphQL\Type\Definition\Type;
18
use Zend\Expressive\Session\SessionInterface;
19
20
abstract class ConfirmRegistration implements FieldInterface
21
{
22 1
    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
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

29
                'token' => Type::nonNull(/** @scrutinizer ignore-type */ _types()->get('Token')),
Loading history...
30 1
                'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')),
31
            ],
32
            '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

32
            '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...
33
34
                /** @var UserRepository $repository */
35
                $repository = _em()->getRepository(User::class);
36
37
                /** @var User $user */
38
                $user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) {
39
                    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

39
                    return $repository->/** @scrutinizer ignore-call */ findOneByToken($args['token']);
Loading history...
40
                });
41
42
                if (!$user) {
43
                    throw new Exception('Cannot confirm registration with an invalid token');
44
                }
45
46
                if (!$user->isTokenValid()) {
47
                    throw new Exception('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.');
48
                }
49
50
                $input = $args['input'];
51
52
                $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $input): void {
53
                    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

53
                    if ($repository->/** @scrutinizer ignore-call */ findOneByLogin($input['login'])) {
Loading history...
54
                        throw new Exception('Ce nom d\'utilisateur est déjà attribué et ne peut être utilisé');
55
                    }
56
                });
57
58
                // Do it
59
                Helper::hydrate($user, $input);
60
61
                // Active the member
62
                $user->initialize();
63
64
                // Create mandatory booking for him
65
                User::setCurrent($user);
66
67
                $mandatoryBookables = _em()->getRepository(Bookable::class)->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

67
                $mandatoryBookables = _em()->getRepository(Bookable::class)->/** @scrutinizer ignore-call */ findByBookingType(BookingTypeType::MANDATORY);
Loading history...
68
                foreach ($mandatoryBookables as $bookable) {
69
                    $booking = new Booking();
70
                    _em()->persist($booking);
71
72
                    $booking->setOwner($user);
73
                    $booking->setStatus(BookingStatusType::BOOKED);
74
                    $booking->setStartDate(new Chronos());
75
                    $booking->setBookable($bookable);
76
77
                    // Non-periodic bookable must be terminated immediately
78
                    if ($bookable->getPeriodicPrice()->isZero()) {
79
                        $booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement');
80
                    }
81
                }
82
83
                _em()->flush();
84
85
                return true;
86 1
            },
87
        ];
88
    }
89
}
90