Completed
Push — master ( fc7aa2...a1b4af )
by Sebastian
03:22
created

Role   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
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 80
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isUserInRole() 0 3 1
A __construct() 0 9 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 Contain users in group
41
     */
42
    private array $users = [];
43
44
    /**
45
     * Constructor.
46
     */
47 219
    public function __construct(array $users = [], array $permissions = [])
48
    {
49 219
        $this->users = $users;
50 219
        $this->permission = $permissions;
51
52
        //set required type
53 219
        \settype($this->id, 'integer');
54
        //\settype($this->objectId, 'integer');
55 219
        \settype($this->active, 'integer');
56 219
    }
57
58
    /**
59
     * Check if an user is in role, use User instance.
60
     *
61
     * @param User $user
62
     *
63
     * @return bool
64
     */
65 21
    public function isUserInRole(User $user): bool
66
    {
67 21
        return $this->isUserInRoleById($user->getId());
68
    }
69
70
    /**
71
     * Check if an user is in role, use the user Id.
72
     *
73
     * @param int $userId
74
     *
75
     * @return bool
76
     */
77 42
    public function isUserInRoleById(int $userId): bool
78
    {
79 42
        if (isset($this->users[$userId])) {
80 14
            return true;
81
        }
82
83 28
        return false;
84
    }
85
86
    /**
87
     * Check if an user is in role, use the user name.
88
     *
89
     * @param string $userName
90
     *
91
     * @return bool
92
     */
93 21
    public function isUserInRoleByName(string $userName): bool
94
    {
95 21
        if (\in_array($userName, \array_column($this->users, 'name'), true)) {
96 7
            return true;
97
        }
98
99 14
        return false;
100
    }
101
}
102