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