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: * Array of HttpPlugins
21: * @var HttpPlugin[]
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: * Sets Up the Team client
55: *
56: * @param PsrHttpClient|null $httpClient
57: * @param HttpPlugin[] $additionalPlugins
58: * @param array $additionalNormalizers
59: * @return API\Team\Client
60: */
61: public function setupTeamClient($httpClient = null, array $additionalPlugins = [],
62: array $additionalNormalizers = []): API\Team\Client
63: {
64: if(!$httpClient)
65: {
66: // might still be null tho...
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: * Sets Up the Budgets client
85: *
86: * @param PsrHttpClient|null $httpClient
87: * @param HttpPlugin[] $additionalPlugins
88: * @param array $additionalNormalizers
89: * @return API\Budgets\Client
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: * Sets Up the Expenses client
113: *
114: * @param PsrHttpClient|null $httpClient
115: * @param HttpPlugin[] $additionalPlugins
116: * @param array $additionalNormalizers
117: * @return API\Expenses\Client
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: * Sets Up the Payments client
142: *
143: * @param PsrHttpClient|null $httpClient
144: * @param HttpPlugin[] $additionalPlugins
145: * @param array $additionalNormalizers
146: * @return API\Payments\Client
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: * Sets Up the Transactions client
172: *
173: * @param PsrHttpClient|null $httpClient
174: * @param HttpPlugin[] $additionalPlugins
175: * @param array $additionalNormalizers
176: * @return API\Transactions\Client
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: * Sets Up the Webhooks client
202: *
203: * @param PsrHttpClient|null $httpClient
204: * @param HttpPlugin[] $additionalPlugins
205: * @param array $additionalNormalizers
206: * @return API\Webhooks\Client
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: * We intend to be one way for that token
232: */
233: private function getAuthPlugin(): HttpPlugin\AuthenticationPlugin
234: {
235: return $this->oAuthPlugin;
236: }
237:
238: /**
239: * Load your Brex token here
240: *
241: * @var string $sBearerToken
242: * @return self
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: * Gets HttpPlugins
254: */
255: public function getHttpPlugins(): array
256: {
257: return $this->aHttpPlugins;
258: }
259:
260: /**
261: * Adds a HttpPlugin
262: */
263: public function addHttpPlugin(HttpPlugin $oHttpPlugin): self
264: {
265: $this->aHttpPlugins[] = $oHttpPlugin;
266: return $this;
267: }
268:
269: /**
270: * clears Http Plugins
271: * @return void
272: */
273: public function resetHttpPlugins(): void
274: {
275: $this->aHttpPlugins = [];
276: }
277: }
278: