HeaderFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 15 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Response;
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 response 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 25
    public static function fromString(string $header): Header
24
    {
25 25
        $regEx = '{^' . Rfc7230::HEADER_FIELD_CAPTURE . '$}';
26 25
        $plainHeader = mb_convert_encoding($header, 'ISO-8859-1', 'UTF-8');
27 25
        if ($plainHeader !== $header || preg_match($regEx, $header, $matches) !== 1) {
28 3
            throw new InvalidHeader('Invalid header field: ' . $header);
29
        }
30
31 22
        switch (strtolower($matches['FIELD_NAME'])) {
32 22
            case 'content-disposition':
33 3
                return ContentDisposition::fromValue($matches['FIELD_VALUE']);
34 19
            case 'etag':
35 3
                return ETag::fromValue($matches['FIELD_VALUE']);
36
            default:
37 16
                return CommonHeaderFactory::fromString($header);
38
        }
39
    }
40
}
41