Passed
Pull Request — master (#27)
by Anatoly
39:10
created

Port   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 5
A getValue() 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\Uri\Component;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Exception\InvalidUriComponentException;
18
19
/**
20
 * Import functions
21
 */
22
use function is_int;
23
24
/**
25
 * URI component "port"
26
 *
27
 * @link https://tools.ietf.org/html/rfc3986#section-3.2.3
28
 */
29
final class Port implements ComponentInterface
30
{
31
32
    /**
33
     * The component value
34
     *
35
     * @var int|null
36
     */
37
    private ?int $value = null;
38
39
    /**
40
     * Constructor of the class
41
     *
42
     * @param mixed $value
43
     *
44
     * @throws InvalidUriComponentException
45
     *         If the component isn't valid.
46
     */
47 57
    public function __construct($value)
48
    {
49 57
        $min = 1;
50 57
        $max = (2 ** 16) - 1;
51
52 57
        if ($value === null) {
53 2
            return;
54
        }
55
56 57
        if (!is_int($value)) {
57 8
            throw new InvalidUriComponentException('URI component "port" must be an integer');
58
        }
59
60 49
        if (!($value >= $min && $value <= $max)) {
61 3
            throw new InvalidUriComponentException('Invalid URI component "port"');
62
        }
63
64 49
        $this->value = $value;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @return int|null
71
     */
72 49
    public function getValue(): ?int
73
    {
74 49
        return $this->value;
75
    }
76
}
77