Completed
Push — master ( 000445...c8f0e6 )
by
unknown
04:21
created

Parameter::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Helper;
4
5
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
6
use Symfony\Bundle\FrameworkBundle\Routing\Router;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
9
class Parameter
10
{
11
    const ROUTE_NAME_DEFAULT = 'default';
12
13
    const PARAMETER_ID = 'id';
14
    const PARAMETER_SIGNATURE = 's';
15
    const PARAMETER_VERSION = 'v';
16
    const PARAMETER_ANTI_CACHE = 'ac'; // allow cache busting by adding &ac=<random>
17
18
    /**
19
     * @var Router
20
     */
21
    protected $router;
22
23
    /**
24
     * @var array
25
     */
26
    protected $routeNames;
27
28
    /**
29
     * @var string
30
     */
31
    protected $secret;
32
33
    /**
34
     * @var string
35
     */
36
    protected $destinationPrefix;
37
38
    /**
39
     * @var string
40
     */
41
    protected $hashAlgorithm = 'sha256';
42
43
    /**
44
     * @param Router $router
45
     * @param array $routeNames
46
     * @param string $secret
47
     * @param string $destinationPrefix
48
     */
49
    public function __construct(Router $router, array $routeNames, $secret, $destinationPrefix = 'images/')
50
    {
51
        $this->router = $router;
52
        $this->secret = $secret;
53
        $this->destinationPrefix = $destinationPrefix;
54
55
        $this->setRouteNames($routeNames);
56
    }
57
58
    /**
59
     * @param array $routeNames
60
     * @throws \Exception
61
     */
62
    protected function setRouteNames(array $routeNames)
63
    {
64
        if (!array_key_exists(self::ROUTE_NAME_DEFAULT, $routeNames)) {
65
            throw new \Exception(sprintf('Route name "%s" is required', self::ROUTE_NAME_DEFAULT));
66
        }
67
68
        $this->routeNames = $routeNames;
69
    }
70
71
    /**
72
     * @param MediaInterface $media
73
     * @param array $parameters
74
     * @param string $routeName
75
     * @return string
76
     */
77 View Code Duplication
    public function generateUrl(MediaInterface $media, $parameters, $routeName = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if (empty($routeName)) {
80
            $routeName = self::ROUTE_NAME_DEFAULT;
81
        }
82
83
        return $this->router->generate(
84
            $this->routeNames[$routeName],
85
            $this->signParameters($media->getDefaultUrlParameters() + $parameters),
86
            UrlGeneratorInterface::ABSOLUTE_URL
87
        );
88
    }
89
90
    /**
91
     * @param array $parameters
92
     * @return array
93
     */
94
    protected function signParameters(array $parameters)
95
    {
96
        $parameters[self::PARAMETER_SIGNATURE] = $this->calculateSignature($parameters);
97
98
        return $parameters;
99
    }
100
101
    /**
102
     * @param array $parameters
103
     * @return string
104
     */
105
    protected function calculateSignature(array $parameters)
106
    {
107
        return hash_hmac($this->hashAlgorithm, $this->secret, json_encode($this->normalizeParameters($parameters)));
108
    }
109
110
    /**
111
     * @param array $parameters
112
     * @return array
113
     */
114
    protected function normalizeParameters(array $parameters)
115
    {
116
        if (isset($parameters[self::PARAMETER_SIGNATURE])) {
117
            unset($parameters[self::PARAMETER_SIGNATURE]);
118
        }
119
        if (isset($parameters[self::PARAMETER_ANTI_CACHE])) {
120
            unset($parameters[self::PARAMETER_ANTI_CACHE]);
121
        }
122
        ksort($parameters);
123
124
        $parametersNormalized = [];
125
        foreach ($parameters as $k => $v) {
126
            $parametersNormalized[$k] = (string)$v;
127
        }
128
129
        return $parametersNormalized;
130
    }
131
132
    /**
133
     * @param array $parameters
134
     * @return bool
135
     */
136
    public function isValid(array $parameters)
137
    {
138
        if (!hash_equals($this->calculateSignature($parameters), $parameters[self::PARAMETER_SIGNATURE])) {
139
            return false;
140
        }
141
142
        return true;
143
    }
144
145
    /**
146
     * @param array $parameters
147
     * @return string
148
     */
149
    protected function getFormat(array $parameters)
150
    {
151
        if (isset($parameters['fm'])) {
152
            return $parameters['fm'];
153
        }
154
155
        return 'jpg';
156
    }
157
158
    /**
159
     * @param $source
160
     * @param $parameters
161
     * @return string
162
     */
163
    public function getDestinationFilename($source, $parameters)
164
    {
165
        $parameters = $this->normalizeParameters($parameters);
166
167
        $parametersFlat = [];
168
        foreach ($parameters as $k => $v) {
169
            $parametersFlat[] = $k.$v;
170
        }
171
172
        return $this->destinationPrefix.
173
            pathinfo($source, PATHINFO_FILENAME).'/'.implode('_', $parametersFlat).'.'.$this->getFormat($parameters);
174
    }
175
}
176