Passed
Push — master ( cf11fa...b87bd4 )
by
04:04
created

OmiseApi   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 98
ccs 14
cts 18
cp 0.7778
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 8 2
A setupFacade() 0 16 3
A __get() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpMob\Omise;
15
16
use PhpMob\Omise\Client\HttpClientInterface;
17
use PhpMob\Omise\Hydrator\FacadeHydration;
18
use PhpMob\Omise\Hydrator\HydrationInterface;
19
20
/**
21
 * @author Ishmael Doss <[email protected]>
22
 *
23
 * @property Api\Charge charge
24
 */
25
class OmiseApi
26
{
27
    const VERSION = '1.0.0-beta1';
28
    const OMISE_ENDPOINT = 'https://api.omise.co/';
29
    const OMISE_VAULT_ENDPOINT = 'https://vault.omise.co/';
30
    const OMISE_VERSION = '2015-11-17';
31
32
    /**
33
     * @var HttpClientInterface
34
     */
35
    private $httpClient;
36
37
    /**
38
     * @var array
39
     */
40
    private $options;
41
42
    /**
43
     * @var HydrationInterface
44
     */
45
    private $hydration;
46
47
    /**
48
     * @var Api[]
49
     */
50
    private static $supports = [
51
        Facade\Account::class => Api\Account::class,
52
        Facade\Balance::class => Api\Balance::class,
53
        Facade\Charge::class => Api\Charge::class,
54
        Facade\Customer::class => Api\Customer::class,
55
        Facade\Dispute::class => Api\Dispute::class,
56
        Facade\Receipt::class => Api\Receipt::class,
57
        Facade\Recipient::class => Api\Recipient::class,
58
        Facade\Refund::class => Api\Refund::class,
59
        Facade\Token::class => Api\Token::class,
60
        Facade\Transaction::class => Api\Transaction::class,
61
        Facade\Transfer::class => Api\Transfer::class,
62
    ];
63
64
    /**
65
     * @param HttpClientInterface $httpClient
66
     * @param array $options
67
     * @param HydrationInterface $hydration
68
     */
69 62
    public function __construct(HttpClientInterface $httpClient, array $options, HydrationInterface $hydration = null)
70
    {
71 62
        $this->httpClient = $httpClient;
72 62
        $this->options = $options;
73 62
        $this->hydration = $hydration;
74 62
    }
75
76
    /**
77
     * @param $apiClass
78
     *
79
     * @return Api
80
     */
81 62
    public function create($apiClass)
82
    {
83 62
        if (!in_array(Api::class, class_parents($apiClass))) {
84
            throw new \LogicException("The api class ($apiClass) should have sub-type of " . Api::class);
85
        }
86
87 62
        return new $apiClass($this->httpClient, $this->options, $this->hydration);
88
    }
89
90
    /**
91
     * Static use setup.
92
     *
93
     * @param HttpClientInterface $httpClient
94
     * @param array $options
95
     */
96 62
    public static function setupFacade(HttpClientInterface $httpClient, array $options)
97
    {
98 62
        $self = new self($httpClient, $options, new FacadeHydration());
99
100
        /**
101
         * @var Facade $domainClass
102
         * @var Api $apiClass
103
         */
104 62
        foreach (self::$supports as $domainClass => $apiClass) {
105 62
            if (!in_array(Facade::class, class_parents($domainClass))) {
106
                throw new \LogicException("The domain class ($domainClass) should have sub-type of " . Facade::class);
107
            }
108
109 62
            $domainClass::setApi($self->create($apiClass));
110
        }
111 62
    }
112
113
    /**
114
     * @param string $name
115
     *
116
     * @return Api
117
     */
118
    public function __get($name)
119
    {
120
        return $this->create(__NAMESPACE__ . "\\Api\\" . ucfirst($name));
121
    }
122
}
123