| Total Complexity | 40 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UploadManager 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 UploadManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class UploadManager |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var Config |
||
| 49 | */ |
||
| 50 | protected $xConfig; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The request data validator |
||
| 54 | * |
||
| 55 | * @var Validator |
||
| 56 | */ |
||
| 57 | protected $xValidator; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Translator |
||
| 61 | */ |
||
| 62 | protected $xTranslator; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A user defined function to transform uploaded file names |
||
| 66 | * |
||
| 67 | * @var Closure |
||
| 68 | */ |
||
| 69 | protected $cNameSanitizer = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The subdir where uploaded files are stored |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $sUploadSubdir = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The constructor |
||
| 80 | * |
||
| 81 | * @param Config $xConfig |
||
| 82 | * @param Validator $xValidator |
||
| 83 | * @param Translator $xTranslator |
||
| 84 | */ |
||
| 85 | public function __construct(Config $xConfig, Validator $xValidator, Translator $xTranslator) |
||
| 86 | { |
||
| 87 | $this->xConfig = $xConfig; |
||
| 88 | $this->xValidator = $xValidator; |
||
| 89 | $this->xTranslator = $xTranslator; |
||
| 90 | $this->sUploadSubdir = $this->randomName() . DIRECTORY_SEPARATOR; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Generate a random name |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | protected function randomName(): string |
||
| 99 | { |
||
| 100 | try |
||
| 101 | { |
||
| 102 | return bin2hex(random_bytes(7)); |
||
| 103 | } |
||
| 104 | catch(Exception $e){} |
||
| 105 | // Generate the name |
||
| 106 | $sChars = '0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz'; |
||
| 107 | return substr(str_shuffle($sChars), 0, 14); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Filter uploaded file name |
||
| 112 | * |
||
| 113 | * @param Closure $cNameSanitizer The closure which filters filenames |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function setNameSanitizer(Closure $cNameSanitizer) |
||
| 118 | { |
||
| 119 | $this->cNameSanitizer = $cNameSanitizer; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Check uploaded files |
||
| 124 | * |
||
| 125 | * @param array $aFiles The uploaded files |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | * @throws RequestException |
||
| 129 | */ |
||
| 130 | private function checkFiles(array $aFiles) |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get a file from upload entry |
||
| 152 | * |
||
| 153 | * @param string $sVarName The corresponding variable |
||
| 154 | * @param array $aVarFiles An entry in the PHP $_FILES array |
||
| 155 | * @param integer $nPosition The position of the file to be processed |
||
| 156 | * |
||
| 157 | * @return null|array |
||
| 158 | */ |
||
| 159 | private function getUploadedFile(string $sVarName, array $aVarFiles, int $nPosition): ?array |
||
| 160 | { |
||
| 161 | if(!$aVarFiles['name'][$nPosition]) |
||
| 162 | { |
||
| 163 | return null; |
||
| 164 | } |
||
| 165 | |||
| 166 | // Filename without the extension |
||
| 167 | $sFilename = pathinfo($aVarFiles['name'][$nPosition], PATHINFO_FILENAME); |
||
| 168 | if(($this->cNameSanitizer)) |
||
| 169 | { |
||
| 170 | $sFilename = (string)call_user_func_array($this->cNameSanitizer, [$sFilename, $sVarName]); |
||
| 171 | } |
||
| 172 | |||
| 173 | return [ |
||
| 174 | 'name' => $aVarFiles['name'][$nPosition], |
||
| 175 | 'type' => $aVarFiles['type'][$nPosition], |
||
| 176 | 'tmp_name' => $aVarFiles['tmp_name'][$nPosition], |
||
| 177 | 'error' => $aVarFiles['error'][$nPosition], |
||
| 178 | 'size' => $aVarFiles['size'][$nPosition], |
||
| 179 | 'filename' => $sFilename, |
||
| 180 | 'extension' => pathinfo($aVarFiles['name'][$nPosition], PATHINFO_EXTENSION), |
||
| 181 | ]; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Read uploaded files info from HTTP request data |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | * @throws RequestException |
||
| 189 | */ |
||
| 190 | public function getUploadedFiles(): array |
||
| 191 | { |
||
| 192 | // Check validity of the uploaded files |
||
| 193 | $aUploadedFiles = []; |
||
| 194 | foreach($_FILES as $sVarName => $aVarFiles) |
||
| 195 | { |
||
| 196 | // If there is only one file, transform each entry into an array, |
||
| 197 | // so the same processing for multiple files can be applied. |
||
| 198 | if(!is_array($aVarFiles['name'])) |
||
| 199 | { |
||
| 200 | $aVarFiles['name'] = [$aVarFiles['name']]; |
||
| 201 | $aVarFiles['type'] = [$aVarFiles['type']]; |
||
| 202 | $aVarFiles['tmp_name'] = [$aVarFiles['tmp_name']]; |
||
| 203 | $aVarFiles['error'] = [$aVarFiles['error']]; |
||
| 204 | $aVarFiles['size'] = [$aVarFiles['size']]; |
||
| 205 | } |
||
| 206 | |||
| 207 | $nFileCount = count($aVarFiles['name']); |
||
| 208 | for($i = 0; $i < $nFileCount; $i++) |
||
| 209 | { |
||
| 210 | $aUploadedFile = $this->getUploadedFile($sVarName, $aVarFiles, $i); |
||
| 211 | if(is_array($aUploadedFile)) |
||
| 212 | { |
||
| 213 | if(!isset($aUploadedFiles[$sVarName])) |
||
| 214 | { |
||
| 215 | $aUploadedFiles[$sVarName] = []; |
||
| 216 | } |
||
| 217 | $aUploadedFiles[$sVarName][] = $aUploadedFile; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | // Check uploaded files validity |
||
| 223 | $this->checkFiles($aUploadedFiles); |
||
| 224 | |||
| 225 | return $aUploadedFiles; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Make sure the upload dir exists and is writable |
||
| 230 | * |
||
| 231 | * @param string $sUploadDir The filename |
||
| 232 | * @param string $sUploadSubDir The filename |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | * @throws RequestException |
||
| 236 | */ |
||
| 237 | private function _makeUploadDir(string $sUploadDir, string $sUploadSubDir): string |
||
| 238 | { |
||
| 239 | $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR; |
||
| 240 | // Verify that the upload dir exists and is writable |
||
| 241 | if(!is_writable($sUploadDir)) |
||
| 242 | { |
||
| 243 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
||
| 244 | } |
||
| 245 | $sUploadDir .= $sUploadSubDir; |
||
| 246 | if(!file_exists($sUploadDir) && !@mkdir($sUploadDir)) |
||
| 247 | { |
||
| 248 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
||
| 249 | } |
||
| 250 | return $sUploadDir; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the path to the upload dir |
||
| 255 | * |
||
| 256 | * @param string $sFieldId The filename |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | * @throws RequestException |
||
| 260 | */ |
||
| 261 | protected function getUploadDir(string $sFieldId): string |
||
| 262 | { |
||
| 263 | // Default upload dir |
||
| 264 | $sDefaultUploadDir = $this->xConfig->getOption('upload.default.dir'); |
||
| 265 | $sUploadDir = $this->xConfig->getOption('upload.files.' . $sFieldId . '.dir', $sDefaultUploadDir); |
||
| 266 | if(!is_string($sUploadDir) || !is_dir($sUploadDir)) |
||
| 267 | { |
||
| 268 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
||
| 269 | } |
||
| 270 | return $this->_makeUploadDir($sUploadDir, $this->sUploadSubdir); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the path to the upload temp dir |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | * @throws RequestException |
||
| 278 | */ |
||
| 279 | protected function getUploadTempDir(): string |
||
| 280 | { |
||
| 281 | // Default upload dir |
||
| 282 | $sUploadDir = $this->xConfig->getOption('upload.default.dir'); |
||
| 283 | if(!is_string($sUploadDir) || !is_dir($sUploadDir)) |
||
| 284 | { |
||
| 285 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
||
| 286 | } |
||
| 287 | return $this->_makeUploadDir($sUploadDir, 'tmp' . DIRECTORY_SEPARATOR); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the path to the upload temp file |
||
| 292 | * |
||
| 293 | * @param string $sTempFile |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | * @throws RequestException |
||
| 297 | */ |
||
| 298 | protected function getUploadTempFile(string $sTempFile): string |
||
| 299 | { |
||
| 300 | // Verify file name validity |
||
| 301 | if(!$this->xValidator->validateTempFileName($sTempFile)) |
||
| 302 | { |
||
| 303 | throw new RequestException($this->xTranslator->trans('errors.upload.invalid')); |
||
| 304 | } |
||
| 305 | $sUploadDir = $this->xConfig->getOption('upload.default.dir', ''); |
||
| 306 | $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR; |
||
| 307 | $sUploadDir .= 'tmp' . DIRECTORY_SEPARATOR; |
||
| 308 | $sUploadTempFile = $sUploadDir . $sTempFile . '.json'; |
||
| 309 | if(!is_readable($sUploadTempFile)) |
||
| 310 | { |
||
| 311 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
||
| 312 | } |
||
| 313 | return $sUploadTempFile; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Read uploaded files info from HTTP request data |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | * @throws RequestException |
||
| 321 | */ |
||
| 322 | public function readFromHttpData(): array |
||
| 323 | { |
||
| 324 | // Check validity of the uploaded files |
||
| 325 | $aTempFiles = $this->getUploadedFiles(); |
||
| 326 | |||
| 327 | // Copy the uploaded files from the temp dir to the user dir |
||
| 328 | $aUserFiles = []; |
||
| 329 | foreach($aTempFiles as $sVarName => $aFiles) |
||
| 330 | { |
||
| 331 | $aUserFiles[$sVarName] = []; |
||
| 332 | // Get the path to the upload dir |
||
| 333 | $sUploadDir = $this->getUploadDir($sVarName); |
||
| 334 | |||
| 335 | foreach($aFiles as $aFile) |
||
| 336 | { |
||
| 337 | // Set the user file data |
||
| 338 | $xUploadedFile = File::fromHttpData($sUploadDir, $aFile); |
||
| 339 | // All's right, move the file to the user dir. |
||
| 340 | move_uploaded_file($aFile["tmp_name"], $xUploadedFile->path()); |
||
| 341 | $aUserFiles[$sVarName][] = $xUploadedFile; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | return $aUserFiles; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Save uploaded files info to a temp file |
||
| 349 | * |
||
| 350 | * @param array $aUserFiles |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | * @throws RequestException |
||
| 354 | */ |
||
| 355 | public function saveToTempFile(array $aUserFiles): string |
||
| 356 | { |
||
| 357 | // Convert uploaded file to an array |
||
| 358 | $aFiles = []; |
||
| 359 | foreach($aUserFiles as $sVarName => $aVarFiles) |
||
| 360 | { |
||
| 361 | $aFiles[$sVarName] = []; |
||
| 362 | foreach($aVarFiles as $aVarFile) |
||
| 363 | { |
||
| 364 | $aFiles[$sVarName][] = $aVarFile->toTempData(); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | // Save upload data in a temp file |
||
| 368 | $sUploadDir = $this->getUploadTempDir(); |
||
| 369 | $sTempFile = $this->randomName(); |
||
| 370 | file_put_contents($sUploadDir . $sTempFile . '.json', json_encode($aFiles)); |
||
| 371 | return $sTempFile; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Read uploaded files info from a temp file |
||
| 376 | * |
||
| 377 | * @param string $sTempFile |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | * @throws RequestException |
||
| 381 | */ |
||
| 382 | public function readFromTempFile(string $sTempFile): array |
||
| 398 | } |
||
| 399 | } |
||
| 400 |