Complex classes like FileBehavior 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class FileBehavior extends Behavior |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | public $attributes = []; |
||
23 | |||
24 | /** |
||
25 | * @var ActiveQuery |
||
26 | */ |
||
27 | private $relation; |
||
28 | |||
29 | /** |
||
30 | * @var FileBind |
||
31 | */ |
||
32 | private $fileBind; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $classPathMap = []; |
||
38 | |||
39 | /** |
||
40 | * @var string name of application component that represents `user` |
||
41 | */ |
||
42 | public $userComponent = 'user'; |
||
43 | |||
44 | /** |
||
45 | * @internal |
||
46 | */ |
||
47 | 31 | public function init() |
|
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | * @internal |
||
59 | */ |
||
60 | 31 | public function events() |
|
61 | { |
||
62 | return [ |
||
63 | 31 | ActiveRecord::EVENT_AFTER_INSERT => 'afterSave', |
|
64 | 31 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', |
|
65 | 31 | ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', |
|
66 | 31 | ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', |
|
67 | 31 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
|
68 | ]; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @internal |
||
73 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
74 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
75 | */ |
||
76 | 2 | public function beforeSave($insert) |
|
|
|||
77 | { |
||
78 | 2 | foreach ($this->attributes as $attribute => $options) { |
|
79 | 2 | $oldValue = $this->owner->isNewRecord ? null : $this->owner->getOldAttribute($attribute); |
|
80 | 2 | $isAttributeChanged = $oldValue === null ? true : $this->owner->isAttributeChanged($attribute); |
|
81 | |||
82 | 2 | $this->attributes[$attribute]['isAttributeChanged'] = $isAttributeChanged; |
|
83 | 2 | $this->attributes[$attribute]['oldValue'] = $oldValue; |
|
84 | } |
||
85 | 2 | } |
|
86 | |||
87 | /** |
||
88 | * @internal |
||
89 | */ |
||
90 | 2 | public function afterSave() |
|
91 | { |
||
92 | 2 | foreach ($this->attributes as $attribute => $options) { |
|
93 | 2 | $files = $this->owner->{$attribute}; |
|
94 | |||
95 | 2 | $isAttributeNotChanged = $options['isAttributeChanged'] === false || $files === null; |
|
96 | 2 | if ($isAttributeNotChanged) { |
|
97 | 2 | continue; |
|
98 | } |
||
99 | |||
100 | 2 | if (is_numeric($files)) { |
|
101 | $files = [$files]; |
||
102 | } |
||
103 | |||
104 | 2 | if (is_array($files)) { |
|
105 | 1 | $files = array_filter($files); |
|
106 | } |
||
107 | |||
108 | 2 | if ($files === [] || $files === '') { |
|
109 | 1 | $this->fileBind->delete($this->owner, $attribute, $this->files($attribute)); |
|
110 | 1 | continue; |
|
111 | } |
||
112 | |||
113 | 1 | $maxFiles = ArrayHelper::getValue($this->fileRules($attribute, true), 'maxFiles'); |
|
114 | 1 | if (is_array($files) && $maxFiles !== null) { |
|
115 | 1 | $files = array_slice($files, 0, $maxFiles, true); |
|
116 | } |
||
117 | |||
118 | 1 | $files = $this->fileBind->bind($this->owner, $attribute, $files); |
|
119 | |||
120 | 1 | $this->clearState($attribute, $files); |
|
121 | |||
122 | 1 | if (is_array($files)) { |
|
123 | $files = array_shift($files); |
||
124 | 1 | $this->setValue($attribute, $files, $options['oldValue']); |
|
125 | } |
||
126 | } |
||
127 | 2 | } |
|
128 | |||
129 | /** |
||
130 | * @internal |
||
131 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
132 | */ |
||
133 | public function beforeDelete() |
||
134 | { |
||
135 | foreach ($this->attributes as $attribute => $options) { |
||
136 | $this->fileBind->delete($this->owner, $attribute, $this->files($attribute)); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | 26 | protected function getUser() |
|
147 | |||
148 | 1 | public function clearState($attribute, $files) |
|
149 | { |
||
150 | 1 | if (!$this->getUser()) { |
|
151 | 1 | return []; |
|
152 | } |
||
153 | if (!is_array($files)) { |
||
154 | $files = [$files]; |
||
155 | } |
||
156 | $query = [ |
||
157 | 'created_user_id' => $this->getUser()->id, |
||
158 | 'target_model_class' => static::getClass(get_class($this->owner)), |
||
159 | 'target_model_id' => $this->owner->getPrimaryKey(), |
||
160 | 'target_model_attribute' => $attribute, |
||
161 | ]; |
||
162 | if ($files) { |
||
163 | $fileIDs = ArrayHelper::getColumn($files, 'id'); |
||
164 | $query['file_id'] = $fileIDs; |
||
165 | } |
||
166 | FileUploadSession::deleteAll($query); |
||
167 | $query['target_model_id'] = null; |
||
168 | FileUploadSession::deleteAll($query); // for cases of uploads when original model was a new record at the moment of uploads |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | 25 | private function setState($attribute, $file) |
|
182 | |||
183 | /** |
||
184 | * for models with single upload only |
||
185 | * @param $attribute |
||
186 | * @param $file |
||
187 | * @param $defaultValue |
||
188 | */ |
||
189 | private function setValue($attribute, $file, $defaultValue) |
||
190 | { |
||
191 | $saveFilePath = $this->fileOption($attribute, 'saveFilePathInAttribute'); |
||
192 | $saveFileId = $this->fileOption($attribute, 'saveFileIdInAttribute'); |
||
193 | |||
194 | if ($saveFilePath || $saveFileId) { |
||
195 | if (!$file) { |
||
196 | $value = $defaultValue; |
||
197 | } elseif ($saveFilePath) { |
||
198 | $handlerTemplatePath = $this->fileOption($attribute, 'templatePath'); |
||
199 | $value = Yii::getAlias($this->fileOption($attribute, 'baseUrl')) . $handlerTemplatePath($file); |
||
200 | } elseif ($saveFileId) { |
||
201 | $value = $file->getPrimaryKey(); |
||
202 | } |
||
203 | $this->owner->{$attribute} = $value; |
||
204 | $this->owner->updateAttributes([$attribute => $value]); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Generate a thumb |
||
210 | * |
||
211 | * @param string $attribute The attribute name |
||
212 | * @param string $preset The preset name |
||
213 | * @param string $path The file path |
||
214 | * @return string The thumb path |
||
215 | */ |
||
216 | private function generateThumb($attribute, $preset, $path) |
||
217 | { |
||
218 | $thumbPath = pathinfo($path, PATHINFO_FILENAME); |
||
219 | $thumbPath = str_replace($thumbPath, $preset . '_' . $thumbPath, $path); |
||
220 | $realPath = $this->fileStorage($attribute)->path; |
||
221 | |||
222 | if (!file_exists($realPath . $thumbPath) && file_exists($realPath . $path)) { |
||
223 | $handlerPreset = $this->fileOption($attribute, 'preset.'.$preset); |
||
224 | $handlerPreset($realPath, $path, $thumbPath); |
||
225 | } |
||
226 | |||
227 | return $thumbPath; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Generate file path by template |
||
232 | * |
||
233 | * @param string $attribute The attribute name |
||
234 | * @param ActiveRecord $file The file model |
||
235 | * @return string The file path |
||
236 | */ |
||
237 | private function templatePath($attribute, $file = null) |
||
238 | { |
||
239 | $value = $this->owner->{$attribute}; |
||
240 | |||
241 | $saveFilePath = $this->fileOption($attribute, 'saveFilePathInAttribute'); |
||
242 | $isFilledPath = $saveFilePath && !empty($value); |
||
243 | |||
244 | $saveFileId = $this->fileOption($attribute, 'saveFileIdInAttribute'); |
||
245 | $isFilledId = $saveFileId && is_numeric($value) && $value; |
||
246 | |||
247 | if (($isFilledPath || $isFilledId) && $file === null) { |
||
248 | $file = $this->file($attribute); |
||
249 | } |
||
250 | |||
251 | if ($file !== null) { |
||
252 | $handlerTemplatePath = $this->fileOption($attribute, 'templatePath'); |
||
253 | return $handlerTemplatePath($file); |
||
254 | } |
||
255 | return $value; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Get relation |
||
260 | * |
||
261 | * @param string $attribute The attribute name |
||
262 | * @return \ActiveQuery |
||
263 | */ |
||
264 | 2 | public function fileRelation($attribute) |
|
265 | { |
||
266 | 2 | if ($this->relation === null) { |
|
267 | 2 | $this->relation = $this->owner->getRelation($this->fileOption($attribute, 'relation')); |
|
268 | } |
||
269 | 2 | return $this->relation; |
|
270 | } |
||
271 | |||
272 | /** |
||
273 | * Get file option |
||
274 | * |
||
275 | * @param string $attribute The attribute name |
||
276 | * @param string $option Option name |
||
277 | * @param mixed $defaultValue Default value |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 30 | public function fileOption($attribute, $option, $defaultValue = null) |
|
284 | |||
285 | /** |
||
286 | * Get file storage |
||
287 | * |
||
288 | * @param string $attribute The attribute name |
||
289 | * @return \Flysystem |
||
290 | */ |
||
291 | 26 | public function fileStorage($attribute) |
|
295 | |||
296 | /** |
||
297 | * Get file path |
||
298 | * |
||
299 | * @param string $attribute The attribute name |
||
300 | * @param ActiveRecord $file Use this file model |
||
301 | * @return string The file path |
||
302 | */ |
||
303 | public function filePath($attribute, $file = null) |
||
308 | |||
309 | /** |
||
310 | * Get file url |
||
311 | * |
||
312 | * @param string $attribute The attribute name |
||
313 | * @param ActiveRecord $file Use this file model |
||
314 | * @return string The file url |
||
315 | */ |
||
316 | public function fileUrl($attribute, $file = null) |
||
321 | |||
322 | /** |
||
323 | * Get extra fields of file |
||
324 | * |
||
325 | * @param string $attribute The attribute name |
||
326 | * @return array |
||
327 | */ |
||
328 | public function fileExtraFields($attribute) |
||
336 | |||
337 | /** |
||
338 | * Get files |
||
339 | * |
||
340 | * @param string $attribute The attribute name |
||
341 | * @return \ActiveRecord[] The file models |
||
342 | */ |
||
343 | 2 | public function files($attribute) |
|
347 | |||
348 | /** |
||
349 | * Get the file |
||
350 | * |
||
351 | * @param string $attribute The attribute name |
||
352 | * @return \ActiveRecord The file model |
||
353 | */ |
||
354 | 1 | public function file($attribute) |
|
358 | |||
359 | /** |
||
360 | * Get rules |
||
361 | * |
||
362 | * @param string $attribute The attribute name |
||
363 | * @param bool $onlyCoreValidators Only core validators |
||
364 | * @return array |
||
365 | */ |
||
366 | 29 | public function fileRules($attribute, $onlyCoreValidators = false) |
|
367 | { |
||
368 | 29 | $rules = $this->fileOption($attribute, 'rules', []); |
|
369 | 29 | if ($onlyCoreValidators && isset($rules['imageSize'])) { |
|
370 | 28 | $rules = array_merge($rules, $rules['imageSize']); |
|
371 | 28 | unset($rules['imageSize']); |
|
372 | } |
||
373 | 29 | return $rules; |
|
374 | } |
||
375 | |||
376 | /** |
||
377 | * Get file state |
||
378 | * |
||
379 | * @param string $attribute The attribute name |
||
380 | * @return array |
||
381 | */ |
||
382 | public function fileState($attribute) |
||
383 | { |
||
384 | if (!$this->getUser()) { |
||
385 | return []; |
||
386 | } |
||
387 | $query = FileUploadSession::find()->where([ |
||
388 | 'created_user_id' => $this->getUser()->id, |
||
389 | 'target_model_class' => static::getClass(get_class($this->owner)), |
||
390 | 'target_model_attribute' => $attribute, |
||
391 | ]); |
||
392 | $query->andWhere(['or', |
||
393 | ['target_model_id' => $this->owner->getPrimaryKey()], |
||
394 | ['target_model_id' => null] // for cases of uploads when original model was a new record at the moment of uploads |
||
395 | ]); |
||
396 | $data = $query->all(); |
||
397 | if ($data) { |
||
398 | return ArrayHelper::getColumn($data, ['file_id']); |
||
399 | } else { |
||
400 | return []; |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Get the presets of the file for apply after upload |
||
406 | * |
||
407 | * @param string $attribute The attribute name |
||
408 | * @return array |
||
409 | */ |
||
410 | public function filePresetAfterUpload($attribute) |
||
418 | |||
419 | /** |
||
420 | * Create a thumb and return url |
||
421 | * |
||
422 | * @param string $attribute The attribute name |
||
423 | * @param string $preset The preset name |
||
424 | * @param ActiveRecord $file Use this file model |
||
425 | * @return string The file url |
||
426 | */ |
||
427 | public function thumbUrl($attribute, $preset, $file = null) |
||
434 | |||
435 | /** |
||
436 | * Create a thumb and return full path |
||
437 | * |
||
438 | * @param string $attribute The attribute name |
||
439 | * @param string $preset The preset name |
||
440 | * @param ActiveRecord $file Use this file model |
||
441 | * @return string The file path |
||
442 | */ |
||
443 | public function thumbPath($attribute, $preset, $file = null) |
||
450 | |||
451 | /** |
||
452 | * Create a file |
||
453 | * |
||
454 | * @param string $attribute The attribute name |
||
455 | * @param string $path The file path |
||
456 | * @param string $name The file name |
||
457 | * @return \ActiveRecord The file model |
||
458 | */ |
||
459 | 25 | public function createFile($attribute, $path, $name) |
|
475 | |||
476 | /** |
||
477 | * Create a file from remote URL |
||
478 | * |
||
479 | * @author Sergii Gamaiunov <[email protected]> |
||
480 | * |
||
481 | * @param string $attribute The attribute name |
||
482 | * @param \igogo5yo\uploadfromurl\UploadFromUrl $remoteFile |
||
483 | * @param string $name The file name |
||
484 | * @return \ActiveRecord The file model |
||
485 | */ |
||
486 | public function createRemoteFile($attribute, $remoteFile, $name) |
||
487 | { |
||
488 | $url = $remoteFile->url; |
||
489 | $handlerCreateFile = $this->fileOption($attribute, 'createRemoteFile'); |
||
490 | $file = $handlerCreateFile($remoteFile, $name); |
||
491 | if ($file) { |
||
492 | $storage = $this->fileStorage($attribute); |
||
493 | $stream = fopen($url, 'r'); |
||
494 | $handlerTemplatePath = $this->fileOption($attribute, 'templatePath'); |
||
495 | if ($storage->putStream($handlerTemplatePath($file), $stream)) { |
||
496 | if (is_resource($stream)) { // some adapters close resources on their own |
||
497 | fclose($stream); |
||
498 | } |
||
499 | if ($this->getUser()) { |
||
500 | $this->setState($attribute, $file); |
||
501 | } |
||
502 | $this->owner->{$attribute} = $file->id; |
||
503 | return $file; |
||
504 | } |
||
505 | } // @codeCoverageIgnore |
||
506 | return false; // @codeCoverageIgnore |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Add class alias to be able to upload files for different versions of a model to a single API endpoint |
||
511 | * |
||
512 | * Example: |
||
513 | * ``` |
||
514 | * class OldCar extends Car |
||
515 | * { |
||
516 | * public function init() |
||
517 | * { |
||
518 | * parent::init(); |
||
519 | * $this->car_type = 'old; |
||
520 | * FileBehavior::addClassAlias(get_class($this), Car::className()); |
||
521 | * } |
||
522 | * |
||
523 | * public function formName() { |
||
524 | * return 'Car'; |
||
525 | * } |
||
526 | * } |
||
527 | * ``` |
||
528 | * @param $source |
||
529 | * @param $mapTo |
||
530 | */ |
||
531 | public static function addClassAlias($source, $mapTo) { |
||
534 | |||
535 | protected static function getClass($source) { |
||
540 | } |
||
541 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.