Completed
Push — master ( dac7df...b079db )
by Daniel
03:09
created

NotificationType::toString()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 0
cts 12
cp 0
rs 8.7624
cc 5
eloc 11
nc 5
nop 0
crap 30
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
 * Notification type
14
 *
15
 * @package GravityMedia\Ssdp
16
 */
17
class NotificationType extends AbstractIdentifier
18
{
19
    /**
20
     * Create notification type from string.
21
     *
22
     * @param string $searchTarget
23
     *
24
     * @return self
25
     */
26
    public static function fromString($searchTarget)
27
    {
28
        $instance = new static();
29
        $searchTarget = strtolower(trim($searchTarget));
30
31
        if ($searchTarget === 'upnp:rootdevice') {
32
            return $instance->setRootDevice(true);
33
        }
34
35
        if (substr($searchTarget, 0, 4) === 'upnp') {
36
            return $instance->setId(Uuid::fromString(substr($searchTarget, 5)));
37
        }
38
39
        $parts = explode(':', $searchTarget, 5);
40 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...
41
            throw new \InvalidArgumentException('Invalid string.');
42
        }
43
44
        $instance->setDomainName(array_shift($parts));
45
46
        $name = array_shift($parts);
47
48 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...
49
            return $instance
50
                ->setDevice(true)
51
                ->setType(array_shift($parts))
52
                ->setVersion((int)array_shift($parts));
53
        }
54
55 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...
56
            return $instance
57
                ->setService(true)
58
                ->setType(array_shift($parts))
59
                ->setVersion((int)array_shift($parts));
60
        }
61
62
        throw new \InvalidArgumentException('Invalid string.');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function toString()
69
    {
70
        if ($this->isRootDevice()) {
71
            return 'upnp:rootdevice';
72
        }
73
74
        if ($this->isDevice()) {
75
            return sprintf('urn:%s:device:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion());
76
        }
77
78
        if ($this->isService()) {
79
            return sprintf('urn:%s:service:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion());
80
        }
81
82
        $id = $this->getId();
83
        if (null === $id) {
84
            $id = Uuid::fromString(Uuid::NIL);
85
        }
86
87
        return sprintf('uuid:%s', $id);
88
    }
89
}
90