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