Passed
Branch 001-basic (dcea18)
by Mr.
07:50
created

Acl   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 71
ccs 24
cts 24
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A isAllowed() 0 13 3
A parseUserPayload() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Service;
6
7
use Laminas\Permissions\Acl\Resource\GenericResource;
8
use Laminas\Permissions\Acl\Resource\ResourceInterface;
9
use Laminas\Permissions\Acl\Role\GenericRole;
10
use Laminas\Permissions\Acl\Role\RoleInterface;
11
use LibraryCatalog\ValueObject\SessionUser;
12
13
class Acl extends \Laminas\Permissions\Acl\Acl implements AclInterface
14
{
15
    // Roles.
16
    public const GUEST = 'guest';
17
    public const USER = 'user';
18
    public const ADMIN = 'admin';
19
20
    // Resources.
21
    public const BOOK = 'book';
22
    public const AUTHOR = 'author';
23
24
    // Privileges.
25
    public const READ = 'read';
26
    public const ADD = 'add';
27
28
    /**
29
     * Acl constructor.
30
     */
31 1
    public function __construct()
32
    {
33 1
        $guest = new GenericRole(static::GUEST);
34 1
        $user = new GenericRole(static::USER);
35 1
        $admin = new GenericRole(static::ADMIN);
36
37 1
        $book = new GenericResource(static::BOOK);
38 1
        $author = new GenericResource(static::AUTHOR);
39
40 1
        $this->addRole($guest)
41 1
            ->addRole($user, $guest)
42 1
            ->addRole($admin, $user)
43 1
            ->addResource($book)
44 1
            ->addResource($author);
45
46 1
        $this->allow($guest, [$book, $author], static::READ)
47 1
            ->allow($user, $book, static::ADD)
48 1
            ->allow($admin, $author, static::ADD);
49 1
    }
50
51
    /**
52
     * Generate SessionUser from payload;
53
     * Payload example: "user:182".
54
     *
55
     * @param string $payload
56
     * @return SessionUser
57
     */
58 13
    public function parseUserPayload(string $payload): SessionUser
59
    {
60 13
        return SessionUser::createFromPayload($payload);
61
    }
62
63
    /**
64
     * SessionUser can be used instead of Role.
65
     *
66
     * @param  RoleInterface|SessionUser|string $role
67
     * @param  ResourceInterface|string $resource
68
     * @param  string $privilege
69
     * @return bool
70
     */
71 13
    public function isAllowed($role = null, $resource = null, $privilege = null)
72
    {
73
        // Autotransform SessionUser to RoleId.
74 13
        if ($role instanceof SessionUser) {
75 13
            $role = $role->roleId;
76
        }
77
78
        try {
79 13
            $res = parent::isAllowed($role, $resource, $privilege);
0 ignored issues
show
Bug introduced by
It seems like $role can also be of type LibraryCatalog\ValueObject\SessionUser; however, parameter $role of Laminas\Permissions\Acl\Acl::isAllowed() does only seem to accept Laminas\Permissions\Acl\Role\RoleInterface|string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            $res = parent::isAllowed(/** @scrutinizer ignore-type */ $role, $resource, $privilege);
Loading history...
80 4
        } catch (\InvalidArgumentException $e) {
81 4
            $res = false;
82
        }
83 13
        return $res;
84
    }
85
}
86