Total Complexity | 54 |
Total Lines | 369 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like ActiveLogBehavior 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 ActiveLogBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
64 | class ActiveLogBehavior extends Behavior |
||
65 | { |
||
66 | /** |
||
67 | * @event MessageEvent an event that is triggered before inserting a record. |
||
68 | * You may add in to the [[MessageEvent::append]] your custom log message. |
||
69 | * @since 1.5.3 |
||
70 | */ |
||
71 | public const EVENT_BEFORE_SAVE_MESSAGE = 'beforeSaveMessage'; |
||
72 | /** |
||
73 | * @event Event an event that is triggered after inserting a record. |
||
74 | * @since 1.5.3 |
||
75 | */ |
||
76 | public const EVENT_AFTER_SAVE_MESSAGE = 'afterSaveMessage'; |
||
77 | |||
78 | public bool $softDelete = false; |
||
79 | /** @since 1.6.0 */ |
||
80 | public ?\Closure $beforeSaveMessage = null; |
||
81 | /** |
||
82 | * [ |
||
83 | * // simple attribute |
||
84 | * 'title', |
||
85 | * |
||
86 | * // simple boolean attribute |
||
87 | * 'is_publish', |
||
88 | * |
||
89 | * // the value of the attribute is a item in the list |
||
90 | * // => $this->getStatusList() |
||
91 | * 'status' => [ |
||
92 | * 'list' => 'statusList' |
||
93 | * ], |
||
94 | * |
||
95 | * // the attribute value is the [id] of the relation model |
||
96 | * 'owner_id' => [ |
||
97 | * 'relation' => 'user', |
||
98 | * 'attribute' => 'username' |
||
99 | * ] |
||
100 | * ] |
||
101 | */ |
||
102 | public array $attributes = []; |
||
103 | |||
104 | public bool $identicalAttributes = false; |
||
105 | /** |
||
106 | * A PHP callable that replaces the default implementation of [[isEmpty()]]. |
||
107 | * @since 1.5.2 |
||
108 | */ |
||
109 | public ?\Closure $isEmpty = null; |
||
110 | /** |
||
111 | * @var \Closure|array|string custom method to getEntityName |
||
112 | * the callback function must return a string |
||
113 | */ |
||
114 | public $getEntityName; |
||
115 | /** |
||
116 | * @var \Closure|array|string custom method to getEntityId |
||
117 | * the callback function can return a string or array |
||
118 | */ |
||
119 | public $getEntityId; |
||
120 | /** |
||
121 | * [ |
||
122 | * 'title' => [ |
||
123 | * 'new' => ['value' => 'New title'], |
||
124 | * ], |
||
125 | * 'is_publish' => [ |
||
126 | * 'old' => ['value' => false], |
||
127 | * 'new' => ['value' => true], |
||
128 | * ], |
||
129 | * 'status' => [ |
||
130 | * 'old' => ['id' => 0, 'value' => 'Disabled'], |
||
131 | * 'new' => ['id' => 1, 'value' => 'Active'], |
||
132 | * ], |
||
133 | * 'owner_id' => [ |
||
134 | * 'old' => ['id' => 1, 'value' => 'admin'], |
||
135 | * 'new' => ['id' => 2, 'value' => 'lucy'], |
||
136 | * ] |
||
137 | * ] |
||
138 | */ |
||
139 | private array $changedAttributes = []; |
||
140 | |||
141 | private string $action; |
||
142 | |||
143 | private ManagerInterface $logger; |
||
144 | |||
145 | 28 | public function __construct( |
|
146 | ManagerInterface $logger, |
||
147 | array $config = [] |
||
148 | ) |
||
149 | { |
||
150 | 28 | $this->logger = $logger; |
|
151 | 28 | parent::__construct($config); |
|
152 | } |
||
153 | |||
154 | 28 | public function init(): void |
|
155 | { |
||
156 | 28 | $this->initAttributes(); |
|
157 | } |
||
158 | |||
159 | 28 | private function initAttributes(): void |
|
160 | { |
||
161 | 28 | foreach ($this->attributes as $key => $value) { |
|
162 | 27 | if (is_int($key)) { |
|
163 | 27 | unset($this->attributes[$key]); |
|
164 | 27 | $this->attributes[$value] = []; |
|
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | 28 | public function events(): array |
|
170 | { |
||
171 | 28 | if (false === $this->logger->isEnabled()) { |
|
172 | 1 | return []; |
|
173 | } |
||
174 | 27 | return [ |
|
175 | 27 | ActiveRecord::EVENT_BEFORE_INSERT => [$this, 'beforeSave'], |
|
176 | 27 | ActiveRecord::EVENT_BEFORE_UPDATE => [$this, 'beforeSave'], |
|
177 | 27 | ActiveRecord::EVENT_BEFORE_DELETE => [$this, 'beforeDelete'], |
|
178 | 27 | ActiveRecord::EVENT_AFTER_INSERT => [$this, 'afterSave'], |
|
179 | 27 | ActiveRecord::EVENT_AFTER_UPDATE => [$this, 'afterSave'], |
|
180 | 27 | ]; |
|
181 | } |
||
182 | |||
183 | 26 | public function beforeSave(): void |
|
187 | } |
||
188 | |||
189 | 24 | public function afterSave(): void |
|
190 | { |
||
191 | 24 | if (empty($this->changedAttributes)) { |
|
192 | 4 | return; |
|
193 | } |
||
194 | 24 | $this->saveMessage($this->action, $this->changedAttributes); |
|
195 | } |
||
196 | |||
197 | 4 | public function beforeDelete(): void |
|
206 | } |
||
207 | |||
208 | 26 | private function prepareChangedAttributes(bool $unset = false): array |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param string|int|null $old_id |
||
228 | * @param string|int|null $new_id |
||
229 | */ |
||
230 | 26 | protected function resolveStoreValues($old_id, $new_id, array $options): array |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string|int|null $old_id |
||
244 | * @param string|int|null $new_id |
||
245 | */ |
||
246 | 22 | private function resolveSimpleValues($old_id, $new_id): array |
|
247 | { |
||
248 | 22 | return [ |
|
249 | 22 | 'old' => ['value' => $old_id], |
|
250 | 22 | 'new' => ['value' => $new_id], |
|
251 | 22 | ]; |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param string|int|array|null $old_id |
||
256 | * @param string|int|array|null $new_id |
||
257 | */ |
||
258 | 24 | private function resolveListValues($old_id, $new_id, string $listName): array |
|
259 | { |
||
260 | 24 | $old = $new = []; |
|
261 | 24 | $old['id'] = $old_id; |
|
262 | 24 | $new['id'] = $new_id; |
|
263 | 24 | $list = []; |
|
264 | |||
265 | 24 | if (is_array($old_id) || is_array($new_id)) { |
|
266 | 1 | $list = ArrayHelper::getValue($this->owner, $listName); |
|
267 | } |
||
268 | 24 | if (is_array($old_id)) { |
|
269 | 1 | $old['value'] = array_intersect_key($list, array_flip($old_id)); |
|
270 | 24 | } elseif ($old_id) { |
|
271 | 4 | $old['value'] = ArrayHelper::getValue($this->owner, [$listName, $old_id]); |
|
272 | } else { |
||
273 | 24 | $old['value'] = null; |
|
274 | } |
||
275 | 24 | if (is_array($new_id)) { |
|
276 | 1 | $new['value'] = array_intersect_key($list, array_flip($new_id)); |
|
277 | 24 | } elseif ($new_id) { |
|
278 | 23 | $new['value'] = ArrayHelper::getValue($this->owner, [$listName, $new_id]); |
|
279 | } else { |
||
280 | 4 | $new['value'] = null; |
|
281 | } |
||
282 | 24 | return [ |
|
283 | 24 | 'old' => $old, |
|
284 | 24 | 'new' => $new |
|
285 | 24 | ]; |
|
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param string|int|null $old_id |
||
290 | * @param string|int|null $new_id |
||
291 | */ |
||
292 | 19 | private function resolveRelationValues($old_id, $new_id, string $relation, string $attribute): array |
|
293 | { |
||
294 | 19 | $old = $new = []; |
|
295 | 19 | $old['id'] = $old_id; |
|
296 | 19 | $new['id'] = $new_id; |
|
297 | |||
298 | /** @var ActiveQueryInterface $relationQuery */ |
||
299 | 19 | $relationQuery = clone $this->owner->getRelation($relation); |
|
300 | 18 | if (count($relationQuery->link) > 1) { |
|
1 ignored issue
–
show
|
|||
301 | 1 | throw new InvalidConfigException('Relation model can only be linked through one primary key.'); |
|
302 | } |
||
303 | 17 | $relationQuery->primaryModel = null; |
|
1 ignored issue
–
show
|
|||
304 | 17 | $idAttribute = array_keys($relationQuery->link)[0]; |
|
305 | 17 | $targetId = array_filter([$old_id, $new_id]); |
|
306 | |||
307 | 17 | $relationModels = $relationQuery |
|
308 | 17 | ->where([$idAttribute => $targetId]) |
|
309 | 17 | ->indexBy($idAttribute) |
|
310 | 17 | ->limit(count($targetId)) |
|
311 | 17 | ->all(); |
|
312 | |||
313 | 17 | if ($old_id) { |
|
314 | 4 | $old['value'] = ArrayHelper::getValue($relationModels, [$old_id, $attribute]); |
|
315 | } else { |
||
316 | 17 | $old['value'] = null; |
|
317 | } |
||
318 | 17 | if ($new_id) { |
|
319 | 17 | $new['value'] = ArrayHelper::getValue($relationModels, [$new_id, $attribute]); |
|
320 | } else { |
||
321 | 2 | $new['value'] = null; |
|
322 | } |
||
323 | |||
324 | 17 | return [ |
|
325 | 17 | 'old' => $old, |
|
326 | 17 | 'new' => $new |
|
327 | 17 | ]; |
|
328 | } |
||
329 | |||
330 | 24 | protected function saveMessage(string $action, array $data): void |
|
331 | { |
||
332 | 24 | $data = $this->beforeSaveMessage($data); |
|
333 | 24 | $this->addLog($data, $action); |
|
334 | 24 | $this->afterSaveMessage(); |
|
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param string|array $data |
||
339 | * @since 1.7.0 |
||
340 | */ |
||
341 | 24 | public function addLog($data, string $action = null): bool |
|
353 | } |
||
354 | |||
355 | /** |
||
356 | * @since 1.5.3 |
||
357 | */ |
||
358 | 24 | public function beforeSaveMessage(array $data): array |
|
359 | { |
||
360 | 24 | if (null !== $this->beforeSaveMessage) { |
|
361 | 1 | return call_user_func($this->beforeSaveMessage, $data); |
|
362 | } |
||
363 | 23 | $name = self::EVENT_BEFORE_SAVE_MESSAGE; |
|
364 | 23 | if (method_exists($this->owner, $name)) { |
|
365 | 1 | return $this->owner->$name($data); |
|
366 | } |
||
367 | 22 | $event = new MessageEvent(); |
|
368 | 22 | $event->logData = $data; |
|
369 | 22 | $this->owner->trigger($name, $event); |
|
370 | 22 | return $event->logData; |
|
371 | } |
||
372 | |||
373 | /** |
||
374 | * @since 1.5.3 |
||
375 | */ |
||
376 | 24 | public function afterSaveMessage(): void |
|
377 | { |
||
378 | 24 | $name = self::EVENT_AFTER_SAVE_MESSAGE; |
|
379 | 24 | if (method_exists($this->owner, $name)) { |
|
380 | 1 | $this->owner->$name(); |
|
381 | } else { |
||
382 | 23 | $this->owner->trigger($name); |
|
383 | } |
||
384 | } |
||
385 | |||
386 | 25 | public function getEntityName(): string |
|
387 | { |
||
388 | 25 | if (is_string($this->getEntityName)) { |
|
389 | 25 | return $this->getEntityName; |
|
390 | } |
||
391 | 1 | if (is_callable($this->getEntityName)) { |
|
392 | 1 | return call_user_func($this->getEntityName); |
|
393 | } |
||
394 | 1 | $class = get_class($this->owner); |
|
395 | 1 | $class = StringHelper::basename($class); |
|
396 | 1 | $this->getEntityName = Inflector::camel2id($class, '_'); |
|
397 | 1 | return $this->getEntityName; |
|
398 | } |
||
399 | |||
400 | 26 | public function getEntityId(): string |
|
401 | { |
||
402 | 26 | if (null === $this->getEntityId) { |
|
403 | 26 | $result = $this->owner->getPrimaryKey(); |
|
404 | 4 | } elseif (is_callable($this->getEntityId)) { |
|
405 | 3 | $result = call_user_func($this->getEntityId); |
|
406 | } else { |
||
407 | 1 | $result = $this->getEntityId; |
|
408 | } |
||
409 | 26 | if ($this->isEmpty($result)) { |
|
410 | 1 | throw new InvalidValueException('the property "entityId" can not be empty'); |
|
411 | } |
||
412 | 25 | if (is_array($result)) { |
|
413 | 1 | ksort($result); |
|
414 | 1 | $result = json_encode($result, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
415 | } |
||
416 | 25 | return $result; |
|
417 | } |
||
418 | |||
419 | /** |
||
420 | * Checks if the given value is empty. |
||
421 | * A value is considered empty if it is null, an empty array, or an empty string. |
||
422 | * Note that this method is different from PHP empty(). It will return false when the value is 0. |
||
423 | * @param mixed $value the value to be checked |
||
424 | * @return bool whether the value is empty |
||
425 | * @since 1.5.2 |
||
426 | */ |
||
427 | 28 | public function isEmpty($value): bool |
|
433 | } |
||
434 | } |