EtagHasher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder;
11
12
use Symfony\Component\HttpFoundation\RequestStack;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class EtagHasher implements EtagHasherInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    const ETAG_SEPARATOR = '|';
21
22
    /**
23
     * @var RequestStack
24
     */
25
    protected $request_stack;
26
27
    /**
28
     * @var string
29
     */
30
    protected $algorithm = '';
31
32
    /**
33
     * @param RequestStack $request_stack
34
     * @param string $algorithm
35
     */
36 3
    public function __construct(RequestStack $request_stack, $algorithm)
37
    {
38 3
        $this->request_stack = $request_stack;
39 3
        $this->algorithm = $algorithm;
40 3
    }
41
42
    /**
43
     * @param Response $response
44
     *
45
     * @return string
46
     */
47 3
    public function hash(Response $response)
48
    {
49
        $params = [
50 3
            $response->getLastModified()->format(\DateTime::ISO8601),
51 3
        ];
52
53
        // add cookies to ETag
54 3
        if ($this->request_stack->getMasterRequest()) {
55 2
            $params[] = http_build_query($this->request_stack->getMasterRequest()->cookies->all());
56 2
        }
57
58 3
        return hash($this->algorithm, implode(self::ETAG_SEPARATOR, $params));
59
    }
60
}
61