| 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 |
||
| 33 | public function __construct(UploadedFile $file, array $data, IdentityInterface $identity = null, $onlyUploaded = true) |
||
| 34 | { |
||
| 35 | |||
| 36 | $this->_onlyUploaded = $onlyUploaded; |
||
| 37 | |||
| 38 | if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
||
| 39 | throw new BadRequestHttpException("Attribute or class name not set."); |
||
| 40 | |||
| 41 | // Загружаем полученные данные |
||
| 42 | $this->_instance = $file; |
||
| 43 | $this->_attribute = $data['attribute']; |
||
| 44 | |||
| 45 | if (!file_exists($this->_instance->tempName)) |
||
| 46 | throw new ErrorException("Tmp file not found on disk."); |
||
| 47 | |||
| 48 | // Инициализируем класс владельца файла для валидаций и ставим сценарий |
||
| 49 | $this->_owner = new $data['modelClass']; |
||
| 50 | |||
| 51 | if (isset($data['scenario'])) |
||
| 52 | $this->_owner->setScenario($data['scenario']); |
||
| 53 | |||
| 54 | |||
| 55 | if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
||
| 56 | foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
||
| 57 | if (!$validator->validate($this->_instance, $error)) |
||
|
|
|||
| 58 | throw new BadRequestHttpException($error); |
||
| 59 | } |
||
| 60 | |||
| 61 | } |
||
| 62 | |||
| 63 | // Создаем модель нового файла и заполняем первоначальными данными |
||
| 64 | $this->_model = new File(); |
||
| 65 | $this->_model->created = time(); |
||
| 66 | $this->_model->field = $this->_attribute; |
||
| 67 | $this->_model->class = $data['modelClass']; |
||
| 68 | |||
| 69 | $this->_model->filename = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.' . $this->_instance->extension; |
||
| 70 | $this->_model->title = $this->_instance->name; |
||
| 71 | $this->_model->content_type = $this->_instance->type; |
||
| 72 | $this->_model->size = $this->_instance->size; |
||
| 73 | $this->_model->type = $this->detectType(); |
||
| 74 | if ($identity) |
||
| 75 | $this->_model->user_id = $identity->id; |
||
| 76 | if ($this->_model->type == FileType::VIDEO) |
||
| 77 | $this->_model->video_status = 0; |
||
| 78 | |||
| 79 | //Генерируем полный новый адрес сохранения файла |
||
| 80 | $this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
||
| 81 | } |
||
| 154 |