Factory::createDiscoverRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 2
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\Request;
9
10
use GravityMedia\Ssdp\Options\AliveOptions;
11
use GravityMedia\Ssdp\Options\ByebyeOptions;
12
use GravityMedia\Ssdp\Options\DiscoverOptions;
13
use GravityMedia\Ssdp\Options\UpdateOptions;
14
use Psr\Http\Message\RequestInterface;
15
16
/**
17
 * Request factory class
18
 *
19
 * @package GravityMedia\Ssdp\Request\Factory
20
 */
21
class Factory
22
{
23
    /**
24
     * Create alive request object
25
     *
26
     * @param AliveOptions $options
27
     *
28
     * @throws \RuntimeException
29
     *
30
     * @return RequestInterface
31
     */
32
    public function createAliveRequest(AliveOptions $options)
33
    {
34
        if (null === $options->getUniqueServiceName()) {
35
            throw new \RuntimeException('Unique service name not specified.');
36
        }
37
38
        $request = new NotifyRequest();
39
        return $request
40
            ->withRequestTarget('*')
41
            ->withProtocolVersion('1.1')
42
            ->withHeader('HOST', (string)$request->getUri())
43
            ->withHeader('CACHE-CONTROL', sprintf('max-age=%u', $options->getMessageLifetime()))
44
            ->withHeader('LOCATION', (string)$options->getDescriptionUrl())
45
            ->withHeader('NT', (string)$options->getNotificationType())
46
            ->withHeader('NTS', '"ssdp:alive"')
47
            ->withHeader('SERVER', $options->getServerString())
48
            ->withHeader('USN', (string)$options->getUniqueServiceName());
49
    }
50
51
    /**
52
     * Create byebye request object
53
     *
54
     * @param ByebyeOptions $options
55
     *
56
     * @throws \RuntimeException
57
     *
58
     * @return RequestInterface
59
     */
60 View Code Duplication
    public function createByebyeRequest(ByebyeOptions $options)
0 ignored issues
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...
61
    {
62
        if (null === $options->getUniqueServiceName()) {
63
            throw new \RuntimeException('Unique service name not specified.');
64
        }
65
66
        $request = new NotifyRequest();
67
        return $request
68
            ->withRequestTarget('*')
69
            ->withProtocolVersion('1.1')
70
            ->withHeader('HOST', (string)$request->getUri())
71
            ->withHeader('NT', (string)$options->getNotificationType())
72
            ->withHeader('NTS', '"ssdp:byebye"')
73
            ->withHeader('USN', (string)$options->getUniqueServiceName());
74
    }
75
76
77
    /**
78
     * Create discover request object
79
     *
80
     * @param DiscoverOptions $options
81
     *
82
     * @throws \RuntimeException
83
     *
84
     * @return RequestInterface
85
     */
86
    public function createDiscoverRequest(DiscoverOptions $options)
87
    {
88
        $request = new SearchRequest();
89
        return $request
90
            ->withRequestTarget('*')
91
            ->withProtocolVersion('1.1')
92
            ->withHeader('HOST', (string)$request->getUri())
93
            ->withHeader('MAN', '"ssdp:discover"')
94
            ->withHeader('MX', (string)$options->getMaximumWaitTime())
95
            ->withHeader('ST', (string)$options->getSearchTarget());
96
    }
97
98
    /**
99
     * Create update request object
100
     *
101
     * @param UpdateOptions $options
102
     *
103
     * @throws \RuntimeException
104
     *
105
     * @return RequestInterface
106
     */
107 View Code Duplication
    public function createUpdateRequest(UpdateOptions $options)
0 ignored issues
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...
108
    {
109
        if (null === $options->getUniqueServiceName()) {
110
            throw new \RuntimeException('Unique service name not specified.');
111
        }
112
113
        $request = new NotifyRequest();
114
        return $request
115
            ->withRequestTarget('*')
116
            ->withProtocolVersion('1.1')
117
            ->withHeader('HOST', (string)$request->getUri())
118
            ->withHeader('LOCATION', (string)$options->getDescriptionUrl())
119
            ->withHeader('NT', (string)$options->getNotificationType())
120
            ->withHeader('NTS', '"ssdp:update"')
121
            ->withHeader('USN', (string)$options->getUniqueServiceName());
122
    }
123
}
124