Passed
Push — master ( 58ac10...e3c27b )
by Adrien
05:30
created

IsFamily::assert()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9.8645

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 12
c 1
b 0
f 0
nc 9
nop 4
dl 0
loc 24
rs 8.4444
ccs 9
cts 13
cp 0.6923
crap 9.8645
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Acl\Assertion;
6
7
use Application\Model\AbstractModel;
8
use Application\Model\User;
9
use Ecodev\Felix\Acl\Assertion\NamedAssertion;
10
use Laminas\Permissions\Acl\Acl;
11
use Laminas\Permissions\Acl\Resource\ResourceInterface;
12
use Laminas\Permissions\Acl\Role\RoleInterface;
13
14
class IsFamily implements NamedAssertion
15
{
16
    public function getName(): string
17
    {
18
        return "l'objet appartient à mon ménage";
19
    }
20
21
    /**
22
     * Assert that the object belongs to someone in the current user family.
23
     *
24
     * @param \Application\Acl\Acl $acl
25
     * @param RoleInterface $role
26
     * @param ResourceInterface $resource
27
     * @param string $privilege
28
     *
29
     * @return bool
30
     */
31 2
    public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null)
32
    {
33 2
        if ($resource === null) {
34
            return false;
35
        }
36
37
        /** @var AbstractModel $object */
38 2
        $object = $resource->getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on Laminas\Permissions\Acl\Resource\ResourceInterface. ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $object = $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...
39
40 2
        $currentFamilyOwner = User::getCurrent();
41 2
        if ($currentFamilyOwner && $currentFamilyOwner->getOwner()) {
42
            $currentFamilyOwner = $currentFamilyOwner->getOwner();
43
        }
44
45 2
        $objectFamilyOwner = $object->getOwner();
46 2
        if ($objectFamilyOwner && $objectFamilyOwner->getOwner()) {
47
            $objectFamilyOwner = $objectFamilyOwner->getOwner();
48
        }
49
50 2
        if ($currentFamilyOwner && $currentFamilyOwner === $objectFamilyOwner) {
51 2
            return true;
52
        }
53
54
        return $acl->reject('the object does not belong to the family');
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

54
        return $acl->/** @scrutinizer ignore-call */ reject('the object does not belong to the family');
Loading history...
55
    }
56
}
57