OmiseApi   A
last analyzed

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
 * @property Api\Source source
25
 */
26
class OmiseApi
27
{
28
    const VERSION = '1.0.0-beta1';
29
    const OMISE_ENDPOINT = 'https://api.omise.co/';
30
    const OMISE_VAULT_ENDPOINT = 'https://vault.omise.co/';
31
    const OMISE_VERSION = '2017-11-02';
32
33
    /**
34
     * @var HttpClientInterface
35
     */
36
    private $httpClient;
37
38
    /**
39
     * @var array
40
     */
41
    private $options;
42
43
    /**
44
     * @var HydrationInterface
45
     */
46
    private $hydration;
47
48
    /**
49
     * @var Api[]
50
     */
51
    private static $supports = [
52
        Facade\Account::class => Api\Account::class,
53
        Facade\Balance::class => Api\Balance::class,
54
        Facade\Charge::class => Api\Charge::class,
55
        Facade\Customer::class => Api\Customer::class,
56
        Facade\Dispute::class => Api\Dispute::class,
57
        Facade\Receipt::class => Api\Receipt::class,
58
        Facade\Recipient::class => Api\Recipient::class,
59
        Facade\Refund::class => Api\Refund::class,
60
        Facade\Token::class => Api\Token::class,
61
        Facade\Transaction::class => Api\Transaction::class,
62
        Facade\Transfer::class => Api\Transfer::class,
63
    ];
64
65
    /**
66
     * @param HttpClientInterface $httpClient
67
     * @param array $options
68
     * @param HydrationInterface $hydration
69 62
     */
70
    public function __construct(HttpClientInterface $httpClient, array $options, HydrationInterface $hydration = null)
71 62
    {
72 62
        $this->httpClient = $httpClient;
73 62
        $this->options = $options;
74 62
        $this->hydration = $hydration;
75
    }
76
77
    /**
78
     * @param $apiClass
79
     *
80
     * @return Api
81 62
     */
82
    public function create($apiClass)
83 62
    {
84
        if (!in_array(Api::class, class_parents($apiClass))) {
85
            throw new \LogicException("The api class ($apiClass) should have sub-type of " . Api::class);
86
        }
87 62
88
        return new $apiClass($this->httpClient, $this->options, $this->hydration);
89
    }
90
91
    /**
92
     * Static use setup.
93
     *
94
     * @param HttpClientInterface $httpClient
95
     * @param array $options
96 62
     */
97
    public static function setupFacade(HttpClientInterface $httpClient, array $options)
98 62
    {
99
        $self = new self($httpClient, $options, new FacadeHydration());
100
101
        /**
102
         * @var Facade $domainClass
103
         * @var Api $apiClass
104 62
         */
105 62
        foreach (self::$supports as $domainClass => $apiClass) {
106
            if (!in_array(Facade::class, class_parents($domainClass))) {
107
                throw new \LogicException("The domain class ($domainClass) should have sub-type of " . Facade::class);
108
            }
109 62
110
            $domainClass::setApi($self->create($apiClass));
111 62
        }
112
    }
113
114
    /**
115
     * @param string $name
116
     *
117
     * @return Api
118
     */
119
    public function __get($name)
120
    {
121
        return $this->create(__NAMESPACE__ . "\\Api\\" . ucfirst($name));
122
    }
123
}
124