Passed
Push — master ( a5c076...fd0c83 )
by Adrien
15:16 queued 10:57
created

BookableAvailable::assert()   C

Complexity

Conditions 13
Paths 19

Size

Total Lines 61
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 13

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 61
ccs 31
cts 31
cp 1
rs 6.6166
cc 13
nc 19
nop 4
crap 13

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Acl\Assertion;
6
7
use Application\DBAL\Types\BookingStatusType;
8
use Application\Model\Booking;
9
use Application\Model\User;
10
use Ecodev\Felix\Acl\Assertion\NamedAssertion;
11
use Laminas\Permissions\Acl\Acl;
12
use Laminas\Permissions\Acl\Resource\ResourceInterface;
13
use Laminas\Permissions\Acl\Role\RoleInterface;
14
15
class BookableAvailable implements NamedAssertion
16
{
17
    public function getName(): string
18
    {
19
        return 'le réservable est est disponible';
20
    }
21
22
    /**
23
     * Assert that the bookable of the given booking can be rented by the current user.
24
     *
25
     * @param \Application\Acl\Acl $acl
26
     * @param RoleInterface $role
27
     * @param ResourceInterface $resource
28
     * @param string $privilege
29
     *
30
     * @return bool
31
     */
32 22
    public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null)
33
    {
34
        /** @var null|Booking $booking */
35 22
        $booking = $resource->getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on null. ( Ignorable by Annotation )

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

35
        /** @scrutinizer ignore-call */ 
36
        $booking = $resource->getInstance();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37 22
        if (!User::getCurrent()) {
38 2
            return $acl->reject('the user is not logged in');
0 ignored issues
show
Bug introduced by
The method reject() does not exist on Laminas\Permissions\Acl\Acl. It seems like you code against a sub-type of Laminas\Permissions\Acl\Acl such as Ecodev\Felix\Acl\Acl. ( Ignorable by Annotation )

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

38
            return $acl->/** @scrutinizer ignore-call */ reject('the user is not logged in');
Loading history...
39
        }
40
41 20
        if (!$booking) {
42 1
            return $acl->reject('the booking does not exist');
43
        }
44
45 19
        $bookable = $booking->getBookable();
46
47 19
        if (!$bookable) {
48
            // Booking using user's own equipment is always allowed
49 2
            return true;
50
        }
51
52 17
        if (!$bookable->isActive()) {
53 1
            return $acl->reject('the bookable is not active');
54
        }
55
56
        // Check that the user has ALL required licenses for the bookable
57 16
        if (!$bookable->getLicenses()->isEmpty() && User::getCurrent()->getRole() !== User::ROLE_BOOKING_ONLY) {
58 3
            $userLicenses = User::getCurrent()->getLicenses();
59
60 3
            foreach ($bookable->getLicenses() as $requiredLicense) {
61 3
                if (!$userLicenses->contains($requiredLicense)) {
62 2
                    return $acl->reject('the user does not have the required license: ' . $requiredLicense->getName());
63
                }
64
            }
65
        }
66
67
        // Check that the bookable has no more running bookings than its maximum
68 14
        if ($bookable->getSimultaneousBookingMaximum() >= 0) {
69 14
            $countConfirmed = $this->countBookingsExcludingTheNewOne($bookable->getSimultaneousBookings(), $booking);
70 14
            $countApplications = $this->countBookingsExcludingTheNewOne($bookable->getSimultaneousApplications(), $booking);
71 14
            $totalCount = $countConfirmed + $countApplications;
72
73 14
            $maximum = $bookable->getSimultaneousBookingMaximum();
74
75
            // If the booking is an application (not confirmed), then we might use the waiting list
76 14
            if ($booking->getStatus() === BookingStatusType::APPLICATION) {
77 11
                $maximum += $bookable->getWaitingListLength();
78
            }
79
80 14
            if ($totalCount >= $maximum) {
81 7
                $countUsedForHuman = min($totalCount, $bookable->getSimultaneousBookingMaximum());
82 7
                $countWaitingListForHuman = $totalCount - $countUsedForHuman;
83 7
                $reason = 'the limit of simultaneous bookings was reached: ' . $countUsedForHuman . '/' . $bookable->getSimultaneousBookingMaximum();
84 7
                if ($countWaitingListForHuman) {
85 3
                    $reason .= " and the waiting list is full: $countWaitingListForHuman/" . $bookable->getWaitingListLength();
86
                }
87
88 7
                return $acl->reject($reason);
89
            }
90
        }
91
92 7
        return true;
93
    }
94
95
    /**
96
     * Don't count the new booking that we just added to the collection.
97
     */
98 14
    private function countBookingsExcludingTheNewOne(array $bookings, Booking $booking): int
99
    {
100 14
        $filteredBookings = array_filter($bookings, fn (Booking $b): bool => $b !== $booking);
101
102 14
        return count($filteredBookings);
103
    }
104
}
105