Production::map()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 SmokeCoAlarmS_ENDPOINT = '/devices/smoke_co_alarms';
17
    private const SmokeCoAlarm_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::SmokeCoAlarmS => self::BASE_URL . self::SmokeCoAlarmS_ENDPOINT,
32 10
            MapperInterface::SmokeCoAlarm => self::BASE_URL . self::SmokeCoAlarm_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