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(); |
|
|
|
|
34
|
|
|
|
35
|
22 |
|
if (!User::getCurrent()) { |
36
|
2 |
|
return $acl->reject('the user is not logged in'); |
|
|
|
|
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
|
|
|
|
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.