1 | <?php |
||
2 | |||
3 | namespace IproSoftwareApi; |
||
4 | |||
5 | use IproSoftwareApi\AccessToken\NoneCacher; |
||
6 | use IproSoftwareApi\Contracts\AccessTokenCacher; |
||
7 | use IproSoftwareApi\DTOs\ClientCredentials; |
||
8 | use IproSoftwareApi\Exceptions\IproSoftwareApiException; |
||
9 | use IproSoftwareApi\Traits\HasApiMethods; |
||
10 | |||
11 | class IproSoftwareClient |
||
12 | { |
||
13 | use HasApiMethods; |
||
14 | |||
15 | /** @var \IproSoftwareApi\Contracts\HttpClient */ |
||
16 | protected $httpClient; |
||
17 | |||
18 | /** |
||
19 | * Client constructor. |
||
20 | * |
||
21 | * @param array $configurations |
||
22 | * |
||
23 | * @throws IproSoftwareApiException |
||
24 | */ |
||
25 | 9 | public function __construct($configurations = []) |
|
26 | { |
||
27 | 9 | if (isset($configurations['requests_path_prefix']) && $configurations['requests_path_prefix']) { |
|
28 | 3 | $this->setPathPrefix($configurations['requests_path_prefix']); |
|
29 | } |
||
30 | |||
31 | 9 | $this->tryCreateDefaultHttpClient($configurations); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param Contracts\HttpClient $httpClient |
||
36 | * |
||
37 | * @return static |
||
38 | */ |
||
39 | 4 | public function setHttpClient(Contracts\HttpClient $httpClient): static |
|
40 | { |
||
41 | 4 | $this->httpClient = $httpClient; |
|
42 | |||
43 | 4 | return $this; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param AccessTokenCacher $cacheManager |
||
48 | * |
||
49 | * @throws IproSoftwareApiException |
||
50 | * |
||
51 | * @return static |
||
52 | */ |
||
53 | 2 | public function setAccessTokenCacheManager(AccessTokenCacher $cacheManager): static |
|
54 | { |
||
55 | 2 | if (!($this->httpClient instanceof Contracts\HttpClient)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
56 | 1 | throw new IproSoftwareApiException('A HttpClient must be set at the beginning.', 500); |
|
57 | } |
||
58 | 1 | $this->httpClient->setCacheManager($cacheManager); |
|
59 | |||
60 | 1 | return $this; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param array $configurations |
||
65 | * |
||
66 | * @throws IproSoftwareApiException |
||
67 | */ |
||
68 | 9 | protected function tryCreateDefaultHttpClient(array $configurations = []) |
|
69 | { |
||
70 | 9 | $clientCredentials = new ClientCredentials( |
|
71 | 9 | $configurations['api_host'] ?? '', |
|
72 | 9 | $configurations['client_id'] ?? '', |
|
73 | 9 | $configurations['client_secret'] ?? '' |
|
74 | 9 | ); |
|
75 | |||
76 | 9 | if (isset($configurations['oauth_endpoint'])) { |
|
77 | 1 | $clientCredentials->tokenEndpoint = $configurations['oauth_endpoint']; |
|
78 | } |
||
79 | |||
80 | 9 | if ($clientCredentials->valid()) { |
|
81 | 1 | $this->httpClient = new HttpClient($clientCredentials, $configurations['cache_manager'] ?? new NoneCacher(), $configurations); |
|
82 | } |
||
83 | } |
||
84 | |||
85 | 5 | public function httpClient(): ?\IproSoftwareApi\Contracts\HttpClient |
|
86 | { |
||
87 | 5 | return $this->httpClient; |
|
88 | } |
||
89 | } |
||
90 |