Completed
Push — master ( c49bae...43ad9f )
by
unknown
09:25
created

ImageUtility   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A setMediaBaseUrl() 0 4 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\Handler\ParameterHandlerInterface;
7
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
10
class ImageUtility
11
{
12
    /**
13
     * @var ParameterHandlerInterface
14
     */
15
    private $parameterHandler;
16
17
    /**
18
     * @var ImageGenerator
19
     */
20
    private $imageGenerator;
21
22
    /**
23
     * @var string
24
     */
25
    private $mediaBaseUrl;
26
27
    /**
28
     * @var int
29
     */
30
    private $cacheTtl;
31
32
    /**
33
     * @param ParameterHandlerInterface $parameterHandler
34
     * @param ImageGenerator $imageGenerator
35
     * @param string $mediaBaseUrl
36
     * @param int $cacheTtl
37
     */
38 3
    public function __construct(
39
        ParameterHandlerInterface $parameterHandler,
40
        ImageGenerator $imageGenerator,
41
        $mediaBaseUrl,
42
        $cacheTtl
43
    ) {
44 3
        $this->parameterHandler = $parameterHandler;
45 3
        $this->imageGenerator = $imageGenerator;
46 3
        $this->mediaBaseUrl = $mediaBaseUrl;
47 3
        $this->cacheTtl = $cacheTtl;
48 3
    }
49
50
    /**
51
     * @param string $mediaBaseUrl
52
     */
53
    public function setMediaBaseUrl($mediaBaseUrl)
54
    {
55
        $this->mediaBaseUrl = $mediaBaseUrl;
56
    }
57
58
    /**
59
     * @param MediaInterface $media
60
     * @param int $width
61
     * @param int $height
62
     * @param array $extra
63
     * @return RedirectResponse
64
     */
65 3
    public function getRedirectResponse(MediaInterface $media, $width, $height, array $extra = [])
66
    {
67 3
        $response = new RedirectResponse($this->mediaBaseUrl.$this->getFilename($media, $width, $height, $extra));
68 3
        $response->setSharedMaxAge($this->cacheTtl);
69 3
        $response->setMaxAge($this->cacheTtl);
70
71 3
        return $response;
72
    }
73
74
    /**
75
     * @param MediaInterface $media
76
     * @param int $width
77
     * @param int $height
78
     * @param array $parameters
79
     * @return string
80
     */
81 3
    public function getFilename(MediaInterface $media, $width, $height, array $parameters)
82
    {
83 3
        $parameterBag = $this->parameterHandler->getPayload($media, $width, $height, $parameters);
84 3
        $filename = $this->imageGenerator->generate($media, $parameterBag);
85
86 3
        return $filename;
87
    }
88
}
89