Completed
Push — master ( 0c7a6e...91504d )
by Richan
01:42
created

ManipulateHttpResponse::acknowledgeEsiSupport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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