|
10 | 10 | use GuzzleHttp\Exception\RequestException; |
11 | 11 | use RapideInternet\PostcodeAPI\Exceptions\Exception; |
12 | 12 | use RapideInternet\PostcodeAPI\Exceptions\NotFoundException; |
| 13 | +use RapideInternet\PostcodeAPI\Exceptions\BadGateWayException; |
13 | 14 | use RapideInternet\PostcodeAPI\Exceptions\ForbiddenException; |
| 15 | +use RapideInternet\PostcodeAPI\Exceptions\TooManyRequests; |
14 | 16 | use RapideInternet\PostcodeAPI\Exceptions\BadRequestException; |
15 | 17 | use Symfony\Component\HttpFoundation\Response as HttpResponse; |
16 | 18 | use RapideInternet\PostcodeAPI\Exceptions\UnauthorizedException; |
|
22 | 24 | abstract class BaseClient { |
23 | 25 |
|
24 | 26 | private Client $client; |
| 27 | + protected array $clients = []; |
25 | 28 |
|
26 | 29 | /** |
27 | 30 | * Constructor |
@@ -86,12 +89,14 @@ protected function parseException(GuzzleException $e): Exception { |
86 | 89 | ? new InternalServerErrorException($e) |
87 | 90 | : match($response->getStatusCode()) { |
88 | 91 | HttpResponse::HTTP_BAD_REQUEST => new BadRequestException($e), |
| 92 | + HttpResponse::HTTP_UNAUTHORIZED => new UnauthorizedException($e), |
89 | 93 | HttpResponse::HTTP_FORBIDDEN => new ForbiddenException($e), |
90 | | - HttpResponse::HTTP_INTERNAL_SERVER_ERROR => new InternalServerErrorException($e), |
91 | 94 | HttpResponse::HTTP_NOT_FOUND => new NotFoundException($e), |
92 | | - HttpResponse::HTTP_SERVICE_UNAVAILABLE => new ServiceUnavailableException($e), |
93 | | - HttpResponse::HTTP_UNAUTHORIZED => new UnauthorizedException($e), |
| 95 | + HttpResponse::HTTP_TOO_MANY_REQUESTS => new TooManyRequests($e), |
94 | 96 | HttpResponse::HTTP_UNPROCESSABLE_ENTITY => new UnprocessableEntityException($e), |
| 97 | + HttpResponse::HTTP_INTERNAL_SERVER_ERROR => new InternalServerErrorException($e), |
| 98 | + HttpResponse::HTTP_BAD_GATEWAY => new BadGateWayException($e), |
| 99 | + HttpResponse::HTTP_SERVICE_UNAVAILABLE => new ServiceUnavailableException($e), |
95 | 100 | default => new NotImplementedException($e) |
96 | 101 | }; |
97 | 102 | } |
@@ -120,4 +125,38 @@ protected abstract function beforeRequest(): void; |
120 | 125 | * @return array |
121 | 126 | */ |
122 | 127 | public abstract function getHeaders(): array; |
| 128 | + |
| 129 | + /** |
| 130 | + * @param $method |
| 131 | + * @param $arguments |
| 132 | + * @return mixed |
| 133 | + * @throws \Exception |
| 134 | + */ |
| 135 | + public function __call($method, $arguments) { |
| 136 | + if(!isset($this->clients[$method]) && !method_exists($this, $method)) { |
| 137 | + throw new \Exception("Unknown method [$method]"); |
| 138 | + } |
| 139 | + elseif(method_exists($this, $method)) { |
| 140 | + return call_user_func([$this, $method], $arguments); |
| 141 | + } |
| 142 | + elseif(isset($this->clients[$method])) { |
| 143 | + return new $this->clients[$method]($this); |
| 144 | + } |
| 145 | + throw new \Exception("Unknown method [$method]"); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param $property |
| 150 | + * @return mixed |
| 151 | + * @throws \Exception |
| 152 | + */ |
| 153 | + public function __get($property){ |
| 154 | + if(property_exists($this, $property)) { |
| 155 | + return $this->{$property}; |
| 156 | + } |
| 157 | + elseif(isset($this->clients[$property])) { |
| 158 | + return new $this->clients[$property]($this); |
| 159 | + } |
| 160 | + throw new \Exception("Unknown property [$property]"); |
| 161 | + } |
123 | 162 | } |
0 commit comments