Failed Conditions
Push — master ( 34a070...6c283c )
by Adrien
05:56
created

ImageAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Action;
6
7
use Application\Model\Image;
8
use Application\Repository\ImageRepository;
9
use Application\Service\ImageResizer;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Zend\Diactoros\Response;
14
15
class ImageAction extends AbstractAction
16
{
17
    /**
18
     * @var ImageRepository
19
     */
20
    private $imageRepository;
21
22
    /**
23
     * @var ImageResizer
24
     */
25
    private $imageService;
26
27
    public function __construct(ImageRepository $imageRepository, ImageResizer $imageService)
28
    {
29
        $this->imageRepository = $imageRepository;
30
        $this->imageService = $imageService;
31
    }
32
33
    /**
34
     * Serve an image from disk, with optional dynamic resizing
35
     *
36
     * @param ServerRequestInterface $request
37
     * @param RequestHandlerInterface $handler
38
     *
39
     * @return ResponseInterface
40
     */
41
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
42
    {
43
        $id = $request->getAttribute('id');
44
45
        /** @var Image $image */
46
        $image = $this->imageRepository->findOneById($id);
1 ignored issue
show
Bug introduced by
The method findOneById() does not exist on Application\Repository\ImageRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $image = $this->imageRepository->findOneById($id);
Loading history...
47
        if (!$image) {
1 ignored issue
show
introduced by
$image is of type Application\Model\Image, thus it always evaluated to true.
Loading history...
48
            return $this->createError("Image $id not found in database");
49
        }
50
51
        $path = $image->getPath();
52
        if (!is_readable($path)) {
53
            return $this->createError("Image for image $id not found on disk, or not readable");
54
        }
55
56
        $maxHeight = (int) $request->getAttribute('maxHeight');
57
        if ($maxHeight) {
58
            $path = $this->imageService->resize($image, $maxHeight);
59
        }
60
61
        $resource = fopen($path, 'r');
62
        $type = mime_content_type($path);
63
        $response = new Response($resource, 200, ['content-type' => $type]);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $body of Zend\Diactoros\Response::__construct() does only seem to accept Psr\Http\Message\StreamInterface|resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $response = new Response(/** @scrutinizer ignore-type */ $resource, 200, ['content-type' => $type]);
Loading history...
64
65
        return $response;
66
    }
67
}
68