BookableAvailable   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 15
eloc 34
c 0
b 0
f 0
dl 0
loc 86
ccs 34
cts 36
cp 0.9444
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
C assert() 0 61 13
A countBookingsExcludingTheNewOne() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Acl\Assertion;
6
7
use Application\Enum\BookingStatus;
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 string $privilege
27
     *
28
     * @return bool
29
     */
30 22
    public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null)
31
    {
32
        /** @var null|Booking $booking */
33 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

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

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