BookableAvailable::assert()   C
last analyzed

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\Enum\BookableStatus;
8
use Application\Enum\BookingStatus;
9
use Application\Model\Booking;
10
use Application\Model\User;
11
use Ecodev\Felix\Acl\Assertion\NamedAssertion;
12
use Laminas\Permissions\Acl\Acl;
13
use Laminas\Permissions\Acl\Resource\ResourceInterface;
14
use Laminas\Permissions\Acl\Role\RoleInterface;
15
16
class BookableAvailable implements NamedAssertion
17
{
18
    public function getName(): string
19
    {
20
        return 'le réservable est est disponible';
21
    }
22
23
    /**
24
     * Assert that the bookable of the given booking can be rented by the current user.
25
     *
26
     * @param \Application\Acl\Acl $acl
27
     * @param string $privilege
28
     *
29
     * @return bool
30
     */
31 22
    public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null)
32
    {
33
        /** @var null|Booking $booking */
34 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

34
        /** @scrutinizer ignore-call */ 
35
        $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...
35
36 22
        if (!User::getCurrent()) {
37 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

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