From 4a4c312b4df9e6b2533575b46f698c131e618ff8 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 13 Jun 2023 15:06:39 +0200 Subject: [PATCH] Wefact response now returns array if resource is not found (#2) * Wefact response now returns array if resource is not found --- CHANGELOG.md | 3 + README.md | 26 ++- src/Exceptions/InvalidRequestException.php | 10 -- src/Resources/Debtor.php | 6 +- src/Resources/Invoice.php | 189 ++++++++++++++------- src/Resources/Resource.php | 35 ++-- src/Resources/Settings/CostCategory.php | 51 ++++-- src/Resources/Settings/Settings.php | 26 ++- src/Resources/Subscription.php | 21 ++- src/Traits/Request.php | 21 ++- tests/WeFactTest.php | 9 +- 11 files changed, 259 insertions(+), 138 deletions(-) delete mode 100644 src/Exceptions/InvalidRequestException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1852919..a449c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All notable changes to `wefact-php` will be documented in this file. +## v0.5.0 - 2023-06-13 + +Removed `InvalidRequestException` and use Guzzle Client Exceptions. ## v0.4.0 - 2023-06-08 diff --git a/README.md b/README.md index 2cd9931..81da8dd 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,10 @@ Then you can use the package like this: ```php $weFact = new \Vormkracht10\WeFact\WeFact('your-api-key'); -$invoices = $weFact->invoices()->list(); +$response = $weFact->invoices()->list(); +if (isset($response['invoices'])) { + print_r($response['invoices']); +} ``` ## Available methods @@ -124,7 +127,10 @@ $invoices = $weFact->invoices()->list(); #### List creditors ```php -$weFact->creditors()->list(); +$response = $weFact->creditors()->list(); +if (isset($response['creditors'])) { + print_r($response['creditors']); +} ``` #### Create creditor @@ -132,9 +138,12 @@ $weFact->creditors()->list(); Required parameters: `CompanyName` or `SurName`. ```php -$weFact->creditors()->create([ +$response = $weFact->creditors()->create([ 'CompanyName' => 'Your company name', - ]) + ]); +if ($result['status'] == 'success') { + print_r($response['company']); +} ``` #### Update creditor @@ -142,10 +151,15 @@ $weFact->creditors()->create([ Required parameter: `Identifier` or `CreditorCode`. ```php -$weFact->creditors()->edit([ +$result = $weFact->creditors()->edit([ 'Identifier' => $creditorId, 'CompanyName' => 'Your company name', - ]) + ]); + +if ($result['status'] == 'error') { + // Something went wrong + print_r($result['errors']); +} ``` #### Show creditor diff --git a/src/Exceptions/InvalidRequestException.php b/src/Exceptions/InvalidRequestException.php deleted file mode 100644 index fafdac4..0000000 --- a/src/Exceptions/InvalidRequestException.php +++ /dev/null @@ -1,10 +0,0 @@ - + */ + public function delete(array $params = []): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) diff --git a/src/Resources/Invoice.php b/src/Resources/Invoice.php index 4ae4447..20a7a12 100644 --- a/src/Resources/Invoice.php +++ b/src/Resources/Invoice.php @@ -3,8 +3,11 @@ namespace Vormkracht10\WeFact\Resources; use GuzzleHttp\Client; +use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; +use JsonException; use Vormkracht10\WeFact\Enums\Invoice\InvoiceAction; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; class Invoice extends Resource { @@ -25,9 +28,9 @@ public function getResourceName(): string /** * @param array $params - * @return array|InvalidRequestException + * @return array */ - public function send(string $action, array $params): array|InvalidRequestException + public function send(string $action, array $params): array { return parent::sendRequest( controller: $this->getResourceName(), @@ -38,198 +41,252 @@ public function send(string $action, array $params): array|InvalidRequestExcepti /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function credit(array $params = []): array|InvalidRequestException + public function credit(array $params = []): array { return $this->send(action: InvoiceAction::CREDIT->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function partPayment(array $params = []): array|InvalidRequestException + public function partPayment(array $params = []): array { return $this->send(action: InvoiceAction::PART_PAYMENT->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function markAsPaid(array $params = []): array|InvalidRequestException + public function markAsPaid(array $params = []): array { return $this->send(action: InvoiceAction::MARK_AS_PAID->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function markAsUnpaid(array $params = []): array|InvalidRequestException + public function markAsUnpaid(array $params = []): array { return $this->send(action: InvoiceAction::MARK_AS_UNPAID->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function sendByEmail(array $params = []): array|InvalidRequestException + public function sendByEmail(array $params = []): array { return $this->send(action: InvoiceAction::SEND_BY_EMAIL->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function sendReminderByEmail(array $params = []): array|InvalidRequestException + public function sendReminderByEmail(array $params = []): array { return $this->send(action: InvoiceAction::SEND_REMINDER_BY_EMAIL->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function sendSummationByEmail(array $params = []): array|InvalidRequestException + public function sendSummationByEmail(array $params = []): array { return $this->send(action: InvoiceAction::SEND_SUMMATION_BY_EMAIL->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function download(array $params = []): array|InvalidRequestException + public function download(array $params = []): array { return $this->send(action: InvoiceAction::DOWNLOAD->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function block(array $params = []): array|InvalidRequestException + public function block(array $params = []): array { return $this->send(action: InvoiceAction::BLOCK->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function unblock(array $params = []): array|InvalidRequestException + public function unblock(array $params = []): array { return $this->send(action: InvoiceAction::UNBLOCK->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function schedule(array $params = []): array|InvalidRequestException + public function schedule(array $params = []): array { return $this->send(action: InvoiceAction::SCHEDULE->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function cancelSchedule(array $params = []): array|InvalidRequestException + public function cancelSchedule(array $params = []): array { return $this->send(action: InvoiceAction::CANCEL_SCHEDULE->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function paymentProcessPause(array $params = []): array|InvalidRequestException + public function paymentProcessPause(array $params = []): array { return $this->send(action: InvoiceAction::PAYMENT_PROCESS_PAUSE->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function paymentProcessReactivate(array $params = []): array|InvalidRequestException + public function paymentProcessReactivate(array $params = []): array { return $this->send(action: InvoiceAction::PAYMENT_PROCESS_REACTIVATE->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function sortLines(array $params = []): array|InvalidRequestException + public function sortLines(array $params = []): array { return $this->send(action: InvoiceAction::SORT_LINES->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function invoiceLineAdd(array $params = []): array|InvalidRequestException + public function invoiceLineAdd(array $params = []): array { return $this->send(action: InvoiceAction::INVOICE_LINE_ADD->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function invoiceLineDelete(array $params = []): array|InvalidRequestException + public function invoiceLineDelete(array $params = []): array { return $this->send(action: InvoiceAction::INVOICE_LINE_DELETE->value, params: $params); } /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function attachmentAdd(array $params = []): array|InvalidRequestException + public function attachmentAdd(array $params = []): array { $params['Type'] = $this->getResourceName(); @@ -238,11 +295,14 @@ public function attachmentAdd(array $params = []): array|InvalidRequestException /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function attachmentDelete(array $params = []): array|InvalidRequestException + public function attachmentDelete(array $params = []): array { $params['Type'] = $this->getResourceName(); @@ -251,11 +311,14 @@ public function attachmentDelete(array $params = []): array|InvalidRequestExcept /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function attachmentDownload(array $params = []): array|InvalidRequestException + public function attachmentDownload(array $params = []): array { $params['Type'] = $this->getResourceName(); diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 2fef418..13a8f00 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -3,8 +3,11 @@ namespace Vormkracht10\WeFact\Resources; use GuzzleHttp\Client; +use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; +use JsonException; use Vormkracht10\WeFact\Enums\Action; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; use Vormkracht10\WeFact\Traits\Request; @@ -22,11 +25,11 @@ public function __construct( /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException|ServerException|BadResponseException|JsonException */ - public function list(array $params = []): array|MethodNotAvailableException|InvalidRequestException + public function list(array $params = []): array { $controller = $this->getResourceName(); @@ -39,11 +42,11 @@ public function list(array $params = []): array|MethodNotAvailableException|Inva /** * @param array $params - * @return array|MethodNotAvailableException|InvalidRequestException + * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException * - * @throws MethodNotAvailableException|InvalidRequestException + * @throws MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException */ - public function show(array $params = []): array|MethodNotAvailableException|InvalidRequestException + public function show(array $params = []): array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException { $controller = $this->getResourceName(); @@ -56,11 +59,11 @@ public function show(array $params = []): array|MethodNotAvailableException|Inva /** * @param array $params - * @return array|MethodNotAvailableException|InvalidRequestException + * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException * - * @throws MethodNotAvailableException|InvalidRequestException + * @throws MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException */ - public function create(array $params): array|MethodNotAvailableException|InvalidRequestException + public function create(array $params): array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException { $controller = $this->getResourceName(); @@ -73,11 +76,11 @@ public function create(array $params): array|MethodNotAvailableException|Invalid /** * @param array $params - * @return array|MethodNotAvailableException|InvalidRequestException + * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException * - * @throws MethodNotAvailableException|InvalidRequestException + * @throws MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException */ - public function edit(array $params): array|MethodNotAvailableException|InvalidRequestException + public function edit(array $params): array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException { $controller = $this->getResourceName(); @@ -90,11 +93,11 @@ public function edit(array $params): array|MethodNotAvailableException|InvalidRe /** * @param array $params - * @return array|MethodNotAvailableException|InvalidRequestException + * @return array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException * - * @throws MethodNotAvailableException|InvalidRequestException + * @throws MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException */ - public function delete(array $params = []): array|MethodNotAvailableException|InvalidRequestException + public function delete(array $params = []): array|MethodNotAvailableException|ClientException|ServerException|BadResponseException|JsonException { $controller = $this->getResourceName(); diff --git a/src/Resources/Settings/CostCategory.php b/src/Resources/Settings/CostCategory.php index f8bb612..3fafb51 100644 --- a/src/Resources/Settings/CostCategory.php +++ b/src/Resources/Settings/CostCategory.php @@ -2,8 +2,11 @@ namespace Vormkracht10\WeFact\Resources\Settings; +use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; +use JsonException; use Vormkracht10\WeFact\Enums\Action; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; use Vormkracht10\WeFact\Resources\Resource; use Vormkracht10\WeFact\Traits\Request; @@ -21,11 +24,14 @@ public function getResourceName(): string /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function list(array $params = []): array|InvalidRequestException + public function list(array $params = []): array { $controller = $this->getResourceName(); @@ -38,11 +44,14 @@ public function list(array $params = []): array|InvalidRequestException /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function show(array $params = []): array|InvalidRequestException + public function show(array $params = []): array { $controller = $this->getResourceName(); @@ -55,11 +64,14 @@ public function show(array $params = []): array|InvalidRequestException /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function create(array $params): array|InvalidRequestException + public function create(array $params): array { $controller = $this->getResourceName(); @@ -72,11 +84,14 @@ public function create(array $params): array|InvalidRequestException /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function edit(array $params): array|InvalidRequestException + public function edit(array $params): array { $controller = $this->getResourceName(); @@ -89,11 +104,15 @@ public function edit(array $params): array|InvalidRequestException /** * @param array $params - * @return array|MethodNotAvailableException|InvalidRequestException + * @return array * - * @throws MethodNotAvailableException|InvalidRequestException + * @throws MethodNotAvailableException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function delete(array $params = []): array|MethodNotAvailableException|InvalidRequestException + public function delete(array $params = []): array { $controller = $this->getResourceName(); diff --git a/src/Resources/Settings/Settings.php b/src/Resources/Settings/Settings.php index 90d82a7..caa8dca 100644 --- a/src/Resources/Settings/Settings.php +++ b/src/Resources/Settings/Settings.php @@ -3,7 +3,6 @@ namespace Vormkracht10\WeFact\Resources\Settings; use Vormkracht10\WeFact\Enums\Action; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; use Vormkracht10\WeFact\Resources\Resource; use Vormkracht10\WeFact\Traits\Request; @@ -19,7 +18,10 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } - public function list(array $params = []): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + */ + public function list(array $params = []): array { $controller = $this->getResourceName(); @@ -30,28 +32,40 @@ public function list(array $params = []): array|MethodNotAvailableException|Inva ); } - public function show(array $params = []): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + */ + public function show(array $params = []): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) ); } - public function create(array $params): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + */ + public function create(array $params): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) ); } - public function edit(array $params): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + */ + public function edit(array $params): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) ); } - public function delete(array $params = []): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + */ + public function delete(array $params = []): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) diff --git a/src/Resources/Subscription.php b/src/Resources/Subscription.php index 7a1b3dc..1d0da8c 100644 --- a/src/Resources/Subscription.php +++ b/src/Resources/Subscription.php @@ -2,8 +2,11 @@ namespace Vormkracht10\WeFact\Resources; +use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; +use JsonException; use Vormkracht10\WeFact\Enums\Subscription\SubscriptionAction; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; use Vormkracht10\WeFact\Exceptions\MethodNotAvailableException; class Subscription extends Resource @@ -15,7 +18,12 @@ public function getResourceName(): string return self::CONTROLLER_NAME; } - public function delete(array $params = []): array|MethodNotAvailableException|InvalidRequestException + /** + * @return array + * + * @throws MethodNotAvailableException + */ + public function delete(array $params = []): array { throw new MethodNotAvailableException( sprintf('%s is not available for this resource.', __METHOD__) @@ -24,11 +32,14 @@ public function delete(array $params = []): array|MethodNotAvailableException|In /** * @param array $params - * @return array|InvalidRequestException + * @return array * - * @throws InvalidRequestException + * @throws ClientException + * @throws ServerException + * @throws BadResponseException + * @throws JsonException */ - public function terminate(array $params = []): array|InvalidRequestException + public function terminate(array $params = []): array { return $this->sendRequest( controller: $this->getResourceName(), diff --git a/src/Traits/Request.php b/src/Traits/Request.php index 8ce46e2..9c5ddd2 100644 --- a/src/Traits/Request.php +++ b/src/Traits/Request.php @@ -3,17 +3,14 @@ namespace Vormkracht10\WeFact\Traits; use Psr\Http\Message\ResponseInterface; -use Vormkracht10\WeFact\Exceptions\InvalidRequestException; trait Request { /** * @param array $params * @return array - * - * @throws InvalidRequestException */ - public function sendRequest(string $controller, string $action, array $params): array|InvalidRequestException + public function sendRequest(string $controller, string $action, array $params): array { $params['api_key'] = $this->apiKey; $params['controller'] = $controller; @@ -27,17 +24,19 @@ public function sendRequest(string $controller, string $action, array $params): return $this->parseResponse($response); } - /** @return array|InvalidRequestException */ - public function parseResponse(ResponseInterface $response): array|InvalidRequestException + /** + * @return array + * + * @throws \GuzzleHttp\Exception\ClientException; + * @throws \GuzzleHttp\Exception\ServerException; + * @throws \JsonException; + * @throws \GuzzleHttp\Exception\BadResponseException; + */ + public function parseResponse(ResponseInterface $response): array { $body = $response->getBody(); $responseData = json_decode((string) $body, true, 512, JSON_THROW_ON_ERROR); - if ($responseData['status'] === 'error') { - $errors = implode(', ', $responseData['errors']); - throw new InvalidRequestException($errors); - } - return $responseData; } } diff --git a/tests/WeFactTest.php b/tests/WeFactTest.php index a35ab07..b3d29e0 100644 --- a/tests/WeFactTest.php +++ b/tests/WeFactTest.php @@ -27,9 +27,12 @@ expect($weFact->subscriptions())->toBeInstanceOf(Subscription::class); }); -it('can return an error when non existing API key is used', function () { +it('cannot return invoices when non existing API key is used', function () { $apiKey = 'FAKE_API_KEY'; $weFact = new WeFact($apiKey); - $weFact->invoices()->list(); -})->throws(\Vormkracht10\WeFact\Exceptions\InvalidRequestException::class); + $response = $weFact->invoices()->list(); + + expect($response)->toHaveKey('errors'); + expect($response)->not->toHaveKey('invoices'); +});