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
|
|
|
|