Failed Conditions
Push — master ( 0fd122...b2f91e )
by Adrien
06:52
created

ConfirmRegistration::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 11.5287

Importance

Changes 0
Metric Value
cc 4
eloc 29
nc 1
nop 0
dl 0
loc 49
ccs 6
cts 27
cp 0.2222
crap 11.5287
rs 9.456
c 0
b 0
f 0
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')),
30 1
                'input' => Type::nonNull(_types()->getPartialInput(User::class)),
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
                $repository->getAclFilter()->setEnabled(false);
39
                $user = $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
                /** @scrutinizer ignore-call */ 
40
                $user = $repository->findOneByToken($args['token']);
Loading history...
40
                $repository->getAclFilter()->setEnabled(true);
41
42
                if (!$user || !$user->isTokenValid()) {
43
                    throw new Exception('Cannot confirm registration with an invalid token');
44
                }
45
46
                // Do it
47
                $input = $args['input'];
48
                Helper::hydrate($user, $input);
49
50
                // Active the member
51
                $user->setRole(User::ROLE_MEMBER);
52
                $user->setStatus(User::STATUS_ACTIVE);
53
54
                // Create mandatory booking for him
55
                User::setCurrent($user);
56
                $booking = new Booking();
57
                _em()->persist($booking);
58
59
                $booking->setOwner($user);
60
                $booking->setStatus(BookingStatusType::BOOKED);
61
                $booking->setStartDate(new Chronos());
62
63
                $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

63
                $mandatoryBookables = _em()->getRepository(Bookable::class)->/** @scrutinizer ignore-call */ findByBookingType(BookingTypeType::MANDATORY);
Loading history...
64
                foreach ($mandatoryBookables as $bookable) {
65
                    $booking->addBookable($bookable);
66
                }
67
68
                _em()->flush();
69
70
                return true;
71 1
            },
72
        ];
73
    }
74
}
75