Passed
Pull Request — master (#27)
by Anatoly
04:10
created

ContentRangeHeader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Header;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Exception\InvalidHeaderValueException;
18
use Sunrise\Http\Message\Header;
19
20
/**
21
 * Import functions
22
 */
23
use function sprintf;
24
25
/**
26
 * @link https://tools.ietf.org/html/rfc2616#section-14.16
27
 */
28
class ContentRangeHeader extends Header
29
{
30
31
    /**
32
     * @var int
33
     */
34
    private int $firstBytePosition;
35
36
    /**
37
     * @var int
38
     */
39
    private int $lastBytePosition;
40
41
    /**
42
     * @var int
43
     */
44
    private int $instanceLength;
45
46
    /**
47
     * Constructor of the class
48
     *
49
     * @param int $firstBytePosition
50
     * @param int $lastBytePosition
51
     * @param int $instanceLength
52
     *
53
     * @throws InvalidHeaderValueException
54
     *         If the range isn't valid.
55
     */
56 8
    public function __construct(int $firstBytePosition, int $lastBytePosition, int $instanceLength)
57
    {
58 8
        $this->validateRange($firstBytePosition, $lastBytePosition, $instanceLength);
59
60 5
        $this->firstBytePosition = $firstBytePosition;
61 5
        $this->lastBytePosition = $lastBytePosition;
62 5
        $this->instanceLength = $instanceLength;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 3
    public function getFieldName(): string
69
    {
70 3
        return 'Content-Range';
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 3
    public function getFieldValue(): string
77
    {
78 3
        return sprintf(
79 3
            'bytes %d-%d/%d',
80 3
            $this->firstBytePosition,
81 3
            $this->lastBytePosition,
82 3
            $this->instanceLength
83 3
        );
84
    }
85
86
    /**
87
     * Validates the given range
88
     *
89
     * @param int $firstBytePosition
90
     * @param int $lastBytePosition
91
     * @param int $instanceLength
92
     *
93
     * @return void
94
     *
95
     * @throws InvalidHeaderValueException
96
     *         If the range isn't valid.
97
     */
98 8
    private function validateRange(int $firstBytePosition, int $lastBytePosition, int $instanceLength): void
99
    {
100 8
        if (! ($firstBytePosition <= $lastBytePosition)) {
101 1
            throw new InvalidHeaderValueException(
102 1
                'The "first-byte-pos" value of the content range ' .
103 1
                'must be less than or equal to the "last-byte-pos" value'
104 1
            );
105
        }
106
107 7
        if (! ($lastBytePosition < $instanceLength)) {
108 2
            throw new InvalidHeaderValueException(
109 2
                'The "last-byte-pos" value of the content range ' .
110 2
                'must be less than the "instance-length" value'
111 2
            );
112
        }
113
    }
114
}
115