Role   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 17
c 2
b 0
f 1
dl 0
loc 82
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isUserInRole() 0 3 1
A __construct() 0 8 1
A isUserInRoleById() 0 7 2
A isUserInRoleByName() 0 7 2
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authorization;
13
14
use Linna\Authentication\User;
15
use Linna\DataMapper\DomainObjectAbstract;
16
17
/**
18
 * Role.
19
 */
20
class Role extends DomainObjectAbstract
21
{
22
    use PermissionTrait;
23
24
    /**
25
     * @var string Group name
26
     */
27
    public string $name = '';
28
29
    /**
30
     * @var string Group description
31
     */
32
    public string $description = '';
33
34
    /**
35
     * @var int It say if group is active or not
36
     */
37
    public int $active = 0;
38
39
    /**
40
     * @var array<mixed> Contain users in group
41
     */
42
    private array $users = [];
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param array<mixed> $users
48
     * @param array<mixed> $permissions
49
     */
50 219
    public function __construct(array $users = [], array $permissions = [])
51
    {
52 219
        $this->users = $users;
53 219
        $this->permission = $permissions;
54
55
        //set required type
56 219
        \settype($this->id, 'integer');
57 219
        \settype($this->active, 'integer');
58 219
    }
59
60
    /**
61
     * Check if an user is in role, use User instance.
62
     *
63
     * @param User $user
64
     *
65
     * @return bool
66
     */
67 21
    public function isUserInRole(User $user): bool
68
    {
69 21
        return $this->isUserInRoleById($user->getId());
70
    }
71
72
    /**
73
     * Check if an user is in role, use the user Id.
74
     *
75
     * @param int $userId
76
     *
77
     * @return bool
78
     */
79 42
    public function isUserInRoleById(int $userId): bool
80
    {
81 42
        if (isset($this->users[$userId])) {
82 14
            return true;
83
        }
84
85 28
        return false;
86
    }
87
88
    /**
89
     * Check if an user is in role, use the user name.
90
     *
91
     * @param string $userName
92
     *
93
     * @return bool
94
     */
95 21
    public function isUserInRoleByName(string $userName): bool
96
    {
97 21
        if (\in_array($userName, \array_column($this->users, 'name'), true)) {
98 7
            return true;
99
        }
100
101 14
        return false;
102
    }
103
}
104