1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\NestApi\Http\Endpoint; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use LauLamanApps\NestApi\Http\Endpoint\Exception\EndpointCouldNotBeMappedException; |
9
|
|
|
|
10
|
|
|
final class Production implements MapperInterface |
11
|
|
|
{ |
12
|
|
|
public const BASE_URL = 'https://developer-api.nest.com'; |
13
|
|
|
private const THERMOSTATS_ENDPOINT = '/devices/thermostats'; |
14
|
|
|
private const THERMOSTAT_ENDPOINT = '/devices/thermostats/%s'; |
15
|
|
|
private const PUT_THERMOSTAT_ENDPOINT = '/devices/thermostats/%s'; |
16
|
|
|
private const PROTECTS_ENDPOINT = '/devices/smoke_co_alarms'; |
17
|
|
|
private const PROTECT_ENDPOINT = '/devices/smoke_co_alarms/%s'; |
18
|
|
|
private const CAMERAS_ENDPOINT = '/devices/cameras'; |
19
|
|
|
private const CAMERA_ENDPOINT = '/devices/cameras/%s'; |
20
|
|
|
private const STRUCTURES_ENDPOINT = '/structures'; |
21
|
|
|
private const STRUCTURE_ENDPOINT = '/structures/%s'; |
22
|
|
|
|
23
|
|
|
private $map = []; |
24
|
|
|
|
25
|
10 |
|
public function __construct() |
26
|
|
|
{ |
27
|
10 |
|
$this->map = [ |
28
|
10 |
|
MapperInterface::THERMOSTATS => self::BASE_URL . self::THERMOSTATS_ENDPOINT, |
29
|
10 |
|
MapperInterface::THERMOSTAT => self::BASE_URL . self::THERMOSTAT_ENDPOINT, |
30
|
10 |
|
MapperInterface::THERMOSTAT_PUT => self::BASE_URL . self::PUT_THERMOSTAT_ENDPOINT, |
31
|
10 |
|
MapperInterface::PROTECTS => self::BASE_URL . self::PROTECTS_ENDPOINT, |
32
|
10 |
|
MapperInterface::PROTECT => self::BASE_URL . self::PROTECT_ENDPOINT, |
33
|
10 |
|
MapperInterface::CAMERAS => self::BASE_URL . self::CAMERAS_ENDPOINT, |
34
|
10 |
|
MapperInterface::CAMERA => self::BASE_URL . self::CAMERA_ENDPOINT, |
35
|
10 |
|
MapperInterface::STRUCTURES => self::BASE_URL . self::STRUCTURES_ENDPOINT, |
36
|
10 |
|
MapperInterface::STRUCTURE => self::BASE_URL . self::STRUCTURE_ENDPOINT, |
37
|
|
|
]; |
38
|
10 |
|
} |
39
|
|
|
|
40
|
10 |
|
public function map(string $key, ?array $bits = []): string |
41
|
|
|
{ |
42
|
10 |
|
if (array_key_exists($key, $this->map)) { |
43
|
9 |
|
$url = $this->map[$key]; |
44
|
|
|
} else { |
45
|
1 |
|
throw new EndpointCouldNotBeMappedException(sprintf('key \'%s\' could not be mapped to an URL', $key)); |
46
|
|
|
} |
47
|
|
|
|
48
|
9 |
|
return sprintf($url, ...$bits); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|