ImageUtility   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 67
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getRedirectResponse() 0 8 1
A getFilename() 0 7 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Utility;
4
5
use MediaMonks\SonataMediaBundle\Generator\ImageGenerator;
6
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
7
use MediaMonks\SonataMediaBundle\ParameterBag\ParameterBagInterface;
8
use MediaMonks\SonataMediaBundle\Handler\ParameterHandlerInterface;
9
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
12
class ImageUtility
13
{
14
    /**
15
     * @var ParameterHandlerInterface
16
     */
17
    private $parameterHandler;
18
19
    /**
20
     * @var ImageGenerator
21
     */
22
    private $imageGenerator;
23
24
    /**
25
     * @var string
26
     */
27
    private $mediaBaseUrl;
28
29
    /**
30
     * @var int
31
     */
32
    private $cacheTtl;
33
34
    /**
35
     * @param ParameterHandlerInterface $parameterHandler
36
     * @param ImageGenerator $imageGenerator
37
     * @param string $mediaBaseUrl
38
     * @param int $cacheTtl
39
     */
40 4
    public function __construct(
41
        ParameterHandlerInterface $parameterHandler,
42
        ImageGenerator $imageGenerator,
43
        string $mediaBaseUrl,
44
        int $cacheTtl
45
    ) {
46 4
        $this->parameterHandler = $parameterHandler;
47 4
        $this->imageGenerator = $imageGenerator;
48 4
        $this->mediaBaseUrl = $mediaBaseUrl;
49 4
        $this->cacheTtl = $cacheTtl;
50 4
    }
51
52
    /**
53
     * @param MediaInterface $media
54
     * @param ImageParameterBag $parameterBag
55
     * @return RedirectResponse
56
     */
57 4
    public function getRedirectResponse(MediaInterface $media, ImageParameterBag $parameterBag): RedirectResponse
58
    {
59 4
        $response = new RedirectResponse($this->mediaBaseUrl.$this->getFilename($media, $parameterBag));
60 4
        $response->setSharedMaxAge($this->cacheTtl);
61 4
        $response->setMaxAge($this->cacheTtl);
62
63 4
        return $response;
64
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param ImageParameterBag $parameterBag
69
     * @return string
70
     */
71 4
    public function getFilename(MediaInterface $media, ImageParameterBag $parameterBag): string
72
    {
73 4
        $parameterBag = $this->parameterHandler->validateParameterBag($media, $parameterBag);
74 4
        $filename = $this->imageGenerator->generate($media, $parameterBag);
0 ignored issues
show
Compatibility introduced by
$parameterBag of type object<MediaMonks\Sonata...\ParameterBagInterface> is not a sub-type of object<MediaMonks\Sonata...rBag\ImageParameterBag>. It seems like you assume a concrete implementation of the interface MediaMonks\SonataMediaBu...g\ParameterBagInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
76 4
        return $filename;
77
    }
78
}
79