-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractClient.php
More file actions
113 lines (92 loc) · 3.71 KB
/
AbstractClient.php
File metadata and controls
113 lines (92 loc) · 3.71 KB
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
<?php
declare(strict_types=1);
namespace AdroSoftware\CircleSoSdk;
use AdroSoftware\CircleSoSdk\Exception\{
RequestUnauthorizedException,
ResourceNotFoundException,
UnsuccessfulResponseException
};
use AdroSoftware\CircleSoSdk\Http\Message\ArrayTransformer;
use AdroSoftware\CircleSoSdk\Http\Message\ResponseTransformerInterface;
use AdroSoftware\CircleSoSdk\Response\FactoryInterface;
use AdroSoftware\CircleSoSdk\Response\Status;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\BaseUriPlugin;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Psr\Http\Message\ResponseInterface;
abstract class AbstractClient
{
protected ClientBuilder $clientBuilder;
protected FactoryInterface $responseFactory;
protected ResponseTransformerInterface $responseTransformer;
public function __construct(
protected string $token,
protected ?Options $options = null,
) {
$this->options = $options ?? new Options();
$this->responseTransformer = new ArrayTransformer();
$this->responseFactory = $this->options->getResponseFactory();
$this->clientBuilder = $this->options->getClientBuilder();
$this->clientBuilder->addPlugin(new BaseUriPlugin($this->options->getUri()));
$this->clientBuilder->addPlugin(
new HeaderDefaultsPlugin([
'User-Agent' => $this->options->getUserAgent(),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => "Token {$this->token}",
])
);
}
public function getHttpClient(): HttpMethodsClientInterface
{
return $this->clientBuilder->getHttpClient();
}
public function getResponseTransformer(): ResponseTransformerInterface
{
return $this->responseTransformer;
}
public function getResponseFactory(): FactoryInterface
{
return $this->responseFactory;
}
/**
* @throws ResourceNotFoundException
* @throws RequestUnauthorizedException
* @throws UnsuccessfulResponseException
*/
public function factorResponse(ResponseInterface $response, ?bool $throwNotFoundException = null): mixed
{
$response = $this->getResponseTransformer()->transform($response);
$this->checkIfRequestWasSuccessful($response, $throwNotFoundException);
if ($this->getResponseFactory() instanceof FactoryInterface) {
$response = $this->getResponseFactory()->factor($response);
}
return $response;
}
/**
* Check if the request was successful.
*
* If the resource return `null` it could mean the resource was not found,
* in this scenario we can throw a `not found` exception setting the
* `$throwNotFoundException` parameter to true.
*
* @throws ResourceNotFoundException
* @throws RequestUnauthorizedException
* @throws UnsuccessfulResponseException
*/
protected function checkIfRequestWasSuccessful(?array $response = null, ?bool $throwNotFoundException = null): void
{
if (isset($response['status']) && $response['status'] === Status::UNAUTHORIZED) {
$message = $response['message'] ?? "The request was not authorized.";
throw new RequestUnauthorizedException($message);
}
if (isset($response['success']) && boolval($response['success']) === false) {
$message = $response['message'] ??
"The request did not return a successful response.";
throw new UnsuccessfulResponseException($message);
}
if ($response === null && $throwNotFoundException === true) {
throw new ResourceNotFoundException();
}
}
}