Conditions | 12 |
Paths | 20 |
Total Lines | 48 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public function __construct(UploadedFile $file, array $data, IdentityInterface $identity = null, $onlyUploaded = true) |
||
33 | { |
||
34 | |||
35 | $this->_onlyUploaded = $onlyUploaded; |
||
36 | |||
37 | if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
||
38 | throw new BadRequestHttpException("Attribute or class name not set."); |
||
39 | |||
40 | // Загружаем полученные данные |
||
41 | $this->_instance = $file; |
||
42 | $this->_attribute = $data['attribute']; |
||
43 | |||
44 | if (!file_exists($this->_instance->tempName)) |
||
45 | throw new ErrorException("Tmp file not found on disk."); |
||
46 | |||
47 | // Инициализируем класс владельца файла для валидаций и ставим сценарий |
||
48 | $this->_owner = new $data['modelClass']; |
||
49 | |||
50 | if (isset($data['scenario'])) |
||
51 | $this->_owner->setScenario($data['scenario']); |
||
52 | |||
53 | |||
54 | if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
||
55 | foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
||
56 | if (!$validator->validate($this->_instance, $error)) |
||
|
|||
57 | throw new BadRequestHttpException($error); |
||
58 | } |
||
59 | |||
60 | } |
||
61 | |||
62 | // Создаем модель нового файла и заполняем первоначальными данными |
||
63 | $this->_model = new File(); |
||
64 | $this->_model->created = time(); |
||
65 | $this->_model->field = $this->_attribute; |
||
66 | $this->_model->class = $data['modelClass']; |
||
67 | |||
68 | $this->_model->filename = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.' . $this->_instance->extension; |
||
69 | $this->_model->title = $this->_instance->name; |
||
70 | $this->_model->content_type = $this->_instance->type; |
||
71 | $this->_model->size = $this->_instance->size; |
||
72 | $this->_model->type = $this->detectType(); |
||
73 | if ($identity) |
||
74 | $this->_model->user_id = $identity->id; |
||
75 | if ($this->_model->type == FileType::VIDEO) |
||
76 | $this->_model->video_status = 0; |
||
77 | |||
78 | //Генерируем полный новый адрес сохранения файла |
||
79 | $this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
||
80 | } |
||
129 | } |