1: | <?php
|
2: | namespace NxSys\Library\Clients\Brex;
|
3: |
|
4: | use NxSys\Library\Clients\Brex\API;
|
5: |
|
6: | use Http\Discovery\HttpClientDiscovery;
|
7: |
|
8: | use Http\Client\Common\Plugin as HttpPlugin;
|
9: |
|
10: | use Http\Message\Authentication\Bearer;
|
11: |
|
12: | use Psr\Http\Client\ClientInterface as PsrHttpClient;
|
13: |
|
14: | class SDKHost
|
15: | {
|
16: | public ?PsrHttpClient $oHttpClient=null;
|
17: | public array $aApiClients = [];
|
18: |
|
19: | |
20: | |
21: | |
22: |
|
23: | public array $aHttpPlugins=[];
|
24: |
|
25: | private $oAuthPlugin;
|
26: |
|
27: | public function setHttpClient(?PsrHttpClient $oPsrHttpClient): self
|
28: | {
|
29: | $this->oHttpClient = $oPsrHttpClient;
|
30: | return $this;
|
31: | }
|
32: |
|
33: | public function getHttpClient(): ?PsrHttpClient
|
34: | {
|
35: | return $this->oHttpClient;
|
36: | }
|
37: |
|
38: |
|
39: |
|
40: | public function setupAllClients($httpClient = null, array $additionalPlugins = [],
|
41: | array $additionalNormalizers = []): self
|
42: | {
|
43: | $this->setupBudgetsClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
44: | $this->setupExpensesClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
45: | $this->setupPaymentsClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
46: | $this->setupTeamClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
47: | $this->setupTransactionsClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
48: | $this->setupWebhooksClient($httpClient, $additionalPlugins, $additionalNormalizers);
|
49: |
|
50: | return $this;
|
51: | }
|
52: |
|
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: |
|
61: | public function setupTeamClient($httpClient = null, array $additionalPlugins = [],
|
62: | array $additionalNormalizers = []): API\Team\Client
|
63: | {
|
64: | if(!$httpClient)
|
65: | {
|
66: |
|
67: | $httpClient = $this->getHttpClient();
|
68: | }
|
69: | $additionalPlugins = array_merge(
|
70: | $additionalPlugins,
|
71: | $this->getHttpPlugins(),
|
72: | [$this->getAuthPlugin()]
|
73: | );
|
74: | $this->aApiClients['team'] = API\Team\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
75: | return $this->aApiClients['team'];
|
76: | }
|
77: |
|
78: | public function getTeamClient(): API\Team\Client
|
79: | {
|
80: | return $this->aApiClients['team']??throw new \RuntimeException("Team client has not been setup yet.");
|
81: | }
|
82: |
|
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: |
|
91: | public function setupBudgetsClient($httpClient = null, array $additionalPlugins = [],
|
92: | array $additionalNormalizers = []): API\Budgets\Client
|
93: | {
|
94: | if(!$httpClient)
|
95: | {
|
96: | $httpClient = $this->getHttpClient();
|
97: | }
|
98: | $additionalPlugins = array_merge(
|
99: | $additionalPlugins,
|
100: | $this->getHttpPlugins(),
|
101: | [$this->getAuthPlugin()]
|
102: | );
|
103: | $this->aApiClients['budgets'] = API\Budgets\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
104: | return $this->aApiClients['budgets'];
|
105: | }
|
106: | public function getBudgetsClient(): API\Budgets\Client
|
107: | {
|
108: | return $this->aApiClients['budgets']??throw new \RuntimeException("Budget client has not been setup yet.");
|
109: | }
|
110: |
|
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: |
|
119: | public function setupExpensesClient($httpClient = null, array $additionalPlugins = [],
|
120: | array $additionalNormalizers = []): API\Expenses\Client
|
121: | {
|
122: | if(!$httpClient)
|
123: | {
|
124: | $httpClient = $this->getHttpClient();
|
125: | }
|
126: | $additionalPlugins = array_merge(
|
127: | $additionalPlugins,
|
128: | $this->getHttpPlugins(),
|
129: | [$this->getAuthPlugin()]
|
130: | );
|
131: | $this->aApiClients['expenses'] = API\Expenses\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
132: | return $this->aApiClients['expenses'];
|
133: | }
|
134: |
|
135: | public function getExpensesClient(): API\Expenses\Client
|
136: | {
|
137: | return $this->aApiClients['expenses']??throw new \RuntimeException("Expenses client has not been setup yet.");
|
138: | }
|
139: |
|
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: |
|
148: | public function setupPaymentsClient($httpClient = null, array $additionalPlugins = [],
|
149: | array $additionalNormalizers = []): API\Payments\Client
|
150: | {
|
151: | if(!$httpClient)
|
152: | {
|
153: | $httpClient = $this->getHttpClient();
|
154: | }
|
155: | $additionalPlugins = array_merge(
|
156: | $additionalPlugins,
|
157: | $this->getHttpPlugins(),
|
158: | [$this->getAuthPlugin()]
|
159: | );
|
160: | $this->aApiClients['payments'] = API\Payments\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
161: | return $this->aApiClients['payments'];
|
162: | }
|
163: |
|
164: | public function getPaymentsClient(): API\Payments\Client
|
165: | {
|
166: | return $this->aApiClients['payments']??throw new \RuntimeException("Payment client has not been setup yet.");
|
167: | ;
|
168: | }
|
169: |
|
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: |
|
178: | public function setupTransactionsClient($httpClient = null, array $additionalPlugins = [],
|
179: | array $additionalNormalizers = []): API\Transactions\Client
|
180: | {
|
181: | if(!$httpClient)
|
182: | {
|
183: | $httpClient = $this->getHttpClient();
|
184: | }
|
185: | $additionalPlugins = array_merge(
|
186: | $additionalPlugins,
|
187: | $this->getHttpPlugins(),
|
188: | [$this->getAuthPlugin()]
|
189: | );
|
190: | $this->aApiClients['transactions']
|
191: | = API\Transactions\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
192: | return $this->aApiClients['transactions'];
|
193: | }
|
194: |
|
195: | public function getTransactionsClient(): API\Transactions\Client
|
196: | {
|
197: | return $this->aApiClients['transactions']??throw new \RuntimeException("Transactions Client has not been setup yet.");
|
198: | }
|
199: |
|
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: |
|
208: | public function setupWebhooksClient($httpClient = null, array $additionalPlugins = [],
|
209: | array $additionalNormalizers = []): API\Webhooks\Client
|
210: | {
|
211: | if(!$httpClient)
|
212: | {
|
213: | $httpClient = $this->getHttpClient();
|
214: | }
|
215: | $additionalPlugins = array_merge(
|
216: | $additionalPlugins,
|
217: | $this->getHttpPlugins(),
|
218: | [$this->getAuthPlugin()]
|
219: | );
|
220: | $this->aApiClients['webhooks']
|
221: | = API\Webhooks\Client::create($httpClient, $additionalPlugins, $additionalNormalizers);
|
222: | return $this->aApiClients['webhooks'];
|
223: | }
|
224: |
|
225: | public function getWebhooksClient(): API\Webhooks\Client
|
226: | {
|
227: | return $this->aApiClients['webhooks']??throw new \RuntimeException("Webhooks client has not been setup yet.");
|
228: | }
|
229: |
|
230: | |
231: | |
232: |
|
233: | private function getAuthPlugin(): HttpPlugin\AuthenticationPlugin
|
234: | {
|
235: | return $this->oAuthPlugin;
|
236: | }
|
237: |
|
238: | |
239: | |
240: | |
241: | |
242: | |
243: |
|
244: | public function setAuthKey(string $sBearerToken): self
|
245: | {
|
246: | $authenticationPlugin = new HttpPlugin\AuthenticationPlugin(new Bearer($sBearerToken));
|
247: | $this->oAuthPlugin = $authenticationPlugin;
|
248: | return $this;
|
249: | }
|
250: |
|
251: |
|
252: | |
253: | |
254: |
|
255: | public function getHttpPlugins(): array
|
256: | {
|
257: | return $this->aHttpPlugins;
|
258: | }
|
259: |
|
260: | |
261: | |
262: |
|
263: | public function addHttpPlugin(HttpPlugin $oHttpPlugin): self
|
264: | {
|
265: | $this->aHttpPlugins[] = $oHttpPlugin;
|
266: | return $this;
|
267: | }
|
268: |
|
269: | |
270: | |
271: | |
272: |
|
273: | public function resetHttpPlugins(): void
|
274: | {
|
275: | $this->aHttpPlugins = [];
|
276: | }
|
277: | }
|
278: | |