Production::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Http\Endpoint;
6
7
use Exception;
8
use LauLamanApps\eCurring\Http\Endpoint\Exception\EndpointCouldNotBeMappedException;
9
10
final class Production implements MapperInterface
11
{
12
    public const BASE_URL = 'https://api.ecurring.com';
13
14
15
    public const GET_CUSTOMERS_ENDPOINT = '/customers';
16
    public const GET_CUSTOMER_ENDPOINT = '/customers/%s';
17
    public const POST_CUSTOMER_ENDPOINT = '/customers';
18
    public const PATCH_CUSTOMER_ENDPOINT = '/customers/%s';
19
20
    public const GET_SUBSCRIPTION_PLANS_ENDPOINT = '/subscription-plans';
21
    public const GET_SUBSCRIPTION_PLAN_ENDPOINT = '/subscription-plans/%s';
22
23
    public const GET_SUBSCRIPTIONS_ENDPOINT = '/subscriptions';
24
    public const GET_SUBSCRIPTION_ENDPOINT = '/subscriptions/%s';
25
    public const POST_SUBSCRIPTION_ENDPOINT = '/subscriptions';
26
    public const PATCH_SUBSCRIPTION_ENDPOINT = '/subscriptions/%s';
27
    public const GET_SUBSCRIPTION_TRANSACTIONS_ENDPOINT = 'subscriptions/%s/transactions';
28
29
    public const GET_TRANSACTION_ENDPOINT = '/transactions/%s';
30
    public const POST_TRANSACTION_ENDPOINT = '/transactions';
31
    public const DELETE_TRANSACTION_ENDPOINT = '/transactions/%s';
32
33
    private $map = [];
34
35
    public function __construct()
36
    {
37
        $this->map = [
38
            MapperInterface::GET_CUSTOMERS => self::BASE_URL . self::GET_CUSTOMERS_ENDPOINT,
39
            MapperInterface::GET_CUSTOMER => self::BASE_URL . self::GET_CUSTOMER_ENDPOINT,
40
            MapperInterface::POST_CUSTOMER => self::BASE_URL . self::POST_CUSTOMER_ENDPOINT,
41
            MapperInterface::PATCH_CUSTOMER => self::BASE_URL . self::PATCH_CUSTOMER_ENDPOINT,
42
43
            MapperInterface::GET_SUBSCRIPTION_PLANS => self::BASE_URL . self::GET_SUBSCRIPTION_PLANS_ENDPOINT,
44
            MapperInterface::GET_SUBSCRIPTION_PLAN => self::BASE_URL . self::GET_SUBSCRIPTION_PLAN_ENDPOINT,
45
46
            MapperInterface::GET_SUBSCRIPTIONS => self::BASE_URL . self::GET_SUBSCRIPTIONS_ENDPOINT,
47
            MapperInterface::GET_SUBSCRIPTION => self::BASE_URL . self::GET_SUBSCRIPTION_ENDPOINT,
48
            MapperInterface::POST_SUBSCRIPTION => self::BASE_URL . self::POST_SUBSCRIPTION_ENDPOINT,
49
            MapperInterface::PATCH_SUBSCRIPTION => self::BASE_URL . self::PATCH_SUBSCRIPTION_ENDPOINT,
50
            MapperInterface::GET_SUBSCRIPTION_TRANSACTIONS => self::BASE_URL . self::GET_SUBSCRIPTION_TRANSACTIONS_ENDPOINT,
51
52
            MapperInterface::GET_TRANSACTION => self::BASE_URL . self::GET_TRANSACTION_ENDPOINT,
53
            MapperInterface::POST_TRANSACTION => self::BASE_URL . self::POST_TRANSACTION_ENDPOINT,
54
            MapperInterface::DELETE_TRANSACTION => self::BASE_URL . self::DELETE_TRANSACTION_ENDPOINT,
55
        ];
56
    }
57
58
    public function map(string $key, ?array $bits = []): string
59
    {
60
        if (array_key_exists($key, $this->map)) {
61
            $url = $this->map[$key];
62
        } else {
63
            throw new EndpointCouldNotBeMappedException(sprintf('key \'%s\' could not be mapped to an URL', $key));
64
        }
65
66
        $bits = $bits ?? [];
67
68
        return sprintf($url, ...$bits);
69
    }
70
}
71