Passed
Push — master ( 5afe13...cf1297 )
by Magnar Ovedal
10:13
created

IfRange::evaluate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 5
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 7
rs 8.8333
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 4
    }
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
        if (utf8_decode($value) === $value && preg_match($entityTagRegEx, $value) === 1) {
46 2
            return new self(EntityTag::fromString($value));
47
        }
48
49 2
        $dateRegEx = '{^' . Rfc7231::HTTP_DATE . '$}';
50 2
        if (utf8_decode($value) === $value && preg_match($dateRegEx, $value) === 1) {
51 1
            return new self(Date::fromString($value, /*isWeak*/false));
52
        }
53
54 1
        throw new InvalidHeader('Invalid header value: ' . $value);
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 4
    public function __toString(): string
61
    {
62 4
        return $this->getName() . ': ' . $this->getValue();
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 4
    public function isValid(): bool
69
    {
70 4
        return !$this->validator->isWeak();
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function getName(): string
77
    {
78 1
        return 'If-Range';
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 4
    public function getValue(): string
85
    {
86 4
        if ($this->validator->isWeak()) {
87 2
            throw new InvalidHeader('Validator must be strong.');
88
        }
89
90 2
        return (string)$this->validator;
91
    }
92
93
    /**
94
     * @param EntityTag|Date|null $validator Validator. Should be a strong entity tag or date, or null if unknown.
95
     * @return bool Whether the condition is satisfied by the validator.
96
     */
97 11
    public function evaluate($validator): bool
98
    {
99 11
        if ($validator instanceof EntityTag && $this->validator instanceof EntityTag) {
100
            // Entity tags must be strong.
101 3
            return $this->validator->compareStrongly($validator);
102
        }
103
104 8
        if ($validator instanceof Date && $this->validator instanceof Date) {
105
            // Dates must be strong and match exactly.
106 4
            return !$validator->isWeak() && !$this->validator->isWeak() && $this->validator->isEq($validator);
107
        }
108
109 4
        return false;
110
    }
111
112
    /**
113
     * @return EntityTag|Date Strong validator.
114
     */
115 1
    public function getValidator()
116
    {
117 1
        return $this->validator;
118
    }
119
120
    /**
121
     * Set validator.
122
     *
123
     * @param EntityTag|Date $validator Strong validator.
124
     */
125 4
    public function setValidator($validator): void
126
    {
127 4
        $this->validator = $validator;
128 4
    }
129
}
130