Total Complexity | 48 |
Total Lines | 270 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AuthItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class AuthItem extends Model |
||
27 | { |
||
28 | public $name; |
||
29 | public $type; |
||
30 | public $description; |
||
31 | public $ruleName; |
||
32 | public $data; |
||
33 | /** |
||
34 | * @var Item |
||
35 | */ |
||
36 | private $_item; |
||
37 | |||
38 | /** |
||
39 | * Initialize object |
||
40 | * @param Item $item |
||
41 | * @param array $config |
||
42 | */ |
||
43 | public function __construct($item = null, $config = []) |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | public function rules() |
||
60 | { |
||
61 | return [ |
||
62 | [['ruleName'], 'checkRule'], |
||
63 | [['name', 'type'], 'required'], |
||
64 | [['name'], 'checkUnique', 'when' => function () { |
||
65 | return $this->isNewRecord || ($this->_item->name != $this->name); |
||
|
|||
66 | }], |
||
67 | [['type'], 'integer'], |
||
68 | [['description', 'data', 'ruleName'], 'default'], |
||
69 | [['name'], 'string', 'max' => 64], |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Check role is unique |
||
75 | */ |
||
76 | public function checkUnique() |
||
77 | { |
||
78 | $authManager = Configs::authManager(); |
||
79 | $value = $this->name; |
||
80 | if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) { |
||
81 | $message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); |
||
82 | $params = [ |
||
83 | 'attribute' => $this->getAttributeLabel('name'), |
||
84 | 'value' => $value, |
||
85 | ]; |
||
86 | $this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Check for rule |
||
92 | */ |
||
93 | public function checkRule() |
||
94 | { |
||
95 | $name = $this->ruleName; |
||
96 | if (!Configs::authManager()->getRule($name)) { |
||
97 | try { |
||
98 | $rule = Yii::createObject($name); |
||
99 | if ($rule instanceof \yii\rbac\Rule) { |
||
100 | $rule->name = $name; |
||
101 | Configs::authManager()->add($rule); |
||
102 | } else { |
||
103 | $this->addError('ruleName', Yii::t('rbac-admin', 'Invalid rule "{value}"', ['value' => $name])); |
||
104 | } |
||
105 | } catch (\Exception $exc) { |
||
106 | $this->addError('ruleName', Yii::t('rbac-admin', 'Rule "{value}" does not exists', ['value' => $name])); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | public function attributeLabels() |
||
115 | { |
||
116 | return [ |
||
117 | 'name' => Yii::t('rbac-admin', 'Name'), |
||
118 | 'type' => Yii::t('rbac-admin', 'Type'), |
||
119 | 'description' => Yii::t('rbac-admin', 'Description'), |
||
120 | 'ruleName' => Yii::t('rbac-admin', 'Rule Name'), |
||
121 | 'data' => Yii::t('rbac-admin', 'Data'), |
||
122 | ]; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Check if is new record. |
||
127 | * @return boolean |
||
128 | */ |
||
129 | public function getIsNewRecord() |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Find role |
||
136 | * @param string $id |
||
137 | * @return null|\self |
||
138 | */ |
||
139 | public static function find($id) |
||
140 | { |
||
141 | $item = Configs::authManager()->getRole($id); |
||
142 | if ($item !== null) { |
||
143 | return new self($item); |
||
144 | } |
||
145 | |||
146 | return null; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Save role to [[\yii\rbac\authManager]] |
||
151 | * @return boolean |
||
152 | */ |
||
153 | public function save() |
||
154 | { |
||
155 | if ($this->validate()) { |
||
156 | $manager = Configs::authManager(); |
||
157 | if ($this->_item === null) { |
||
158 | if ($this->type == Item::TYPE_ROLE) { |
||
159 | $this->_item = $manager->createRole($this->name); |
||
160 | } else { |
||
161 | $this->_item = $manager->createPermission($this->name); |
||
162 | } |
||
163 | $isNew = true; |
||
164 | } else { |
||
165 | $isNew = false; |
||
166 | $oldName = $this->_item->name; |
||
167 | } |
||
168 | $this->_item->name = $this->name; |
||
169 | $this->_item->description = $this->description; |
||
170 | $this->_item->ruleName = $this->ruleName; |
||
171 | $this->_item->data = $this->data === null || $this->data === '' ? null : Json::decode($this->data); |
||
172 | if ($isNew) { |
||
173 | $manager->add($this->_item); |
||
174 | } else { |
||
175 | $manager->update($oldName, $this->_item); |
||
176 | } |
||
177 | Helper::invalidate(); |
||
178 | return true; |
||
179 | } else { |
||
180 | return false; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Adds an item as a child of another item. |
||
186 | * @param array $items |
||
187 | * @return int |
||
188 | */ |
||
189 | public function addChildren($items) |
||
190 | { |
||
191 | $manager = Configs::authManager(); |
||
192 | $success = 0; |
||
193 | if ($this->_item) { |
||
194 | foreach ($items as $name) { |
||
195 | $child = $manager->getPermission($name); |
||
196 | if ($this->type == Item::TYPE_ROLE && $child === null) { |
||
197 | $child = $manager->getRole($name); |
||
198 | } |
||
199 | try { |
||
200 | $manager->addChild($this->_item, $child); |
||
201 | $success++; |
||
202 | } catch (\Exception $exc) { |
||
203 | Yii::error($exc->getMessage(), __METHOD__); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | if ($success > 0) { |
||
208 | Helper::invalidate(); |
||
209 | } |
||
210 | return $success; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Remove an item as a child of another item. |
||
215 | * @param array $items |
||
216 | * @return int |
||
217 | */ |
||
218 | public function removeChildren($items) |
||
219 | { |
||
220 | $manager = Configs::authManager(); |
||
221 | $success = 0; |
||
222 | if ($this->_item !== null) { |
||
223 | foreach ($items as $name) { |
||
224 | $child = $manager->getPermission($name); |
||
225 | if ($this->type == Item::TYPE_ROLE && $child === null) { |
||
226 | $child = $manager->getRole($name); |
||
227 | } |
||
228 | try { |
||
229 | $manager->removeChild($this->_item, $child); |
||
230 | $success++; |
||
231 | } catch (\Exception $exc) { |
||
232 | Yii::error($exc->getMessage(), __METHOD__); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | if ($success > 0) { |
||
237 | Helper::invalidate(); |
||
238 | } |
||
239 | return $success; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get items |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getItems() |
||
268 | ]; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Get item |
||
273 | * @return Item |
||
274 | */ |
||
275 | public function getItem() |
||
276 | { |
||
277 | return $this->_item; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get type name |
||
282 | * @param mixed $type |
||
283 | * @return string|array |
||
284 | */ |
||
285 | public static function getTypeName($type = null) |
||
296 | } |
||
297 | } |
||
298 |