Role::getRole()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Model;
13
14
abstract class Role implements RoleInterface
15
{
16
    /**
17
     * The machine name of this role.
18
     *
19
     * @var mixed
20
     */
21
    protected $id;
22
23
    /**
24
     * The human-readable name of this role.
25
     *
26
     * @var string
27
     */
28
    protected $role;
29
30
    /**
31
     * The permissions belonging to this role.
32
     *
33
     * @var array
34
     */
35
    protected $permissions;
36
37 15
    public function __construct($role = null)
38
    {
39 15
        $this->role = $role;
40 15
        $this->permissions = [];
41 15
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getId()
47
    {
48
        return $this->id;
49
    }
50
51 10
    public function getRole()
52
    {
53 10
        return $this->role;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 5
    public function setRole($role)
60
    {
61 5
        $this->role = $role;
62
63 5
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 6
    public function getPermissions()
70
    {
71 6
        if ($this->isSuperAdmin()) {
72 1
            return [];
73
        }
74
75 6
        return $this->permissions;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function setPermissions(array $permissions)
82
    {
83 2
        $this->permissions = [];
84
85 2
        foreach ($permissions as $permission) {
86 2
            if (!$permission) {
87 1
                break;
88
            }
89 2
            $this->grantPermission($permission);
90
        }
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 6
    public function hasPermission($permission)
99
    {
100 6
        if ($this->isSuperAdmin()) {
101 1
            return true;
102
        }
103
104 6
        return in_array($permission, $this->getPermissions(), true);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 6
    public function grantPermission($permission)
111
    {
112 6
        if ($this->isSuperAdmin()) {
113 1
            return $this;
114
        }
115 5
        if (!$this->hasPermission($permission)) {
116 5
            $this->permissions[] = $permission;
117
        }
118
119 5
        return $this;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 3
    public function revokePermission($permission)
126
    {
127 3
        if ($this->isSuperAdmin()) {
128 1
            return $this;
129
        }
130 2
        $this->permissions = array_diff($this->permissions, [$permission]);
131
132 2
        return $this;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 7
    public function isSuperAdmin()
139
    {
140 7
        return $this->getRole() === static::ROLE_SUPER_ADMIN;
141
    }
142
}
143