-
Notifications
You must be signed in to change notification settings - Fork 0
/
IBanFirst.php
168 lines (143 loc) · 4.96 KB
/
IBanFirst.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the iBanFirst HTTP Client package.
*
* (c) Radhi GUENNICHI <[email protected]> <+216 50 711 816>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace IBanFirst;
use IBanFirst\Authenticator\AuthenticatorInterface;
use IBanFirst\Authenticator\UsernameTokenAuthenticator;
use IBanFirst\Exception\SDKException;
use IBanFirst\Service\FinancialMovementsService;
use IBanFirst\Service\WalletsService;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class IBanFirst
{
private const ENVIRONMENTS = [
IBanFirstEnvironment::SANDBOX => 'https://sandbox2.ibanfirst.com/api',
// TODO: Add another environment endpoints here... ("live" for example)
IBanFirstEnvironment::LIVE => 'https://unknown'
];
private const AUTHENTICATORS = [
'username_token' => UsernameTokenAuthenticator::class
// We may add more authenticators later like (Bearer, oAuth, etc...)
];
protected array $config;
private IBanFirstHttpClient $iBanFirstHttpClient;
public function __construct(array $config = [])
{
$config = array_merge([
'authenticator' => 'username_token',
'http_client' => HttpClient::create()
// We may add more default options in the future...
], $config);
$this->validateConfig($config);
// Build the HTTP client with the authenticator
$this->iBanFirstHttpClient = new IBanFirstHttpClient(
$this->getBaseURL($config['environment']),
$config['http_client'],
$this->getAuthenticator($config)
);
$this->config = $config;
}
/**
* Service for interacting with /wallets/ endpoints.
*
* @return WalletsService
*/
public function wallets(): WalletsService
{
if (!isset($this->wallets)) {
$this->wallets = new WalletsService($this->iBanFirstHttpClient);
}
return $this->wallets;
}
/**
* Service for interacting with /financialMovements/ endpoints.
*
* @return FinancialMovementsService
*/
public function financialMovements(): FinancialMovementsService
{
if (!isset($this->financialMovements)) {
$this->financialMovements = new FinancialMovementsService($this->iBanFirstHttpClient);
}
return $this->financialMovements;
}
public function setHttpClient(HttpClientInterface $httpClient): void
{
$this->iBanFirstHttpClient->setBaseHttpClient($httpClient);
}
public function getHttpClient(): HttpClientInterface
{
return $this->iBanFirstHttpClient->getBaseHttpClient();
}
/**
* Ensures a config is valid and sets defaults where required.
*
* @param array $config
*
* @throws SDKException
*/
private function validateConfig(array $config): void
{
$requiredKeys = ['environment', 'authenticator', 'http_client'];
foreach ($requiredKeys as $requiredKey) {
if (!isset($config[$requiredKey])) {
throw new SDKException(
sprintf('Missing required option "%s".', $requiredKey)
);
}
}
if (!$config['http_client'] instanceof HttpClientInterface) {
throw new SDKException(
sprintf('The provided "http_client" should be an instance of "%s", "%s" given.',
HttpClientInterface::class,
gettype($config['http_client'])
)
);
}
// Check if the authenticator is valid and already existed in the AUTHENTICATORS list.
if (!array_key_exists($config['authenticator'], self::AUTHENTICATORS)) {
throw new SDKException(
sprintf('The provided authenticator "%s" is not supported.', $config['authenticator'])
);
}
if (!array_key_exists($config['environment'], self::ENVIRONMENTS)) {
throw new SDKException(
sprintf('The provided environment "%s" is not supported.', $config['environment'])
);
}
}
/**
* Get base URL for a given environment.
*
* @param string $environment
*
* @return string
*/
private function getBaseURL(string $environment): string
{
return self::ENVIRONMENTS[$environment];
}
/**
* Build authenticator instance based on configuration.
*
* @param array $config
*
* @return AuthenticatorInterface
*/
private function getAuthenticator(array $config): AuthenticatorInterface
{
if (!isset($this->authenticator)) {
$authenticatorClass = self::AUTHENTICATORS[$config['authenticator']];
$this->authenticator = new $authenticatorClass($config);
}
return $this->authenticator;
}
}