IfRange   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 112
ccs 32
cts 32
cp 1
rs 10
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setValidator() 0 3 1
A fromValue() 0 15 5
A isValid() 0 3 1
A getName() 0 3 1
A getValidator() 0 3 1
B evaluate() 0 13 7
A getValue() 0 7 2
A __toString() 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\Header\Value\EntityTag\EntityTag;
10
use Stadly\Http\Utilities\Rfc7231;
11
use Stadly\Http\Utilities\Rfc7232;
12
13
/**
14
 * Class for handling the HTTP header field If-Range.
15
 *
16
 * Specification: https://tools.ietf.org/html/rfc7233#section-3.2
17
 */
18
final class IfRange implements Header
19
{
20
    /**
21
     * @var EntityTag|Date Strong validator.
22
     */
23
    private $validator;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param EntityTag|Date $validator Strong validator.
29
     */
30 4
    public function __construct($validator)
31
    {
32 4
        $this->validator = $validator;
33
    }
34
35
    /**
36
     * Construct header from value.
37
     *
38
     * @param string $value Header value.
39
     * @return self Header generated based on the value.
40
     * @throws InvalidHeader If the header value is invalid.
41
     */
42 4
    public static function fromValue(string $value): self
43
    {
44 4
        $entityTagRegEx = '{^' . Rfc7232::ENTITY_TAG . '$}';
45 4
        $plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
46 4
        if ($plainValue === $value && preg_match($entityTagRegEx, $value) === 1) {
47 2
            return new self(EntityTag::fromString($value));
48
        }
49
50 2
        $dateRegEx = '{^' . Rfc7231::HTTP_DATE . '$}';
51 2
        $plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
52 2
        if ($plainValue === $value && preg_match($dateRegEx, $value) === 1) {
53 1
            return new self(Date::fromString($value, /*isWeak*/false));
54
        }
55
56 1
        throw new InvalidHeader('Invalid header value: ' . $value);
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 4
    public function __toString(): string
63
    {
64 4
        return $this->getName() . ': ' . $this->getValue();
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 4
    public function isValid(): bool
71
    {
72 4
        return !$this->validator->isWeak();
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78 1
    public function getName(): string
79
    {
80 1
        return 'If-Range';
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86 4
    public function getValue(): string
87
    {
88 4
        if ($this->validator->isWeak()) {
89 2
            throw new InvalidHeader('Validator must be strong.');
90
        }
91
92 2
        return (string)$this->validator;
93
    }
94
95
    /**
96
     * @param EntityTag|Date|null $validator Validator. Should be a strong entity tag or date, or null if unknown.
97
     * @return bool Whether the condition is satisfied by the validator.
98
     */
99 11
    public function evaluate($validator): bool
100
    {
101 11
        if ($validator instanceof EntityTag && $this->validator instanceof EntityTag) {
102
            // Entity tags must be strong.
103 3
            return $this->validator->compareStrongly($validator);
104
        }
105
106 8
        if ($validator instanceof Date && $this->validator instanceof Date) {
107
            // Dates must be strong and match exactly.
108 4
            return !$validator->isWeak() && !$this->validator->isWeak() && $this->validator->isEq($validator);
109
        }
110
111 4
        return false;
112
    }
113
114
    /**
115
     * @return EntityTag|Date Strong validator.
116
     */
117 1
    public function getValidator()
118
    {
119 1
        return $this->validator;
120
    }
121
122
    /**
123
     * Set validator.
124
     *
125
     * @param EntityTag|Date $validator Strong validator.
126
     */
127 4
    public function setValidator($validator): void
128
    {
129 4
        $this->validator = $validator;
130
    }
131
}
132