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

AccessControlMaxAgeHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 7 3
A getFieldValue() 0 3 1
A __construct() 0 5 1
A getFieldName() 0 3 1
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://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
27
 */
28
class AccessControlMaxAgeHeader extends Header
29
{
30
31
    /**
32
     * @var int
33
     */
34
    private int $value;
35
36
    /**
37
     * Constructor of the class
38
     *
39
     * @param int $value
40
     *
41
     * @throws InvalidHeaderValueException
42
     *         If the value isn't valid.
43
     */
44 11
    public function __construct(int $value)
45
    {
46 11
        $this->validateValue($value);
47
48 8
        $this->value = $value;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 6
    public function getFieldName(): string
55
    {
56 6
        return 'Access-Control-Max-Age';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 6
    public function getFieldValue(): string
63
    {
64 6
        return sprintf('%d', $this->value);
65
    }
66
67
    /**
68
     * Validates the given value
69
     *
70
     * @param int $value
71
     *
72
     * @return void
73
     *
74
     * @throws InvalidHeaderValueException
75
     *         If the value isn't valid.
76
     */
77 11
    private function validateValue(int $value): void
78
    {
79 11
        if (! ($value === -1 || $value >= 1)) {
80 3
            throw new InvalidHeaderValueException(sprintf(
81 3
                'The value "%2$d" for the header "%1$s" is not valid.',
82 3
                $this->getFieldName(),
83 3
                $value
84 3
            ));
85
        }
86
    }
87
}
88