IsOwnerOrResponsible   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 54.17%

Importance

Changes 0
Metric Value
wmc 11
eloc 23
dl 0
loc 56
ccs 13
cts 24
cp 0.5417
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
B assert() 0 40 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Acl\Assertion;
6
7
use Application\Model\AbstractModel;
8
use Application\Model\Card;
9
use Application\Model\Change;
10
use Application\Model\Collection;
11
use Application\Model\User;
12
use Ecodev\Felix\Acl\Assertion\NamedAssertion;
13
use Ecodev\Felix\Acl\ModelResource;
14
use Laminas\Permissions\Acl\Acl;
15
use Laminas\Permissions\Acl\Resource\ResourceInterface;
16
use Laminas\Permissions\Acl\Role\RoleInterface;
17
18
class IsOwnerOrResponsible implements NamedAssertion
19
{
20
    public function getName(): string
21
    {
22
        return "l'objet m'appartient ou j'en suis responsable";
23
    }
24
25
    /**
26
     * Assert that the object belongs to the current user, or belong to a collection that the user is responsible of.
27
     *
28
     * @param \Application\Acl\Acl $acl
29
     * @param ModelResource $resource
30
     * @param string $privilege
31
     *
32
     * @return bool
33
     */
34 8
    public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null)
35
    {
36
        /** @var AbstractModel $object */
37 8
        $object = $resource->getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on Laminas\Permissions\Acl\Resource\ResourceInterface. It seems like you code against a sub-type of Laminas\Permissions\Acl\Resource\ResourceInterface such as Ecodev\Felix\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

37
        /** @scrutinizer ignore-call */ 
38
        $object = $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

37
        /** @scrutinizer ignore-call */ 
38
        $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...
38
39
        // Without user no chance to be the owner
40 8
        if (!User::getCurrent()) {
41
            return $acl->reject('it is not himself');
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

41
            return $acl->/** @scrutinizer ignore-call */ reject('it is not himself');
Loading history...
42
        }
43
44 8
        if (User::getCurrent() === $object->getOwner()) {
45 6
            return true;
46
        }
47
48
        // If not direct owner, look for indirect collection responsible
49
        /** @var Collection[] $collections */
50 3
        $collections = [];
51 3
        if ($object instanceof Collection) {
52 3
            $collections = [$object];
53 1
        } elseif ($object instanceof Card) {
54 1
            $collections = $object->getCollections();
55
        } elseif ($object instanceof Change) {
56
            $original = $object->getOriginal();
57
            if ($original) {
58
                $collections = $original->getCollections()->toArray();
59
            }
60
61
            $suggestion = $object->getSuggestion();
62
            if ($suggestion) {
63
                $collections = array_merge($collections, $suggestion->getCollections()->toArray());
64
            }
65
        }
66
67 3
        foreach ($collections as $collection) {
68 3
            if ($collection->getUsers()->contains(User::getCurrent())) {
69
                return true;
70
            }
71
        }
72
73 3
        return $acl->reject('it is not the owner, nor one of the responsible');
74
    }
75
}
76