Completed
Push — master ( ba407f...6dbf2d )
by Chris
02:29
created

Service::setTypeId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
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
     * @throws InvalidValueException
17
     */
18
    private function setTypeId(int $typeId): void
19
    {
20
        if ($typeId < 0 || $typeId > 255) {
21
            throw new InvalidValueException("Service type ID {$typeId} outside allowable range of 0 - 255");
22
        }
23
24
        $this->typeId = $typeId;
25
    }
26
27
    /**
28
     * @param int $port
29
     * @throws InvalidValueException
30
     */
31 View Code Duplication
    private function setPort(int $port): void
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...
32
    {
33
        // Protocol spec states this is a uint32 rather than a uint16, so allow any uint32 value at the protocol level
34
        if ($port < UINT32_MIN || $port > UINT32_MAX) {
35
            throw new InvalidValueException(
36
                "Port {$port} outside allowable range of " . UINT32_MIN . " - " . UINT32_MAX
37
            );
38
        }
39
40
        $this->port = $port;
41
    }
42
43
    /**
44
     * @param int $typeId
45
     * @param int $port
46
     * @throws InvalidValueException
47
     */
48
    public function __construct(int $typeId, int $port)
49
    {
50
        $this->setTypeId($typeId);
51
        $this->setPort($port);
52
    }
53
54
    public function getTypeId(): int
55
    {
56
        return $this->typeId;
57
    }
58
59
    public function getPort(): int
60
    {
61
        return $this->port;
62
    }
63
64
    public function getName(): string
65
    {
66
        try {
67
            return ServiceTypes::parseValue($this->typeId);
68
        } catch (\InvalidArgumentException $e) {
69
            return "Unknown({$this->typeId})";
70
        }
71
    }
72
}
73