Test Failed
Push — master ( 3abf2e...da9b67 )
by Elijah
03:54
created

Role::getRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
use Symfony\Component\Security\Core\Role\Role as BaseRole;
15
16
abstract class Role extends BaseRole implements RoleInterface
17
{
18
    /**
19
     * The machine name of this role.
20
     *
21
     * @var mixed
22
     */
23
    protected $id;
24
25
    /**
26
     * The human-readable name of this role.
27
     *
28
     * @var string
29
     */
30
    protected $role;
31
32
    /**
33
     * The permissions belonging to this role.
34
     *
35
     * @var array
36
     */
37
    protected $permissions;
38
39
    public function __construct($role = '')
40
    {
41
        parent::__construct($role);
42
        $this->permissions = [];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getId()
49
    {
50
        return $this->id;
51
    }
52
53
    public function getRole()
54
    {
55
        return $this->role;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function setRole($role)
62
    {
63
        $this->role = $role;
64
65
        return $this;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getPermissions()
72
    {
73
        if ($this->isSuperAdmin()) {
74
            return [];
75
        }
76
77
        return $this->permissions;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function setPermissions(array $permissions)
84
    {
85
        $this->permissions = [];
86
87
        foreach ($permissions as $permission) {
88
            if (!$permission) {
89
                break;
90
            }
91
            $this->grantPermission($permission);
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function hasPermission($permission)
101
    {
102
        if ($this->isSuperAdmin()) {
103
            return true;
104
        }
105
106
        return in_array($permission, $this->getPermissions(), true);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function grantPermission($permission)
113
    {
114
        if ($this->isSuperAdmin()) {
115
            return $this;
116
        }
117
        if (!$this->hasPermission($permission)) {
118
            $this->permissions[] = $permission;
119
        }
120
121
        return $this;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function revokePermission($permission)
128
    {
129
        if ($this->isSuperAdmin()) {
130
            return $this;
131
        }
132
        $this->permissions = array_diff($this->permissions, [$permission]);
133
134
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function isSuperAdmin()
141
    {
142
        return $this->getRole() === static::ROLE_SUPER_ADMIN;
143
    }
144
145
    public function prePersist()
146
    {
147
        // TODO sort permissions alphabetically, convert permission to lowercase
148
    }
149
}
150