Completed
Push — master ( 5e35ca...9a4201 )
by Mathieu
04:03
created

Role::setDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Charcoal\User\Acl;
4
5
use InvalidArgumentException;
6
7
use Pimple\Container;
8
9
use Charcoal\Translator\TranslatorAwareTrait;
10
11
// From 'charcoal-core'
12
use Charcoal\Model\AbstractModel;
13
14
/**
15
 * ACL Roles define hierarchical allowed and denied permissions.
16
 *
17
 * They can be attached to user accounts for fine-grained permission control.
18
 */
19
class Role extends AbstractModel
20
{
21
    use TranslatorAwareTrait;
22
23
    const SEPARATOR = ',';
24
25
    /**
26
     * @var string|null $ident
27
     */
28
    public $ident;
29
30
    /**
31
     * The parent ACL role.
32
     *
33
     * This role will inherit all of its parent's permissions.
34
     *
35
     * @var string $parent
36
     */
37
    public $parent;
38
39
    /**
40
     * The user-friendly name.
41
     *
42
     * @var \Charcoal\Translator\Translation
43
     */
44
    public $name;
45
46
    /**
47
     * List of explicitely allowed permissions.
48
     *
49
     * @var string[]|null $allowed
50
     */
51
    public $allowed;
52
53
    /**
54
     * List of explicitely denied permissions.
55
     *
56
     * @var string[]|null $denied
57
     */
58
    public $denied;
59
60
    /**
61
     * @var boolean
62
     */
63
    private $superuser = false;
64
65
    /**
66
     * @var integer
67
     */
68
    private $position;
69
70
    /**
71
     * ACL Role can be used as a string (ident).
72
     *
73
     * @return string
74
     */
75
    public function __toString()
76
    {
77
        if ($this->ident === null) {
78
            return '';
79
        }
80
        return $this->ident;
81
    }
82
83
    /**
84
     * @param Container $container Pimple DI container.
85
     * @return void
86
     */
87
    public function setDependencies(Container $container)
88
    {
89
        parent::setDependencies($container);
90
        $this->setTranslator($container['translator']);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function key()
97
    {
98
        return 'ident';
99
    }
100
101
    /**
102
     * @param string|Role $parent Role's parent.
103
     * @return Role Chainable
104
     */
105
    public function setParent($parent)
106
    {
107
        $this->parent = (string)$parent;
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function parent()
115
    {
116
        return $this->parent;
117
    }
118
119
    /**
120
     * @param string[]|string|null $allowed The allowed permissions for this role.
121
     * @throws InvalidArgumentException If the passed arguments is not an array, null, or a comma-separated string.
122
     * @return Role Chainable
123
     */
124 View Code Duplication
    public function setAllowed($allowed)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        if ($allowed === null) {
127
            $this->allowed = null;
128
            return $this;
129
        }
130
131
        if (is_string($allowed)) {
132
            $allowed = explode(self::SEPARATOR, $allowed);
133
            $allowed = array_map('trim', $allowed);
134
        }
135
        if (!is_array($allowed)) {
136
            throw new InvalidArgumentException(
137
                'Invalid allowed value. Must be an array, null, or a comma-separated string.'
138
            );
139
        }
140
        $this->allowed = $allowed;
141
        return $this;
142
    }
143
144
    /**
145
     * @return string[]|null
146
     */
147
    public function allowed()
148
    {
149
        return $this->allowed;
150
    }
151
152
    /**
153
     * @param string[]|string|null $denied The denied permissions for this role.
154
     * @throws InvalidArgumentException If the passed arguments is not an array, null, or a comma-separated string.
155
     * @return Role Chainable
156
     */
157 View Code Duplication
    public function setDenied($denied)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        if ($denied === null) {
160
            $this->denied = null;
161
            return $this;
162
        }
163
164
        if (is_string($denied)) {
165
            $denied = explode(self::SEPARATOR, $denied);
166
            $denied = array_walk('trim', $denied);
0 ignored issues
show
Bug introduced by
'trim' cannot be passed to array_walk() as the parameter $array expects a reference.
Loading history...
167
        }
168
        if (!is_array($denied)) {
169
            throw new InvalidArgumentException(
170
                'Invalid denied value. Must be an array, null, or a comma-separated string.'
171
            );
172
        }
173
        $this->denied = $denied;
174
        return $this;
175
    }
176
177
    /**
178
     * @return string[]|null
179
     */
180
    public function denied()
181
    {
182
        return $this->denied;
183
    }
184
185
    /**
186
     * @param boolean $isSuper The superuser flag.
187
     * @return Role Chainable
188
     */
189
    public function setSuperuser($isSuper)
190
    {
191
        $this->superuser = !!$isSuper;
192
        return $this;
193
    }
194
195
    /**
196
     * @return boolean
197
     */
198
    public function superuser()
199
    {
200
        return $this->superuser;
201
    }
202
203
    /**
204
     * @param integer|string|null $position The role's ordering position.
205
     * @return Role Chainable
206
     */
207
    public function setPosition($position)
208
    {
209
        $this->position = (int)$position;
210
        return $this;
211
    }
212
213
    /**
214
     * @return integer
215
     */
216
    public function position()
217
    {
218
        return $this->position;
219
    }
220
}
221