ManipulateHttpResponse::setRequestHeaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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): void
26
    {
27
        $esiHeader = $this->requestHeaders->get($this->getConfig('esi_capability_header'));
28
29
        if ($esiHeader !== null) {
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
     *
40
     * @return \Symfony\Component\HttpFoundation\Response
41
     */
42
    protected function addCacheableHeader(Response $response): Response
43
    {
44
        $duration = $this->getCacheDuration();
45
46
        $response->headers->set($this->getConfig('cacheable_header'), '1');
47
        $response->headers->set('Cache-Control', 'public, max-age='.$duration);
48
49
        return $response;
50
    }
51
52
    /**
53
     * Add uncacheable header so varnish can recognize
54
     * the response as an uncacheable content.
55
     *
56
     * @param \Symfony\Component\HttpFoundation\Response $response
57
     *
58
     * @return void
59
     */
60
    public function addUncacheableHeader(Response $response): void
61
    {
62
        $response->headers->set($this->getConfig('uncacheable_header'), '1');
63
    }
64
65
    /**
66
     * Normalize the cache duration value and convert
67
     * it to seconds.
68
     *
69
     * @return int|float
70
     */
71
    protected function getCacheDuration()
72
    {
73
        return $this->getConfig('cache_duration') * 60;
74
    }
75
76
    /**
77
     * Manipulate the current Http response.
78
     *
79
     * @param \Symfony\Component\HttpFoundation\Response $response
80
     *
81
     * @return \Symfony\Component\HttpFoundation\Response
82
     */
83
    public function manipulate(Response $response): Response
84
    {
85
        $this->acknowledgeEsiSupport($response);
86
87
        if ($this->shouldNotCache($response)) {
88
            return $response;
89
        }
90
91
        $this->addCacheableHeader($response);
92
        $this->addLastModifiedHeader($response);
93
        $this->addEtagHeader($response);
94
95
        return $response;
96
    }
97
98
    /**
99
     * Set cache duration value in minutes. This value will
100
     * be added to the HTTP response's Cache-Control header.
101
     *
102
     * @param int $duration [Cache duration value in minutes]
103
     *
104
     * @return void
105
     */
106
    public function setCacheDuration(int $duration): void
107
    {
108
        $this->setConfig('cache_duration', $duration);
109
    }
110
111
    /**
112
     * Set the current Http request headers.
113
     *
114
     * @param \Symfony\Component\HttpFoundation\HeaderBag $headers
115
     *
116
     * @return void
117
     */
118
    public function setRequestHeaders(HeaderBag $headers): void
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 bool
129
     */
130
    protected function shouldNotCache(Response $response): bool
131
    {
132
        $headers = (array) $response->headers->get($this->getConfig('uncacheable_header'));
133
134
        return count($headers) > 0;
135
    }
136
137
    /**
138
     * Add an ETag header to the current response.
139
     *
140
     * @param \Symfony\Component\HttpFoundation\Response $response
141
     *
142
     * @return void
143
     */
144
    abstract protected function addEtagHeader(Response $response): void;
145
146
    /**
147
     * Add Last-Modified header to the current response.
148
     *
149
     * @param \Symfony\Component\HttpFoundation\Response $response
150
     *
151
     * @return void
152
     */
153
    abstract protected function addLastModifiedHeader(Response $response): void;
154
155
    /**
156
     * Get configuration value for a specific key.
157
     *
158
     * @param string $key
159
     *
160
     * @return mixed
161
     */
162
    abstract public function getConfig($key);
163
164
    /**
165
     * Set configuration value for a specific key.
166
     *
167
     * @param string $key
168
     * @param mixed  $value
169
     *
170
     * @return void
171
     */
172
    abstract public function setConfig($key, $value): void;
173
}
174