Passed
Push — master ( 6d5f29...32d930 )
by Adrien
08:15
created

BookableAvailable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 59
ccs 18
cts 22
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B assert() 0 47 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Acl\Assertion;
6
7
use Application\Model\Booking;
8
use Application\Model\User;
9
use Zend\Permissions\Acl\Acl;
10
use Zend\Permissions\Acl\Assertion\AssertionInterface;
11
use Zend\Permissions\Acl\Resource\ResourceInterface;
12
use Zend\Permissions\Acl\Role\RoleInterface;
13
14
class BookableAvailable implements AssertionInterface
15
{
16
    /**
17
     * Assert that the bookable of the given booking can be rented by the current user
18
     *
19
     * @param Acl $acl
20
     * @param RoleInterface $role
21
     * @param ResourceInterface $resource
22
     * @param string $privilege
23
     *
24
     * @return bool
25
     */
26 4
    public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
27
    {
28 4
        $booking = $resource->getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on Zend\Permissions\Acl\Resource\ResourceInterface. It seems like you code against a sub-type of Zend\Permissions\Acl\Resource\ResourceInterface such as Application\Acl\ModelResource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $booking = $resource->getInstance();
Loading history...
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

28
        /** @scrutinizer ignore-call */ 
29
        $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...
29
30 4
        if (!$booking) {
31
            return false;
32
        }
33
34 4
        if (!User::getCurrent()) {
35
            return false;
36
        }
37
38 4
        $bookable = $booking->getBookable();
39
40 4
        if (!$bookable) {
41
            // Booking using user's own equipment is always allowed
42 2
            return true;
43
        }
44
45 2
        if (!$bookable->isActive()) {
46
            return false;
47
        }
48
49
        // Check that the user has ALL required licenses for the bookable
50 2
        if (!$bookable->getLicenses()->isEmpty() && User::getCurrent()->getRole() !== User::ROLE_BOOKING_ONLY) {
51 1
            $userLicenses = User::getCurrent()->getLicenses();
52
53 1
            foreach ($bookable->getLicenses() as $requiredLicense) {
54 1
                if (!$userLicenses->contains($requiredLicense)) {
55 1
                    return false;
56
                }
57
            }
58
        }
59
60 1
        if ($bookable->getSimultaneousBookingMaximum() > 0) {
61
            // Check that the bookable has no more running bookings than its maximum
62 1
            $runningBookings = _em()->getRepository(Booking::class)->findBy([
63 1
                'bookable' => $bookable,
64
                'endDate' => null,
65
            ]);
66
67 1
            if (count($runningBookings) >= $bookable->getSimultaneousBookingMaximum()) {
68
                return false;
69
            }
70
        }
71
72 1
        return true;
73
    }
74
}
75