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
|
|
|
|