|
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); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
4 |
|
return $filename; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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.