Completed
Push — master ( 8e0025...04518a )
by
05:55
created

OmiseApi::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
namespace PhpMob\Omise;
13
14
use PhpMob\Omise\Api\Account as AccountApi;
15
use PhpMob\Omise\Api\Charge as ChargeApi;
16
use PhpMob\Omise\Api\Token as TokenApi;
17
use PhpMob\Omise\Client\HttpClientInterface;
18
use PhpMob\Omise\Facade\Account;
19
use PhpMob\Omise\Facade\Charge;
20
use PhpMob\Omise\Facade\Token;
21
use PhpMob\Omise\Hydrator\FacadeHydration;
22
use PhpMob\Omise\Hydrator\HydrationInterface;
23
24
/**
25
 * @author Ishmael Doss <[email protected]>
26
 */
27
class OmiseApi
28
{
29
    const VERSION = '1.0.0-beta1';
30
    const OMISE_ENDPOINT = 'https://api.omise.co/';
31
    const OMISE_VAULT_ENDPOINT = 'https://vault.omise.co/';
32
    const OMISE_VERSION = '2015-11-17';
33
34
    /**
35
     * @var HttpClientInterface
36
     */
37
    private $httpClient;
38
39
    /**
40
     * @var array
41
     */
42
    private $options;
43
44
    /**
45
     * @var HydrationInterface
46
     */
47
    private $hydration;
48
49
    /**
50
     * @var array
51
     */
52
    private static $supports = [
53
        Account::class => AccountApi::class,
54
        Charge::class => ChargeApi::class,
55
        Token::class => TokenApi::class,
56
    ];
57
58
    /**
59
     * @param HttpClientInterface $httpClient
60
     * @param array $options
61
     * @param HydrationInterface $hydration
62
     */
63
    public function __construct(HttpClientInterface $httpClient, array $options, HydrationInterface $hydration = null)
64
    {
65
        $this->httpClient = $httpClient;
66
        $this->options = $options;
67
        $this->hydration = $hydration;
68
    }
69
70
    /**
71
     * @param $apiClass
72
     *
73
     * @return Api
74
     */
75
    public function create($apiClass)
76
    {
77
        if (!in_array(Api::class, class_parents($apiClass))) {
78
            throw new \LogicException("The api class ($apiClass) should have sub-type of ".Api::class);
79
        }
80
81
        return new $apiClass($this->httpClient, $this->options, $this->hydration);
82
    }
83
84
    /**
85
     * Static use setup.
86
     *
87
     * @param HttpClientInterface $httpClient
88
     * @param array $options
89
     */
90
    public static function setupFacade(HttpClientInterface $httpClient, array $options)
91
    {
92
        $self = new self($httpClient, $options, new FacadeHydration());
93
94
        /**
95
         * @var Facade $domainClass
96
         * @var Api $apiClass
97
         */
98
        foreach (self::$supports as $domainClass => $apiClass) {
99
            if (!in_array(Facade::class, class_parents($domainClass))) {
100
                throw new \LogicException("The domain class ($domainClass) should have sub-type of ".Facade::class);
101
            }
102
103
            $domainClass::setApi($self->create($apiClass));
104
        }
105
    }
106
}
107