RangeSetFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 22
ccs 8
cts 8
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Value\Range;
6
7
use InvalidArgumentException;
8
use Stadly\Http\Utilities\Rfc7233;
9
10
/**
11
 * Class for generating range sets.
12
 */
13
final class RangeSetFactory
14
{
15
    /**
16
     * Construct range set from string.
17
     *
18
     * @param string $rangeSet Range set string.
19
     * @return RangeSet Range set generated based on the string.
20
     */
21 8
    public static function fromString(string $rangeSet): RangeSet
22
    {
23 8
        $plainRangeSet = mb_convert_encoding($rangeSet, 'ISO-8859-1', 'UTF-8');
24 8
        if ($plainRangeSet === $rangeSet) {
25 8
            if (preg_match('{^' . Rfc7233::BYTE_RANGES_SPECIFIER . '$}', $rangeSet) === 1) {
26 2
                return ByteRangeSet::fromString($rangeSet);
27
            }
28
29 6
            if (preg_match('{^' . Rfc7233::OTHER_RANGES_SPECIFIER . '$}', $rangeSet) === 1) {
30 1
                return OtherRangeSet::fromString($rangeSet);
31
            }
32
        }
33
34 5
        throw new InvalidArgumentException('Invalid set of ranges: ' . $rangeSet);
35
    }
36
}
37