Completed
Pull Request — master (#6)
by Richan
11:44 queued 05:32
created

ManipulateHttpResponse::addLastModifiedHeader()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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
     * Normalize the given cache duration and convert
64
     * it to seconds.
65
     *
66
     * @param int $duration
67
     *
68
     * @return int|float
69
     */
70
    protected function getCacheDuration($duration)
71
    {
72
        $cacheInMinutes = ($duration > 0) ? $duration : $this->getConfig('cache_duration');
73
74
        return $cacheInMinutes * 60;
75
    }
76
77
    /**
78
     * Manipulate the current Http response.
79
     *
80
     * @param \Symfony\Component\HttpFoundation\Response $response
81
     * @param int                                        $cacheDuration
82
     *
83
     * @return \Symfony\Component\HttpFoundation\Response
84
     */
85
    public function manipulate(Response $response, $cacheDuration)
86
    {
87
        $this->acknowledgeEsiSupport($response);
88
89
        if ($this->shouldNotCache($response)) {
90
            return $response;
91
        }
92
93
        $this->addCacheableHeader($response, $cacheDuration);
94
        $this->addLastModifiedHeader($response);
95
        $this->addEtagHeader($response);
96
97
        return $response;
98
    }
99
100
    /**
101
     * Set the current Http request headers.
102
     *
103
     * @param \Symfony\Component\HttpFoundation\HeaderBag $headers
104
     */
105
    public function setRequestHeaders(HeaderBag $headers)
106
    {
107
        $this->requestHeaders = $headers;
108
    }
109
110
    /**
111
     * Check if the current response shouldn't be cached.
112
     *
113
     * @param \Symfony\Component\HttpFoundation\Response $response
114
     *
115
     * @return string|array
116
     */
117
    protected function shouldNotCache(Response $response)
118
    {
119
        return $response->headers->get($this->getConfig('uncacheable_header'));
120
    }
121
122
    /**
123
     * Add an ETag header to the current response.
124
     *
125
     * @param \Symfony\Component\HttpFoundation\Response $response
126
     *
127
     * @return void
128
     */
129
    abstract protected function addEtagHeader(Response $response);
130
131
    /**
132
     * Add Last-Modified header to the current response.
133
     *
134
     * @param \Symfony\Component\HttpFoundation\Response $response
135
     *
136
     * @return void
137
     */
138
    abstract protected function addLastModifiedHeader(Response $response);
139
140
    /**
141
     * Get configuration value for a specific key.
142
     *
143
     * @param string $key
144
     *
145
     * @return mixed
146
     */
147
    abstract public function getConfig($key);
148
}
149