| Conditions | 16 |
| Paths | 36 |
| Total Lines | 68 |
| Code Lines | 42 |
| 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 |
||
| 31 | #[Route( |
||
| 32 | '/{slug}/logos', |
||
| 33 | name: 'theme_logos_upload', |
||
| 34 | methods: ['POST'], |
||
| 35 | priority: 10 |
||
| 36 | )] |
||
| 37 | #[IsGranted('ROLE_ADMIN')] |
||
| 38 | public function uploadLogos( |
||
| 39 | string $slug, |
||
| 40 | Request $request, |
||
| 41 | #[Autowire(service: 'oneup_flysystem.themes_filesystem')] FilesystemOperator $fs |
||
| 42 | ): JsonResponse { |
||
| 43 | $map = [ |
||
| 44 | 'header_svg' => 'images/header-logo.svg', |
||
| 45 | 'header_png' => 'images/header-logo.png', |
||
| 46 | 'email_svg' => 'images/email-logo.svg', |
||
| 47 | 'email_png' => 'images/email-logo.png', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | if (!$fs->directoryExists($slug)) { |
||
| 51 | $fs->createDirectory($slug); |
||
| 52 | } |
||
| 53 | if (!$fs->directoryExists("$slug/images")) { |
||
| 54 | $fs->createDirectory("$slug/images"); |
||
| 55 | } |
||
| 56 | |||
| 57 | $results = []; |
||
| 58 | |||
| 59 | foreach ($map as $field => $relativePath) { |
||
| 60 | $file = $request->files->get($field); |
||
| 61 | if (!$file) { $results[$field] = 'skipped'; continue; } |
||
| 62 | |||
| 63 | $ext = strtolower((string) $file->getClientOriginalExtension()); |
||
| 64 | $mime = (string) $file->getMimeType(); |
||
| 65 | |||
| 66 | // SVG |
||
| 67 | if (str_ends_with($field, '_svg')) { |
||
| 68 | if ($mime !== 'image/svg+xml' && $ext !== 'svg') { |
||
| 69 | $results[$field] = 'invalid_mime'; continue; |
||
| 70 | } |
||
| 71 | $content = @file_get_contents($file->getPathname()) ?: ''; |
||
| 72 | $content = $this->sanitizeSvg($content); |
||
| 73 | $this->ensureDir($fs, $slug.'/images'); |
||
| 74 | $fs->write($slug.'/'.$relativePath, $content); |
||
| 75 | $results[$field] = 'uploaded'; continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | // PNG |
||
| 79 | if ($mime !== 'image/png' && $ext !== 'png') { |
||
| 80 | $results[$field] = 'invalid_mime'; continue; |
||
| 81 | } |
||
| 82 | $info = @getimagesize($file->getPathname()); |
||
| 83 | if (!$info) { $results[$field] = 'invalid_image'; continue; } |
||
| 84 | [$w, $h] = $info; |
||
| 85 | |||
| 86 | if ($field === 'header_png' && ($w > 190 || $h > 60)) { |
||
| 87 | $results[$field] = 'invalid_dimensions_header_png'; continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->ensureDir($fs, $slug.'/images'); |
||
| 91 | $stream = fopen($file->getPathname(), 'rb'); |
||
| 92 | $fs->writeStream($slug.'/'.$relativePath, $stream); |
||
| 93 | if (is_resource($stream)) { fclose($stream); } |
||
| 94 | |||
| 95 | $results[$field] = 'uploaded'; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $this->json(['status' => 'ok', 'results' => $results], Response::HTTP_CREATED); |
||
| 99 | } |
||
| 223 |