Completed
Push — uncacheable-middleware ( ab3961 )
by Richan
02:06
created

ManipulateHttpResponse::addUncacheableHeader()   A

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 Illuminate\Http\Response;
6
use Symfony\Component\HttpFoundation\HeaderBag;
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 \Illuminate\Http\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->header($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 \Illuminate\Http\Response $response
39
     * @param int                       $cacheDuration
40
     */
41
    protected function addCacheableHeader(Response $response, $cacheDuration)
42
    {
43
        $duration = $this->getCacheDuration((int) $cacheDuration);
44
45
        return $response->header($this->getConfig('cacheable_header'), '1')
46
                ->header('Cache-Control', 'public, max-age='.$duration);
47
    }
48
49
    /**
50
     * Add uncacheable header so varnish can recognize
51
     * the response as an uncacheable content.
52
     *
53
     * @param \Illuminate\Http\Response $response
54
     */
55
    public function addUncacheableHeader(Response $response)
56
    {
57
        return $response->header($this->getConfig('uncacheable_header'), '1');
58
    }
59
60
    /**
61
     * Add an ETag header to the current response.
62
     *
63
     * @param \Illuminate\Http\Response $response
64
     *
65
     * @return void
66
     */
67
    protected function addEtagHeader(Response $response)
68
    {
69
        if ($this->getConfig('use_etag')) {
70
            $response->setEtag(md5($response->getContent()));
71
        }
72
    }
73
74
    /**
75
     * Normalize the given cache duration and convert
76
     * it to seconds.
77
     *
78
     * @param int $duration
79
     *
80
     * @return int|float
81
     */
82
    protected function getCacheDuration($duration)
83
    {
84
        $cacheInMinutes = ($duration > 0) ? $duration : $this->getConfig('cache_duration');
85
86
        return $cacheInMinutes * 60;
87
    }
88
89
    /**
90
     * Manipulate the current Http response.
91
     *
92
     * @param \Illuminate\Http\Response $response
93
     * @param int                       $cacheDuration
94
     *
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function manipulate(Response $response, $cacheDuration)
98
    {
99
        $this->acknowledgeEsiSupport($response);
100
101
        if ($this->shouldNotCache($response)) {
102
            return $response;
103
        }
104
105
        $this->addCacheableHeader($response, $cacheDuration);
106
        $this->addEtagHeader($response);
107
108
        return $response;
109
    }
110
111
    /**
112
     * Set the current Http request headers.
113
     *
114
     * @param \Symfony\Component\HttpFoundation\HeaderBag $headers
115
     */
116
    public function setRequestHeaders(HeaderBag $headers)
117
    {
118
        $this->requestHeaders = $headers;
119
    }
120
121
    /**
122
     * Check if the current response shouldn't be cached.
123
     *
124
     * @param \Illuminate\Http\Response $response
125
     *
126
     * @return string|array
127
     */
128
    protected function shouldNotCache(Response $response)
129
    {
130
        return $response->headers->get($this->getConfig('uncacheable_header'));
131
    }
132
133
    /**
134
     * Get configuration value for a specific key.
135
     *
136
     * @param string $key
137
     *
138
     * @return mixed
139
     */
140
    abstract public function getConfig($key);
141
}
142