Completed
Push — master ( 071097...7934b9 )
by
unknown
09:17
created

ImageUtility::getRedirectResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 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 MediaInterface $media
52
     * @param int $width
53
     * @param int $height
54
     * @param array $extra
55
     * @return RedirectResponse
56
     */
57 3
    public function getRedirectResponse(MediaInterface $media, $width, $height, array $extra = [])
58
    {
59 3
        $response = new RedirectResponse($this->mediaBaseUrl.$this->getFilename($media, $width, $height, $extra));
60 3
        $response->setSharedMaxAge($this->cacheTtl);
61 3
        $response->setMaxAge($this->cacheTtl);
62
63 3
        return $response;
64
    }
65
66
    /**
67
     * @param MediaInterface $media
68
     * @param int $width
69
     * @param int $height
70
     * @param array $parameters
71
     * @return string
72
     */
73 3
    public function getFilename(MediaInterface $media, $width, $height, array $parameters)
74
    {
75 3
        $parameterBag = $this->parameterHandler->getPayload($media, $width, $height, $parameters);
76 3
        $filename = $this->imageGenerator->generate($media, $parameterBag);
77
78 3
        return $filename;
79
    }
80
}
81