| Conditions | 3 |
| Paths | 2 |
| Total Lines | 62 |
| Code Lines | 31 |
| 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 |
||
| 24 | function registerUpload() |
||
| 25 | { |
||
| 26 | $jaxon = jaxon(); |
||
| 27 | $di = $jaxon->di(); |
||
| 28 | if($di->h(UploadHandler::class)) |
||
| 29 | { |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | |||
| 33 | // Upload file and dir name generator |
||
| 34 | $di->set(FileNameInterface::class, function() { |
||
| 35 | return new class implements FileNameInterface |
||
| 36 | { |
||
| 37 | public function random(int $nLength): string |
||
| 38 | { |
||
| 39 | return bin2hex(random_bytes((int)($nLength / 2))); |
||
| 40 | } |
||
| 41 | }; |
||
| 42 | }); |
||
| 43 | // Upload validator |
||
| 44 | $di->set(Validator::class, function($c) { |
||
| 45 | return new Validator($c->g(ConfigManager::class), $c->g(Translator::class)); |
||
| 46 | }); |
||
| 47 | // File storage |
||
| 48 | $di->set(FileStorage::class, function($c) { |
||
| 49 | $xFileStorage = new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class)); |
||
| 50 | $xFileStorage->registerAdapters(); |
||
| 51 | return $xFileStorage; |
||
| 52 | }); |
||
| 53 | // File upload manager |
||
| 54 | $di->set(UploadManager::class, function($c) { |
||
| 55 | // Translation directory |
||
| 56 | $sTranslationDir = realpath(__DIR__ . '/../../translations'); |
||
| 57 | // Load the upload translations |
||
| 58 | $xTranslator = $c->g(Translator::class); |
||
| 59 | $xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
||
| 60 | $xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
||
| 61 | $xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
||
| 62 | |||
| 63 | return new UploadManager($c->g(FileStorage::class), $c->g(FileNameInterface::class), |
||
| 64 | $c->g(ConfigManager::class), $c->g(Validator::class), $xTranslator); |
||
| 65 | }); |
||
| 66 | // File upload plugin |
||
| 67 | $di->set(UploadHandler::class, function($c) { |
||
| 68 | return new UploadHandler($c->g(FileStorage::class), $c->g(UploadManager::class)); |
||
| 69 | }); |
||
| 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($jaxon, $di) { |
||
| 75 | if(!$jaxon->getOption('core.upload.enabled')) |
||
| 76 | { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | /** @var UploadHandler */ |
||
| 80 | $xUploadHandler = $di->g(UploadHandler::class); |
||
| 81 | // The HTTP request |
||
| 82 | $xRequest = $di->getRequest(); |
||
| 83 | if($xUploadHandler->canProcessRequest($xRequest)) |
||
| 84 | { |
||
| 85 | $xUploadHandler->processRequest($xRequest); |
||
| 86 | } |
||
| 135 |