Test Failed
Push — master ( 5ff3d7...670d03 )
by
05:03
created

OmiseApi::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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