Passed
Push — master ( fedff9...360060 )
by Magnar Ovedal
07:05
created

IfModifiedSince::getDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Request;
6
7
use Stadly\Http\Exception\InvalidHeader;
8
use Stadly\Http\Header\Value\Date;
9
use Stadly\Http\Utilities\Rfc7232;
10
11
/**
12
 * Class for handling the HTTP header field If-Modified-Since.
13
 *
14
 * Specification: https://tools.ietf.org/html/rfc7232#section-3.3
15
 */
16
final class IfModifiedSince implements Header
17
{
18
    /**
19
     * @var Date Date.
20
     */
21
    private $date;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param Date $date Date.
27
     */
28 1
    public function __construct(Date $date)
29
    {
30 1
        $this->setDate($date);
31 1
    }
32
33
    /**
34
     * Construct header from value.
35
     *
36
     * @param string $value Header value.
37
     * @return self Header generated based on the value.
38
     * @throws InvalidHeader If the header value is invalid.
39
     */
40 2
    public static function fromValue(string $value): self
41
    {
42 2
        $regEx = '{^' . Rfc7232::IF_MODIFIED_SINCE . '$}';
43 2
        if (utf8_decode($value) !== $value || preg_match($regEx, $value) !== 1) {
44 1
            throw new InvalidHeader('Invalid header value: ' . $value);
45
        }
46
47 1
        return new self(Date::fromString($value));
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @throws void The header is always valid.
53
     */
54 1
    public function __toString(): string
55
    {
56 1
        return $this->getName() . ': ' . $this->getValue();
57
    }
58
59
    /**
60
     * @return true The header is always valid.
61
     */
62 1
    public function isValid(): bool
63
    {
64 1
        return true;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 1
    public function getName(): string
71
    {
72 1
        return 'If-Modified-Since';
73
    }
74
75
    /**
76
     * @inheritDoc
77
     * @throws void The header is always valid.
78
     */
79 1
    public function getValue(): string
80
    {
81 1
        return (string)$this->date;
82
    }
83
84
    /**
85
     * @param Date|null $lastModifiedDate Last modified date, or null if unknown.
86
     * @return bool Whether the condition is satisfied by the last modified date.
87
     */
88 4
    public function evaluate(?Date $lastModifiedDate): bool
89
    {
90
        // If last modified date is unknown, the condition is satisfied.
91 4
        if ($lastModifiedDate === null) {
92 1
            return true;
93
        }
94
95 3
        return $lastModifiedDate->isGt($this->date);
96
    }
97
98
    /**
99
     * @return Date Date.
100
     */
101 1
    public function getDate(): Date
102
    {
103 1
        return $this->date;
104
    }
105
106
    /**
107
     * Set date.
108
     *
109
     * @param Date $date Date.
110
     */
111 1
    public function setDate(Date $date): void
112
    {
113 1
        $this->date = $date;
114 1
    }
115
}
116