Skip to content

Commit cbabbb8

Browse files
committed
feat: added some extra exceptions
1 parent 4cee137 commit cbabbb8

7 files changed

Lines changed: 168 additions & 42 deletions

File tree

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rapideinternet/postcodeapi.client",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "PostcodeAPI client package",
55
"keywords": [],
66
"homepage": "https://github.com/rapideinternet/postcodeapi.client",
@@ -12,9 +12,9 @@
1212
}],
1313
"require": {
1414
"php": "^8.1",
15-
"guzzlehttp/guzzle": "^7.5",
15+
"guzzlehttp/guzzle": "^7.8",
1616
"illuminate/support": "^9.52",
17-
"symfony/symfony": "^6.2"
17+
"symfony/symfony": "^6.3"
1818
},
1919
"require-dev": {
2020
"phpunit/phpunit": "^9.6"

src/Contracts/BaseClient.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use GuzzleHttp\Exception\RequestException;
1111
use RapideInternet\PostcodeAPI\Exceptions\Exception;
1212
use RapideInternet\PostcodeAPI\Exceptions\NotFoundException;
13+
use RapideInternet\PostcodeAPI\Exceptions\BadGateWayException;
1314
use RapideInternet\PostcodeAPI\Exceptions\ForbiddenException;
15+
use RapideInternet\PostcodeAPI\Exceptions\TooManyRequests;
1416
use RapideInternet\PostcodeAPI\Exceptions\BadRequestException;
1517
use Symfony\Component\HttpFoundation\Response as HttpResponse;
1618
use RapideInternet\PostcodeAPI\Exceptions\UnauthorizedException;
@@ -22,6 +24,7 @@
2224
abstract class BaseClient {
2325

2426
private Client $client;
27+
protected array $clients = [];
2528

2629
/**
2730
* Constructor
@@ -86,12 +89,14 @@ protected function parseException(GuzzleException $e): Exception {
8689
? new InternalServerErrorException($e)
8790
: match($response->getStatusCode()) {
8891
HttpResponse::HTTP_BAD_REQUEST => new BadRequestException($e),
92+
HttpResponse::HTTP_UNAUTHORIZED => new UnauthorizedException($e),
8993
HttpResponse::HTTP_FORBIDDEN => new ForbiddenException($e),
90-
HttpResponse::HTTP_INTERNAL_SERVER_ERROR => new InternalServerErrorException($e),
9194
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),
9496
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),
95100
default => new NotImplementedException($e)
96101
};
97102
}
@@ -120,4 +125,38 @@ protected abstract function beforeRequest(): void;
120125
* @return array
121126
*/
122127
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+
}
123162
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RapideInternet\PostcodeAPI\Exceptions;
4+
5+
use Symfony\Component\HttpFoundation\Response as HttpResponse;
6+
7+
class BadGateWayException extends Exception {
8+
9+
/**
10+
* @return int
11+
*/
12+
public function getStatusCode(): int {
13+
return HttpResponse::HTTP_BAD_GATEWAY;
14+
}
15+
}

src/Exceptions/TooManyRequests.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RapideInternet\PostcodeAPI\Exceptions;
4+
5+
use Symfony\Component\HttpFoundation\Response as HttpResponse;
6+
7+
class TooManyRequests extends Exception {
8+
9+
/**
10+
* @return int
11+
*/
12+
public function getStatusCode(): int {
13+
return HttpResponse::HTTP_TOO_MANY_REQUESTS;
14+
}
15+
}

src/PostcodeAPIClient.php

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Psr\Http\Message\ResponseInterface;
66
use RapideInternet\PostcodeAPI\Clients\AddressClient;
77
use RapideInternet\PostcodeAPI\Contracts\BaseClient;
8-
use Exception;
98

9+
/**
10+
* @property AddressClient $address
11+
*/
1012
class PostcodeAPIClient extends BaseClient {
1113

1214
protected string $url = 'https://api.postcodeapi.nu/v3';
@@ -61,38 +63,4 @@ public function getHeaders(): array {
6163
'Content-Type' => 'application/json'
6264
];
6365
}
64-
65-
/**
66-
* @param $method
67-
* @param $arguments
68-
* @return mixed
69-
* @throws Exception
70-
*/
71-
public function __call($method, $arguments) {
72-
if(!isset($this->clients[$method]) && !method_exists($this, $method)) {
73-
throw new Exception("Unknown method [$method]");
74-
}
75-
elseif(method_exists($this, $method)) {
76-
return call_user_func([$this, $method], $arguments);
77-
}
78-
elseif(isset($this->clients[$method])) {
79-
return new $this->clients[$method]($this);
80-
}
81-
throw new Exception("Unknown method [$method]");
82-
}
83-
84-
/**
85-
* @param $property
86-
* @return mixed
87-
* @throws Exception
88-
*/
89-
public function __get($property){
90-
if(property_exists($this, $property)) {
91-
return $this->{$property};
92-
}
93-
elseif(isset($this->clients[$property])) {
94-
return new $this->clients[$property]($this);
95-
}
96-
throw new Exception("Unknown property [$property]");
97-
}
9866
}

src/Response.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Component\HttpFoundation\Response as HttpResponse;
66

7-
class Response {
7+
class Response implements ResponseInterface {
88

99
protected int $status_code;
1010
protected ?array $body;
@@ -61,10 +61,38 @@ public function getMessages(): array {
6161
return [['status' => $this->getStatus(), 'text' => $this->message]];
6262
}
6363

64+
/**
65+
* @return string
66+
*/
67+
public function toJson(): string {
68+
return json_encode(['status_code' => $this->status_code, 'message' => $this->message, 'body' => $this->body]);
69+
}
70+
6471
/**
6572
* @return bool
6673
*/
6774
public function isValid(): bool {
6875
return $this->getStatusCode() >= HttpResponse::HTTP_OK && $this->getStatusCode() <= HttpResponse::HTTP_IM_USED;
6976
}
77+
78+
/**
79+
* @return bool
80+
*/
81+
public function isRedirect(): bool {
82+
return $this->getStatusCode() >= HttpResponse::HTTP_MOVED_PERMANENTLY && $this->getStatusCode() <= HttpResponse::HTTP_PERMANENTLY_REDIRECT;
83+
}
84+
85+
/**
86+
* @return bool
87+
*/
88+
public function isInvalid(): bool {
89+
return $this->getStatusCode() >= HttpResponse::HTTP_BAD_REQUEST && $this->getStatusCode() <= HttpResponse::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS;
90+
}
91+
92+
/**
93+
* @return bool
94+
*/
95+
public function isServerError(): bool {
96+
return $this->getStatusCode() >= HttpResponse::HTTP_INTERNAL_SERVER_ERROR;
97+
}
7098
}

src/ResponseInterface.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace RapideInternet\PostcodeAPI;
4+
5+
interface ResponseInterface {
6+
7+
/**
8+
* @return int
9+
*/
10+
public function getStatusCode(): int;
11+
12+
/**
13+
* @return mixed
14+
*/
15+
public function getBody(): mixed;
16+
17+
/**
18+
* @return array|null
19+
*/
20+
public function getData(): ?array;
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getMessage(): string;
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getStatus(): string;
31+
32+
/**
33+
* @return array
34+
*/
35+
public function getMessages(): array;
36+
37+
/**
38+
* @return string
39+
*/
40+
public function toJson(): string;
41+
42+
/**
43+
* @return bool
44+
*/
45+
public function isValid(): bool;
46+
47+
/**
48+
* @return bool
49+
*/
50+
public function isRedirect(): bool;
51+
52+
/**
53+
* @return bool
54+
*/
55+
public function isInvalid(): bool;
56+
57+
/**
58+
* @return bool
59+
*/
60+
public function isServerError(): bool;
61+
}

0 commit comments

Comments
 (0)