Passed
Push — master ( 8634b9...ba407f )
by Chris
14:07
created

Service::getTypeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\DataTypes;
4
5
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException;
6
use const DaveRandom\LibLifxLan\UINT32_MAX;
7
use const DaveRandom\LibLifxLan\UINT32_MIN;
8
9
final class Service
10
{
11
    private $typeId;
12
    private $port;
13
14
    /**
15
     * @param int $typeId
16
     * @param int $port
17
     * @throws InvalidValueException
18
     */
19 View Code Duplication
    public function __construct(int $typeId, int $port)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        if ($typeId < 0 || $typeId > 255) {
22
            throw new InvalidValueException("Service type ID {$typeId} outside allowable range of 0 - 255");
23
        }
24
25
        // Protocol spec states this is a uint32 rather than a uint16, so allow any uint32 value at the protocol level
26
        if ($port < UINT32_MIN || $port > UINT32_MAX) {
27
            throw new InvalidValueException(
28
                "Port {$port} outside allowable range of " . UINT32_MIN . " - " . UINT32_MAX
29
            );
30
        }
31
32
        $this->typeId = $typeId;
33
        $this->port = $port;
34
    }
35
36
    public function getTypeId(): int
37
    {
38
        return $this->typeId;
39
    }
40
41
    public function getPort(): int
42
    {
43
        return $this->port;
44
    }
45
46
    public function getName(): string
47
    {
48
        try {
49
            return ServiceTypes::parseValue($this->typeId);
50
        } catch (\InvalidArgumentException $e) {
51
            return "Unknown({$this->typeId})";
52
        }
53
    }
54
}
55