| Conditions | 2 |
| Paths | 3 |
| Total Lines | 59 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 23 | function registerUpload(): void |
||
| 24 | { |
||
| 25 | $jaxon = jaxon(); |
||
| 26 | if(!$jaxon->getAppOption('upload.enabled')) |
||
| 27 | { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | $di = $jaxon->di(); |
||
| 32 | if($di->h(UploadHandler::class)) |
||
| 33 | { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | // Upload file and dir name generator |
||
| 38 | $di->set(FileNameInterface::class, function() { |
||
| 39 | return new class implements FileNameInterface |
||
| 40 | { |
||
| 41 | public function random(int $nLength): string |
||
| 42 | { |
||
| 43 | return bin2hex(random_bytes((int)($nLength / 2))); |
||
| 44 | } |
||
| 45 | }; |
||
| 46 | }); |
||
| 47 | |||
| 48 | // Upload validator |
||
| 49 | $di->set(Validator::class, fn($c) => |
||
| 50 | new Validator($c->g(ConfigManager::class), $c->g(Translator::class))); |
||
| 51 | |||
| 52 | // File upload manager |
||
| 53 | $di->set(UploadManager::class, function($c) { |
||
| 54 | // Translation directory |
||
| 55 | $sTranslationDir = dirname(__DIR__) . '/translations'; |
||
| 56 | // Load the upload translations |
||
| 57 | $xTranslator = $c->g(Translator::class); |
||
| 58 | $xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
||
| 59 | $xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
||
| 60 | $xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
||
| 61 | |||
| 62 | return new UploadManager($c->g(Validator::class), $xTranslator, |
||
| 63 | $c->g(LoggerInterface::class), $c->g(FileNameInterface::class), |
||
| 64 | $c->g(StorageManager::class), $c->g(ConfigManager::class)); |
||
| 65 | }); |
||
| 66 | |||
| 67 | // File upload plugin |
||
| 68 | $di->set(UploadHandler::class, fn($c) => |
||
| 69 | new UploadHandler($c->g(StorageManager::class), $c->g(UploadManager::class))); |
||
| 70 | // Set alias on the interface |
||
| 71 | $di->alias(UploadHandlerInterface::class, UploadHandler::class); |
||
| 72 | |||
| 73 | // Set a callback to process uploaded files in the incoming requests. |
||
| 74 | $jaxon->callback()->before(function() use($di) { |
||
| 75 | /** @var UploadHandler */ |
||
| 76 | $xUploadHandler = $di->g(UploadHandler::class); |
||
| 77 | // The HTTP request |
||
| 78 | $xRequest = $di->getRequest(); |
||
| 79 | if($xUploadHandler->canProcessRequest($xRequest)) |
||
| 80 | { |
||
| 81 | $xUploadHandler->processRequest($xRequest); |
||
| 82 | } |
||
| 107 |