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

RefreshHeader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
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 Psr\Http\Message\UriInterface;
18
use Sunrise\Http\Message\Exception\InvalidHeaderValueException;
19
use Sunrise\Http\Message\Exception\InvalidUriException;
20
use Sunrise\Http\Message\Header;
21
use Sunrise\Http\Message\Uri;
22
23
/**
24
 * Import functions
25
 */
26
use function sprintf;
27
28
/**
29
 * @link https://en.wikipedia.org/wiki/Meta_refresh
30
 */
31
class RefreshHeader extends Header
32
{
33
34
    /**
35
     * @var int
36
     */
37
    private int $delay;
38
39
    /**
40
     * @var UriInterface
41
     */
42
    private UriInterface $uri;
43
44
    /**
45
     * Constructor of the class
46
     *
47
     * @param int $delay
48
     * @param mixed $uri
49
     *
50
     * @throws InvalidUriException
51
     *         If the URI isn't valid.
52
     *
53
     * @throws InvalidHeaderValueException
54
     *         If the delay isn't valid.
55
     */
56 6
    public function __construct(int $delay, $uri)
57
    {
58 6
        $this->validateDelay($delay);
59
60 5
        $uri = Uri::create($uri);
61
62 5
        $this->delay = $delay;
63 5
        $this->uri = $uri;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 4
    public function getFieldName(): string
70
    {
71 4
        return 'Refresh';
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 3
    public function getFieldValue(): string
78
    {
79 3
        return sprintf('%d; url=%s', $this->delay, $this->uri->__toString());
80
    }
81
82
    /**
83
     * Validates the redirection delay
84
     *
85
     * @param int $delay
86
     *
87
     * @return void
88
     *
89
     * @throws InvalidHeaderValueException
90
     *         If the delay isn't valid.
91
     */
92 6
    private function validateDelay(int $delay): void
93
    {
94 6
        if (! ($delay >= 0)) {
95 1
            throw new InvalidHeaderValueException(sprintf(
96 1
                'The delay "%2$d" for the header "%1$s" is not valid',
97 1
                $this->getFieldName(),
98 1
                $delay
99 1
            ));
100
        }
101
    }
102
}
103