Passed
Pull Request — master (#1)
by
unknown
02:15
created

Production::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
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 1
    public function __construct()
36
    {
37 1
        $this->map = [
38 1
            MapperInterface::GET_CUSTOMERS => self::BASE_URL . self::GET_CUSTOMERS_ENDPOINT,
39 1
            MapperInterface::GET_CUSTOMER => self::BASE_URL . self::GET_CUSTOMER_ENDPOINT,
40 1
            MapperInterface::POST_CUSTOMER => self::BASE_URL . self::POST_CUSTOMER_ENDPOINT,
41 1
            MapperInterface::PATCH_CUSTOMER => self::BASE_URL . self::PATCH_CUSTOMER_ENDPOINT,
42
43 1
            MapperInterface::GET_SUBSCRIPTION_PLANS => self::BASE_URL . self::GET_SUBSCRIPTION_PLANS_ENDPOINT,
44 1
            MapperInterface::GET_SUBSCRIPTION_PLAN => self::BASE_URL . self::GET_SUBSCRIPTION_PLAN_ENDPOINT,
45
46 1
            MapperInterface::GET_SUBSCRIPTIONS => self::BASE_URL . self::GET_SUBSCRIPTIONS_ENDPOINT,
47 1
            MapperInterface::GET_SUBSCRIPTION => self::BASE_URL . self::GET_SUBSCRIPTION_ENDPOINT,
48 1
            MapperInterface::POST_SUBSCRIPTION => self::BASE_URL . self::POST_SUBSCRIPTION_ENDPOINT,
49 1
            MapperInterface::PATCH_SUBSCRIPTION => self::BASE_URL . self::PATCH_SUBSCRIPTION_ENDPOINT,
50 1
            MapperInterface::GET_SUBSCRIPTION_TRANSACTIONS => self::BASE_URL . self::GET_SUBSCRIPTION_TRANSACTIONS_ENDPOINT,
51
52 1
            MapperInterface::GET_TRANSACTION => self::BASE_URL . self::GET_TRANSACTION_ENDPOINT,
53 1
            MapperInterface::POST_TRANSACTION => self::BASE_URL . self::POST_TRANSACTION_ENDPOINT,
54 1
            MapperInterface::DELETE_TRANSACTION => self::BASE_URL . self::DELETE_TRANSACTION_ENDPOINT,
55
        ];
56 1
    }
57
58 1
    public function map(string $key, ?array $bits = []): string
59
    {
60 1
        if (array_key_exists($key, $this->map)) {
61 1
            $url = $this->map[$key];
62
        } else {
63
            throw new EndpointCouldNotBeMappedException(sprintf('key \'%s\' could not be mapped to an URL', $key));
64
        }
65
66 1
        $bits = $bits ?? [];
67
68 1
        return sprintf($url, ...$bits);
69
    }
70
}
71