IfUnmodifiedSince   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 99
ccs 24
cts 24
cp 1
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDate() 0 3 1
A evaluate() 0 8 2
A __toString() 0 3 1
A getName() 0 3 1
A fromValue() 0 9 3
A isValid() 0 3 1
A getValue() 0 3 1
A getDate() 0 3 1
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-Unmodified-Since.
13
 *
14
 * Specification: https://tools.ietf.org/html/rfc7232#section-3.4
15
 */
16
final class IfUnmodifiedSince 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
    }
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_UNMODIFIED_SINCE . '$}';
43 2
        $plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
44 2
        if ($plainValue !== $value || preg_match($regEx, $value) !== 1) {
45 1
            throw new InvalidHeader('Invalid header value: ' . $value);
46
        }
47
48 1
        return new self(Date::fromString($value));
49
    }
50
51
    /**
52
     * @inheritDoc
53
     * @throws void The header is always valid.
54
     */
55 1
    public function __toString(): string
56
    {
57 1
        return $this->getName() . ': ' . $this->getValue();
58
    }
59
60
    /**
61
     * @return true The header is always valid.
62
     */
63 1
    public function isValid(): bool
64
    {
65 1
        return true;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 1
    public function getName(): string
72
    {
73 1
        return 'If-Unmodified-Since';
74
    }
75
76
    /**
77
     * @inheritDoc
78
     * @throws void The header is always valid.
79
     */
80 1
    public function getValue(): string
81
    {
82 1
        return (string)$this->date;
83
    }
84
85
    /**
86
     * @param Date|null $lastModifiedDate Last modified date, or null if unknown.
87
     * @return bool Whether the condition is satisfied by the last modified date.
88
     */
89 4
    public function evaluate(?Date $lastModifiedDate): bool
90
    {
91
        // If last modified date is unknown, the condition is not satisfied.
92 4
        if ($lastModifiedDate === null) {
93 1
            return false;
94
        }
95
96 3
        return $lastModifiedDate->isLte($this->date);
97
    }
98
99
    /**
100
     * @return Date Date.
101
     */
102 1
    public function getDate(): Date
103
    {
104 1
        return $this->date;
105
    }
106
107
    /**
108
     * Set date.
109
     *
110
     * @param Date $date Date.
111
     */
112 1
    public function setDate(Date $date): void
113
    {
114 1
        $this->date = $date;
115
    }
116
}
117