UniqueServiceName::fromString()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 44
Code Lines 27

Duplication

Lines 15
Ratio 34.09 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 44
ccs 0
cts 25
cp 0
rs 5.3846
cc 8
eloc 27
nc 7
nop 1
crap 72
1
<?php
2
/**
3
 * This file is part of the Ssdp project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ssdp;
9
10
use Ramsey\Uuid\Uuid;
11
12
/**
13
 * Unique service name
14
 *
15
 * @package GravityMedia\Ssdp
16
 */
17
class UniqueServiceName extends AbstractIdentifier
18
{
19
    /**
20
     * Create unique service name from string.
21
     *
22
     * @param string $uniqueServiceName
23
     *
24
     * @return self
25
     */
26
    public static function fromString($uniqueServiceName)
27
    {
28
        $instance = new static();
29
        $uniqueServiceName = strtolower(trim($uniqueServiceName));
30
31
        $parts = explode('::', $uniqueServiceName, 2);
32
        if (!substr($parts[0], 0, 4) === 'uuid') {
33
            throw new \InvalidArgumentException('Invalid string.');
34
        }
35
36
        $instance->setId(Uuid::fromString(substr($parts[0], 5)));
37
        if (count($parts) < 2) {
38
            return $instance;
39
        }
40
41
        if ($parts[1] === 'upnp:rootdevice') {
42
            return $instance->setRootDevice(true);
43
        }
44
45
        $parts = explode(':', $parts[1], 5);
46 View Code Duplication
        if (5 !== count($parts) || 'urn' !== array_shift($parts)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            throw new \InvalidArgumentException('Invalid string.');
48
        }
49
50
        $instance->setDomainName(array_shift($parts));
51
52
        $name = array_shift($parts);
53
54 View Code Duplication
        if ('device' === $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            return $instance
56
                ->setDevice(true)
57
                ->setType(array_shift($parts))
58
                ->setVersion((int)array_shift($parts));
59
        }
60
61 View Code Duplication
        if ('service' === $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
            return $instance
63
                ->setService(true)
64
                ->setType(array_shift($parts))
65
                ->setVersion((int)array_shift($parts));
66
        }
67
68
        throw new \InvalidArgumentException('Invalid string.');
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function toString()
75
    {
76
        $id = $this->getId();
77
        if (null === $id) {
78
            $id = Uuid::fromString(Uuid::NIL);
79
        }
80
81
        if ($this->isRootDevice()) {
82
            return sprintf('uuid:%s::upnp:rootdevice', $id);
83
        }
84
85
        if ($this->isDevice()) {
86
            return sprintf(
87
                'uuid:%s::urn:%s:device:%s:%u',
88
                $id,
89
                $this->getDomainName(),
90
                $this->getType(),
91
                $this->getVersion()
92
            );
93
        }
94
95
        if ($this->isService()) {
96
            return sprintf(
97
                'uuid:%s::urn:%s:service:%s:%u',
98
                $id,
99
                $this->getDomainName(),
100
                $this->getType(),
101
                $this->getVersion()
102
            );
103
        }
104
105
        return sprintf('uuid:%s', $id);
106
    }
107
}
108