| Conditions | 9 |
| Paths | 13 |
| Total Lines | 68 |
| Code Lines | 43 |
| 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 |
||
| 137 | #[Route( |
||
| 138 | '/{name}/{path}', |
||
| 139 | name: 'theme_asset', |
||
| 140 | requirements: ['path' => '.+'], |
||
| 141 | methods: ['GET'], |
||
| 142 | priority: -10 |
||
| 143 | )] |
||
| 144 | public function index( |
||
| 145 | string $name, |
||
| 146 | string $path, |
||
| 147 | Request $request, |
||
| 148 | #[Autowire(service: 'oneup_flysystem.themes_filesystem')] FilesystemOperator $filesystem |
||
| 149 | ): Response { |
||
| 150 | $themeDir = basename($name); |
||
| 151 | $strict = $request->query->getBoolean('strict', false); |
||
| 152 | |||
| 153 | if (!$filesystem->directoryExists($themeDir)) { |
||
| 154 | throw $this->createNotFoundException('The folder name does not exist.'); |
||
| 155 | } |
||
| 156 | |||
| 157 | $filePath = null; |
||
| 158 | |||
| 159 | if ($strict) { |
||
| 160 | $candidate = $themeDir.DIRECTORY_SEPARATOR.$path; |
||
| 161 | if ($filesystem->fileExists($candidate)) { |
||
| 162 | $filePath = $candidate; |
||
| 163 | } else { |
||
| 164 | throw $this->createNotFoundException('The requested file does not exist.'); |
||
| 165 | } |
||
| 166 | } else { |
||
| 167 | $candidates = [ |
||
| 168 | $themeDir.DIRECTORY_SEPARATOR.$path, |
||
| 169 | ThemeHelper::DEFAULT_THEME.DIRECTORY_SEPARATOR.$path, |
||
| 170 | ]; |
||
| 171 | foreach ($candidates as $c) { |
||
| 172 | if ($filesystem->fileExists($c)) { |
||
| 173 | $filePath = $c; |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | if (!$filePath) { |
||
| 178 | throw $this->createNotFoundException('The requested file does not exist.'); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $response = new StreamedResponse(function () use ($filesystem, $filePath): void { |
||
| 183 | $out = fopen('php://output', 'wb'); |
||
| 184 | $in = $filesystem->readStream($filePath); |
||
| 185 | stream_copy_to_stream($in, $out); |
||
| 186 | fclose($out); |
||
| 187 | fclose($in); |
||
| 188 | }); |
||
| 189 | |||
| 190 | $mimeType = $filesystem->mimeType($filePath) ?: 'application/octet-stream'; |
||
| 191 | if (str_ends_with(strtolower($filePath), '.svg')) { |
||
| 192 | $mimeType = 'image/svg+xml'; |
||
| 193 | } |
||
| 194 | |||
| 195 | $disposition = $response->headers->makeDisposition( |
||
| 196 | ResponseHeaderBag::DISPOSITION_INLINE, |
||
| 197 | basename($path) |
||
| 198 | ); |
||
| 199 | |||
| 200 | $response->headers->set('Content-Disposition', $disposition); |
||
| 201 | $response->headers->set('Content-Type', $mimeType); |
||
| 202 | $response->headers->set('Cache-Control', 'no-store'); |
||
| 203 | |||
| 204 | return $response; |
||
| 205 | } |
||
| 223 |