| Total Complexity | 49 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Uploader 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 Uploader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Uploader extends Component |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var FileUploadTrait |
||
| 24 | */ |
||
| 25 | public $modelClass; |
||
| 26 | public $data; |
||
| 27 | public $file; |
||
| 28 | public $name; |
||
| 29 | public $slug; |
||
| 30 | public $uid; |
||
| 31 | public $delete = true; |
||
| 32 | public $folder; |
||
| 33 | public $context; |
||
| 34 | protected $fileName; |
||
| 35 | protected $filePath; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $name |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | public function name($name) |
||
| 42 | { |
||
| 43 | $this->name = $name; |
||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param $data |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function data($data) |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $path |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | public function folder($path) |
||
| 65 | } |
||
| 66 | |||
| 67 | public function context($context) |
||
| 68 | { |
||
| 69 | $this->context = $context; |
||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param bool $deleteOnFinish |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function delete($deleteOnFinish = true) |
||
| 78 | { |
||
| 79 | $this->delete = $deleteOnFinish; |
||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return null|string |
||
| 85 | */ |
||
| 86 | protected function getSession() |
||
| 87 | { |
||
| 88 | if (isset(Yii::$app->session)) { |
||
| 89 | $session = Yii::$app->session->getIsActive() ? Yii::$app->session->getId() : null; |
||
|
|
|||
| 90 | } else { |
||
| 91 | $session = null; |
||
| 92 | } |
||
| 93 | return $session; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | public function getFileName() |
||
| 100 | { |
||
| 101 | return $this->name ?: $this->fileName; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | protected function getFileNameWithOutExtension() |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | protected function getMimeType() |
||
| 116 | { |
||
| 117 | return FileHelper::getMimeType($this->filePath); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return mixed|string |
||
| 122 | */ |
||
| 123 | protected function getFileExtension() |
||
| 124 | { |
||
| 125 | if (!$extension = strtolower(pathinfo($this->fileName, PATHINFO_EXTENSION))) { |
||
| 126 | $extension = ArrayHelper::getValue(FileHelper::getExtensionsByMimeType($this->getMimeType()), 0); |
||
| 127 | } |
||
| 128 | return $extension; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return int|null|string |
||
| 133 | */ |
||
| 134 | public function getUserId() |
||
| 135 | { |
||
| 136 | if (isset(Yii::$app->user)) { |
||
| 137 | $userId = Yii::$app->user->getId(); |
||
| 138 | } else { |
||
| 139 | $userId = null; |
||
| 140 | } |
||
| 141 | return $userId; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param $source |
||
| 146 | * @param $destination |
||
| 147 | * @throws \Exception |
||
| 148 | */ |
||
| 149 | protected function copyUploadedFile($source, $destination) |
||
| 150 | { |
||
| 151 | if (!move_uploaded_file($source, $destination)) { |
||
| 152 | throw new \Exception('Unknown upload error'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $source |
||
| 158 | * @param $destination |
||
| 159 | * @throws \Exception |
||
| 160 | */ |
||
| 161 | protected function renameFile($source, $destination) |
||
| 162 | { |
||
| 163 | if (!rename($source, $destination)) { |
||
| 164 | throw new \Exception('Failed rename file'); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $source |
||
| 170 | * @param $destination |
||
| 171 | * @throws \Exception |
||
| 172 | */ |
||
| 173 | protected function copyFile($source, $destination) |
||
| 174 | { |
||
| 175 | if (!copy($source, $destination)) { |
||
| 176 | throw new \Exception('Failed copy file'); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $source |
||
| 182 | * @param $destination |
||
| 183 | * @throws \Exception |
||
| 184 | */ |
||
| 185 | public function copy($source, $destination) |
||
| 186 | { |
||
| 187 | if (is_uploaded_file($source)) { |
||
| 188 | $this->copyUploadedFile($source, $destination); |
||
| 189 | } elseif ($this->delete) { |
||
| 190 | $this->renameFile($source, $destination); |
||
| 191 | } else { |
||
| 192 | $this->copyFile($source, $destination); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getUid() |
||
| 200 | { |
||
| 201 | return $this->uid = $this->uid ?: $this->formUid(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getNewFileAlias() |
||
| 208 | { |
||
| 209 | return static::formAliasPath($this->getUid(), $this->folder); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ActiveRecord $model |
||
| 214 | * @param $attributes |
||
| 215 | */ |
||
| 216 | public function loadAttributes($model, $attributes) |
||
| 217 | { |
||
| 218 | $model->setAttributes($attributes); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function formAttributes() |
||
| 222 | { |
||
| 223 | $this->processFilePath($this->file); |
||
| 224 | return [ |
||
| 225 | 'session' => $this->getSession(), |
||
| 226 | 'name' => $this->getFileNameWithOutExtension(), |
||
| 227 | 'extension' => $this->getFileExtension(), |
||
| 228 | 'user_id' => $this->getUserId(), |
||
| 229 | 'uid' => $this->getUid(), |
||
| 230 | 'data' => !is_null($this->data) ? json_encode($this->data) : null, |
||
| 231 | 'mime_type' => $this->getMimeType(), |
||
| 232 | 'md5' => md5_file($this->filePath), |
||
| 233 | 'folder' => static::formAliasPath($this->getUid(), $this->folder), |
||
| 234 | 'slug' => $this->slug, |
||
| 235 | 'size' => filesize($this->filePath) |
||
| 236 | ]; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return FileUploadTrait|null |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | public function process() |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param $file |
||
| 270 | */ |
||
| 271 | protected function processFilePath($file) |
||
| 272 | { |
||
| 273 | if (strpos($file, 'http') === 0) { |
||
| 274 | $context = $this->context ? stream_context_create($this->context) : null; |
||
| 275 | $tmp = Yii::getAlias('@runtime') . DIRECTORY_SEPARATOR . uniqid('file_upload_', true); |
||
| 276 | file_put_contents($tmp, file_get_contents($file, false, $context)); |
||
| 277 | $this->filePath = $tmp; |
||
| 278 | $this->fileName = explode('?', basename($file))[0]; |
||
| 279 | } elseif (is_string($file)) { |
||
| 280 | $this->filePath = Yii::getAlias($file); |
||
| 281 | $this->fileName = basename($this->filePath); |
||
| 282 | } elseif ($file instanceof UploadedFile) { |
||
| 283 | $this->filePath = $file->tempName; |
||
| 284 | $this->fileName = $file->name; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param $slug |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function slug($slug) |
||
| 293 | { |
||
| 294 | $this->slug = $slug; |
||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function generateUid() |
||
| 302 | { |
||
| 303 | $sec = new Security(); |
||
| 304 | return md5($sec->generateRandomString(64)); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | protected function formUid() |
||
| 311 | { |
||
| 312 | $class = $this->modelClass; |
||
| 313 | do { |
||
| 314 | $uPath = $this->generateUid(); |
||
| 315 | } while ($class::find()->where(['uid' => $uPath])->exists()); |
||
| 316 | return $uPath; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $path |
||
| 321 | * @param null|string $aliasPrefix |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public static function formAliasPath($path, $aliasPrefix = null) |
||
| 331 | } |
||
| 332 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.