Completed
Pull Request — master (#4)
by Richan
01:38
created

ManipulateHttpResponse::addEtagHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace RichanFongdasen\Varnishable\Concerns;
4
5
use Symfony\Component\HttpFoundation\HeaderBag;
6
use Symfony\Component\HttpFoundation\Response;
7
8
trait ManipulateHttpResponse
9
{
10
    /**
11
     * HTTP Request object.
12
     *
13
     * @var \Symfony\Component\HttpFoundation\HeaderBag
14
     */
15
    protected $requestHeaders;
16
17
    /**
18
     * Acknowledge the ESI support and send a specific
19
     * HTTP header as a reply.
20
     *
21
     * @param \Symfony\Component\HttpFoundation\Response $response
22
     *
23
     * @return void
24
     */
25
    protected function acknowledgeEsiSupport(Response $response)
26
    {
27
        $esiHeader = $this->getConfig('esi_capability_header');
28
29
        if ($esiHeader = $this->requestHeaders->get($esiHeader)) {
30
            $response->headers->set($this->getConfig('esi_reply_header'), $esiHeader);
31
        }
32
    }
33
34
    /**
35
     * Add cacheable header so varnish can recognize
36
     * the response as a cacheable content.
37
     *
38
     * @param \Symfony\Component\HttpFoundation\Response $response
39
     * @param int                                        $cacheDuration
40
     */
41
    protected function addCacheableHeader(Response $response, $cacheDuration)
42
    {
43
        $duration = $this->getCacheDuration((int) $cacheDuration);
44
45
        $response->headers->set($this->getConfig('cacheable_header'), '1');
46
        $response->headers->set('Cache-Control', 'public, max-age='.$duration);
47
48
        return $response;
49
    }
50
51
    /**
52
     * Add uncacheable header so varnish can recognize
53
     * the response as an uncacheable content.
54
     *
55
     * @param \Symfony\Component\HttpFoundation\Response $response
56
     */
57
    public function addUncacheableHeader(Response $response)
58
    {
59
        return $response->headers->set($this->getConfig('uncacheable_header'), '1');
60
    }
61
62
    /**
63
     * Add an ETag header to the current response.
64
     *
65
     * @param \Symfony\Component\HttpFoundation\Response $response
66
     *
67
     * @return void
68
     */
69
    protected function addEtagHeader(Response $response)
70
    {
71
        if ($this->getConfig('use_etag')) {
72
            $response->setEtag(md5($response->getContent()));
73
        }
74
    }
75
76
    /**
77
     * Normalize the given cache duration and convert
78
     * it to seconds.
79
     *
80
     * @param int $duration
81
     *
82
     * @return int|float
83
     */
84
    protected function getCacheDuration($duration)
85
    {
86
        $cacheInMinutes = ($duration > 0) ? $duration : $this->getConfig('cache_duration');
87
88
        return $cacheInMinutes * 60;
89
    }
90
91
    /**
92
     * Manipulate the current Http response.
93
     *
94
     * @param \Symfony\Component\HttpFoundation\Response $response
95
     * @param int                                        $cacheDuration
96
     *
97
     * @return \Symfony\Component\HttpFoundation\Response
98
     */
99
    public function manipulate(Response $response, $cacheDuration)
100
    {
101
        $this->acknowledgeEsiSupport($response);
102
103
        if ($this->shouldNotCache($response)) {
104
            return $response;
105
        }
106
107
        $this->addCacheableHeader($response, $cacheDuration);
108
        $this->addEtagHeader($response);
109
110
        return $response;
111
    }
112
113
    /**
114
     * Set the current Http request headers.
115
     *
116
     * @param \Symfony\Component\HttpFoundation\HeaderBag $headers
117
     */
118
    public function setRequestHeaders(HeaderBag $headers)
119
    {
120
        $this->requestHeaders = $headers;
121
    }
122
123
    /**
124
     * Check if the current response shouldn't be cached.
125
     *
126
     * @param \Symfony\Component\HttpFoundation\Response $response
127
     *
128
     * @return string|array
129
     */
130
    protected function shouldNotCache(Response $response)
131
    {
132
        return $response->headers->get($this->getConfig('uncacheable_header'));
133
    }
134
135
    /**
136
     * Get configuration value for a specific key.
137
     *
138
     * @param string $key
139
     *
140
     * @return mixed
141
     */
142
    abstract public function getConfig($key);
143
}
144