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

AliveFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 34
ccs 0
cts 15
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createRequest() 0 22 3
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\Factory;
9
10
use GravityMedia\Ssdp\Options\AliveOptions;
11
use GravityMedia\Ssdp\Request\NotifyRequest;
12
use Psr\Http\Message\RequestInterface;
13
14
/**
15
 * Alive request factory class
16
 *
17
 * @package GravityMedia\Ssdp\Request\Factory
18
 */
19
class AliveFactory implements FactoryInterface
20
{
21
    /**
22
     * Create alive request object
23
     *
24
     * @param AliveOptions $options
25
     *
26
     * @throws \RuntimeException
27
     *
28
     * @return RequestInterface
29
     */
30
    public function createRequest($options)
31
    {
32
        if (!$options instanceof AliveOptions) {
33
            throw new \RuntimeException('Options must be instance of ' . AliveOptions::class);
34
        }
35
36
        if (null === $options->getUniqueServiceName()) {
37
            throw new \RuntimeException('Unique service name not specified.');
38
        }
39
40
        $request = new NotifyRequest();
41
        return $request
42
            ->withRequestTarget('*')
43
            ->withProtocolVersion('1.1')
44
            ->withHeader('HOST', (string)$request->getUri())
45
            ->withHeader('CACHE-CONTROL', sprintf('max-age=%u', $options->getMessageLifetime()))
46
            ->withHeader('LOCATION', (string)$options->getDescriptionUrl())
47
            ->withHeader('NT', (string)$options->getNotificationType())
48
            ->withHeader('NTS', '"ssdp:alive"')
49
            ->withHeader('SERVER', $options->getServerString())
50
            ->withHeader('USN', (string)$options->getUniqueServiceName());
51
    }
52
}
53