ManageEtagHeader::addEtagHeader()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace RichanFongdasen\Varnishable\Concerns;
4
5
use Symfony\Component\HttpFoundation\Response;
6
7
trait ManageEtagHeader
8
{
9
    /**
10
     * Add an ETag header to the current response.
11
     *
12
     * @param \Symfony\Component\HttpFoundation\Response $response
13
     *
14
     * @return void
15
     */
16
    protected function addEtagHeader(Response $response): void
17
    {
18
        $useEtag = (bool) $this->getConfig('use_etag');
19
        $content = $response->getContent();
20
21
        if (($content !== false) && $useEtag) {
22
            $response->setEtag(md5($content));
23
        }
24
    }
25
26
    /**
27
     * Disable Etag for current request.
28
     *
29
     * @return void
30
     */
31
    public function disableEtag(): void
32
    {
33
        $this->setConfig('use_etag', false);
34
    }
35
36
    /**
37
     * Enable Etag for current request.
38
     *
39
     * @return void
40
     */
41
    public function enableEtag(): void
42
    {
43
        $this->setConfig('use_etag', true);
44
    }
45
46
    /**
47
     * Get configuration value for a specific key.
48
     *
49
     * @param string $key
50
     *
51
     * @return mixed
52
     */
53
    abstract public function getConfig($key);
54
55
    /**
56
     * Set configuration value for a specific key.
57
     *
58
     * @param string $key
59
     * @param mixed  $value
60
     *
61
     * @return void
62
     */
63
    abstract public function setConfig($key, $value): void;
64
}
65