Failed Conditions
Push — master ( 4369d4...a5c076 )
by Adrien
15:03
created

BookableAvailable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
wmc 15
eloc 34
c 0
b 0
f 0
dl 0
loc 88
ccs 19
cts 25
cp 0.76
rs 10

3 Methods

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