Completed
Push — master ( 0edb31...d8ec53 )
by Richan
01:27
created

ManageLastModifiedHeader::setConfig()

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 Carbon\Carbon;
6
use Symfony\Component\HttpFoundation\Response;
7
8
trait ManageLastModifiedHeader
9
{
10
    /**
11
     * Last modified header value.
12
     *
13
     * @var \Carbon\Carbon|null
14
     */
15
    protected $lastModified = null;
16
17
    /**
18
     * Add Last-Modified header to the current response.
19
     *
20
     * @param \Symfony\Component\HttpFoundation\Response $response
21
     *
22
     * @return void
23
     */
24
    protected function addLastModifiedHeader(Response $response)
25
    {
26
        $lastModified = $this->getLastModifiedHeader();
27
28
        if ($this->getConfig('use_last_modified') && ($lastModified !== null)) {
29
            $response->setLastModified($lastModified);
30
        }
31
    }
32
33
    /**
34
     * Disable Last-Modified header for the current response.
35
     *
36
     * @return void
37
     */
38
    public function disableLastModified()
39
    {
40
        $this->setConfig('use_last_modified', false);
41
    }
42
43
    /**
44
     * Enable Last-Modified header for the current response.
45
     *
46
     * @return void
47
     */
48
    public function enableLastModified()
49
    {
50
        $this->setConfig('use_last_modified', true);
51
    }
52
53
    /**
54
     * Get last modified header for the current response.
55
     *
56
     * @return \Carbon\Carbon|null
57
     */
58
    public function getLastModifiedHeader()
59
    {
60
        return $this->lastModified;
61
    }
62
63
    /**
64
     * Set last modified header for the current response.
65
     *
66
     * @param \Carbon\Carbon|string $current
67
     */
68
    public function setLastModifiedHeader($current)
69
    {
70
        if (!($current instanceof Carbon)) {
71
            $current = new Carbon($current);
72
        }
73
74
        if (($this->lastModified === null) || ($current->getTimestamp() > $this->lastModified->getTimestamp())) {
75
            $this->lastModified = $current;
76
        }
77
    }
78
79
    /**
80
     * Get configuration value for a specific key.
81
     *
82
     * @param string $key
83
     *
84
     * @return mixed
85
     */
86
    abstract public function getConfig($key);
87
88
    /**
89
     * Set configuration value for a specific key.
90
     *
91
     * @param string $key
92
     * @param mixed  $value
93
     *
94
     * @return void
95
     */
96
    abstract public function setConfig($key, $value);
97
}
98