Completed
Push — master ( 38612a...1c3c5f )
by Julián
02:35
created

ServiceAbstract::initializeHttpClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace JulianBustamante\Plaid;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use JulianBustamante\Plaid\Exceptions\InvalidEnvironmentException;
8
use JulianBustamante\Plaid\Traits\InstantiatorTrait;
9
10
/**
11
 * Class ServiceAbstract
12
 *
13
 * @package JulianBustamante\Plaid
14
 */
15
abstract class ServiceAbstract
16
{
17
    use InstantiatorTrait;
18
19
    /**
20
     * Plaid API environments.
21
     */
22
    public const ENV_SANDBOX     = 'sandbox';
23
    public const ENV_DEVELOPMENT = 'development';
24
    public const ENV_PRODUCTION  = 'production';
25
    public const URI_SANDBOX     = 'https://sandbox.plaid.com';
26
    public const URI_DEVELOPMENT = 'https://development.plaid.com';
27
    public const URI_PRODUCTION  = 'https://production.plaid.com';
28
29
    public const ENVIRONMENTS = [
30
        self::ENV_SANDBOX     => self::URI_SANDBOX,
31
        self::ENV_DEVELOPMENT => self::URI_DEVELOPMENT,
32
        self::ENV_PRODUCTION  => self::URI_PRODUCTION,
33
    ];
34
35
    /**
36
     * Client used for http requests.
37
     *
38
     * @var \GuzzleHttp\ClientInterface
39
     */
40
    private $client;
41
42
    /**
43
     * Your Plaid client ID.
44
     *
45
     * @var string
46
     */
47
    private $client_id;
48
49
    /**
50
     * Your Plaid secret.
51
     *
52
     * @var string
53
     */
54
    private $secret;
55
56
    /**
57
     * One of sandbox, development, or production.
58
     *
59
     * @var string
60
     */
61
    private $environment;
62
63
    /**
64
     * Plaid API Version.
65
     *
66
     * @var string
67
     */
68
    private $api_version;
69
70
    /**
71
     * Create a new Plaid Client Instance
72
     *
73
     * @param $client_id
74
     * @param $secret
75
     * @param $environment
76
     * @param $api_version
77
     */
78
    public function __construct(
79
        $client_id,
80
        $secret,
81
        $environment,
82
        $api_version = null
83
    ) {
84
        $this->isValidEnvironment($environment);
85
86
        $this->client_id   = $client_id;
87
        $this->secret      = $secret;
88
        $this->environment = $environment;
89
        $this->api_version = $api_version;
90
91
        $this->initializeHttpClient();
92
    }
93
94
    /**
95
     * Checks if the environment is valid.
96
     *
97
     * @param string $environment
98
     */
99
    private function isValidEnvironment($environment): void
100
    {
101
        if (! array_key_exists($environment, self::ENVIRONMENTS)) {
102
            throw new InvalidEnvironmentException();
103
        }
104
    }
105
106
    /**
107
     * Returns the basic API Keys.
108
     *
109
     * @return array
110
     */
111
    public function getAPIKeys(): array
112
    {
113
        return [
114
            'client_id' => $this->client_id,
115
            'secret'    => $this->secret,
116
        ];
117
    }
118
119
    /**
120
     * Allows to set a custom client.
121
     *
122
     * @param \GuzzleHttp\ClientInterface $client
123
     */
124
    public function setClient(ClientInterface $client): void
125
    {
126
        $this->client = $client;
127
    }
128
129
    /**
130
     * Returns the current client.
131
     *
132
     * @return \GuzzleHttp\ClientInterface
133
     */
134
    public function getClient(): ClientInterface
135
    {
136
        return $this->client;
137
    }
138
139
    /**
140
     * Returns the base URI depending on the environment.
141
     *
142
     * @return string
143
     */
144
    public function getBaseUri(): string
145
    {
146
        return self::ENVIRONMENTS[$this->environment];
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     *
152
     * @return array
153
     */
154
    abstract public static function getValidResources(): array;
155
156
    /**
157
     * Initializes the Http Client.
158
     */
159
    private function initializeHttpClient(): void
160
    {
161
        $config = ['base_uri' => $this->getBaseUri()];
162
163
        if (null !== $this->api_version) {
164
            $config += ['headers' => ['Plaid-Version' => $this->api_version]];
165
        }
166
167
        $this->client = new Client($config);
168
    }
169
}
170