Port::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\Uri\Component;
13
14
use Sunrise\Http\Message\Exception\InvalidArgumentException;
15
16
use function is_int;
17
18
/**
19
 * @link https://tools.ietf.org/html/rfc3986#section-3.2.3
20
 */
21
final class Port implements ComponentInterface
22
{
23
    private const MIN_VALUE = 1;
24
    private const MAX_VALUE = (2 ** 16) - 1;
25
26
    private ?int $value = null;
27
28
    /**
29
     * @param mixed $value
30
     *
31
     * @throws InvalidArgumentException
32
     */
33 55
    public function __construct($value)
34
    {
35 55
        if ($value === null) {
36 2
            return;
37
        }
38
39 55
        if (!is_int($value)) {
40 8
            throw new InvalidArgumentException('URI component "port" must be an integer');
41
        }
42
43 47
        if (!($value >= self::MIN_VALUE && $value <= self::MAX_VALUE)) {
44 3
            throw new InvalidArgumentException('Invalid URI component "port"');
45
        }
46
47 47
        $this->value = $value;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @return int|null
54
     */
55 47
    public function getValue(): ?int
56
    {
57 47
        return $this->value;
58
    }
59
}
60