Passed
Push — master ( 6248da...8ce640 )
by László
02:40
created

ContentRange::isFirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
namespace CodingSocks\UploadHandler\Range;
4
5
use CodingSocks\UploadHandler\Exception\RequestEntityTooLargeHttpException;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use InvalidArgumentException;
9
use Symfony\Component\HttpFoundation\HeaderBag;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
12
class ContentRange implements Range
13
{
14
    /**
15
     * @var float
16
     */
17
    protected $start;
18
19
    /**
20
     * @var float
21
     */
22
    protected $end;
23
24
    /**
25
     * @var float
26
     */
27
    protected $total;
28
29
    /**
30
     * ContentRange constructor.
31
     *
32
     * @param string|\Symfony\Component\HttpFoundation\HeaderBag|\Illuminate\Http\Request $contentRange
33
     */
34 20
    public function __construct($contentRange)
35
    {
36 20
        if ($contentRange instanceof Request) {
37 1
            $contentRange = $contentRange->header('content-range');
38 19
        } elseif ($contentRange instanceof HeaderBag) {
39 5
            $contentRange = $contentRange->get('content-range');
40
        }
41
42 20
        if (preg_match('#bytes (\d+)-(\d+)/(\d+)#', $contentRange, $matches) !== 1) {
43 3
            throw new InvalidArgumentException('Content Range header is missing or invalid');
44
        }
45
46 17
        $this->start = $this->numericValue($matches[1]);
47 17
        $this->end = $this->numericValue($matches[2]);
48 17
        $this->total = $this->numericValue($matches[3]);
49
50 16
        if ($this->end < $this->start) {
51 1
            throw new InvalidArgumentException('Range end must be greater than or equal to range start');
52
        }
53 15
        if ($this->total <= $this->end) {
54 2
            throw new InvalidArgumentException('Size must be greater than range end');
55
        }
56 13
    }
57
58
    /**
59
     * Converts the string value to float - throws exception if float value is exceeded.
60
     *
61
     * @param string $value
62
     *
63
     * @return float
64
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
65
     */
66 17
    protected function numericValue($value): float
67
    {
68 17
        $floatVal = floatval($value);
69
70 17
        if ($floatVal === INF) {
71 1
            throw new RequestEntityTooLargeHttpException('The content range value is too large');
72
        }
73
74 17
        return $floatVal;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 7
    public function getStart(): float
81
    {
82 7
        return $this->start;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 7
    public function getEnd(): float
89
    {
90 7
        return $this->end;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 7
    public function getTotal(): float
97
    {
98 7
        return $this->total;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 2
    public function isFirst(): bool
105
    {
106 2
        return $this->start === 0.0;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 6
    public function isLast(): bool
113
    {
114 6
        return $this->end >= ($this->total - 1);
115
    }
116
117
    /**
118
     * @return float
119
     */
120 5
    public function getPercentage(): float
121
    {
122 5
        return floor(($this->end + 1) / $this->total * 100);
123
    }
124
}
125