Completed
Push — master ( 4f85c2...d0915c )
by
unknown
03:44
created

RedirectHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 58
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A redirectToMediaImage() 0 12 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Helper;
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
use Symfony\Component\HttpFoundation\Request;
10
11
class RedirectHelper
12
{
13
    /**
14
     * @var ParameterHandlerInterface
15
     */
16
    private $parameterHandler;
17
18
    /**
19
     * @var ImageGenerator
20
     */
21
    private $imageGenerator;
22
23
    /**
24
     * @var string
25
     */
26
    private $mediaBaseUrl;
27
28
    /**
29
     * @var int
30
     */
31
    private $cacheTtl;
32
33
    /**
34
     * @param ParameterHandlerInterface $parameterHandler
35
     * @param ImageGenerator $imageGenerator
36
     * @param string $mediaBaseUrl
37
     * @param int $cacheTtl
38
     */
39
    public function __construct(
40
        ParameterHandlerInterface $parameterHandler,
41
        ImageGenerator $imageGenerator,
42
        $mediaBaseUrl,
43
        $cacheTtl
44
    ) {
45
        $this->parameterHandler = $parameterHandler;
46
        $this->imageGenerator = $imageGenerator;
47
        $this->mediaBaseUrl = $mediaBaseUrl;
48
        $this->cacheTtl = $cacheTtl;
49
    }
50
51
    /**
52
     * @param MediaInterface $media
53
     * @param Request $request
54
     * @return RedirectResponse
55
     */
56
    public function redirectToMediaImage(MediaInterface $media, Request $request)
57
    {
58
        $parameters = $this->parameterHandler->getPayload($media, $request);
59
        // @todo apply default parameters?
60
        // @todo apply media parameters
61
        $filename = $this->imageGenerator->generate($media, $parameters);
62
63
        $response = new RedirectResponse($this->mediaBaseUrl.$filename);
64
        $response->setSharedMaxAge($this->cacheTtl);
65
        $response->setMaxAge($this->cacheTtl);
66
        return $response;
67
    }
68
}
69