HeaderFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 17 6
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\Common\HeaderFactory as CommonHeaderFactory;
9
use Stadly\Http\Utilities\Rfc7230;
10
11
/**
12
 * Class for generating HTTP request header fields.
13
 */
14
final class HeaderFactory
15
{
16
    /**
17
     * Construct header from string.
18
     *
19
     * @param string $header Header field string.
20
     * @return Header Header generated based on the string.
21
     * @throws InvalidHeader If the header is invalid.
22
     */
23 22
    public static function fromString(string $header): Header
24
    {
25 22
        $regEx = '{^' . Rfc7230::HEADER_FIELD_CAPTURE . '$}';
26 22
        $plainHeader = mb_convert_encoding($header, 'ISO-8859-1', 'UTF-8');
27 22
        if ($plainHeader !== $header || preg_match($regEx, $header, $matches) !== 1) {
28 3
            throw new InvalidHeader('Invalid header field: ' . $header);
29
        }
30
31 19
        switch (strtolower($matches['FIELD_NAME'])) {
32 19
            case 'if-match':
33 3
                return IfMatch::fromValue($matches['FIELD_VALUE']);
34 16
            case 'if-none-match':
35 3
                return IfNoneMatch::fromValue($matches['FIELD_VALUE']);
36 13
            case 'range':
37 3
                return Range::fromValue($matches['FIELD_VALUE']);
38
            default:
39 10
                return CommonHeaderFactory::fromString($header);
40
        }
41
    }
42
}
43