ManageEtagHeader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addEtagHeader() 0 9 3
A disableEtag() 0 4 1
A enableEtag() 0 4 1
getConfig() 0 1 ?
setConfig() 0 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