Conditions | 4 |
Paths | 4 |
Total Lines | 24 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
22 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
23 | { |
||
24 | $id = (int) $request->getAttribute('id'); |
||
25 | |||
26 | /** @var null|File $file */ |
||
27 | $file = $this->fileRepository->find($id); |
||
28 | if (!$file) { |
||
29 | return $this->createError("File $id not found in database"); |
||
30 | } |
||
31 | |||
32 | $path = $file->getPath(); |
||
33 | if (!is_readable($path)) { |
||
34 | return $this->createError("File for $id not found on disk, or not readable"); |
||
35 | } |
||
36 | |||
37 | $resource = fopen($path, 'rb'); |
||
38 | if ($resource === false) { |
||
39 | return $this->createError("Cannot open file for $id on disk"); |
||
40 | } |
||
41 | $size = filesize($path); |
||
42 | $type = mime_content_type($path); |
||
43 | $response = new Response($resource, 200, ['content-type' => $type, 'content-length' => $size]); |
||
44 | |||
45 | return $response; |
||
46 | } |
||
48 |