Skip to content

Commit

Permalink
Merge pull request #6 from jonasdekeukelaere/fix-status-code-checker
Browse files Browse the repository at this point in the history
Fix checking status code for some calls
  • Loading branch information
jonasdekeukelaere authored Aug 6, 2024
2 parents 6b6ce1f + de50dcf commit 2c95f6f
Showing 1 changed file with 73 additions and 53 deletions.
126 changes: 73 additions & 53 deletions src/DeFactuur.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use SumoCoders\DeFactuur\Exception as DeFactuurException;
use SumoCoders\DeFactuur\Client\Client;
Expand Down Expand Up @@ -107,18 +108,54 @@ private function encodeData($data, array $array = [], ?string $prefix = null): a
return $array;
}

private function doCallAndReturnStatusCode(
string $url,
?array $parameters = null,
string $method = 'GET'
): int {
return $this->doCall($url, $parameters, $method)->getStatusCode();
}

/**
* Make the call
*
* @return array|bool|string
*/
private function doCallAndReturnData(
string $url,
?array $parameters = null,
string $method = 'GET'
) {
$response = $this->doCall($url, $parameters, $method);

if (stristr($url, '.pdf')) {
// Return pdf contents immediately without tampering with them
return $response->getBody()->getContents();
}

$json = json_decode($response->getBody()->getContents(), true);

// validate json
if($json === false) throw new DeFactuurException('Invalid JSON-response');

// decode the response
array_walk_recursive($json, array(__CLASS__, 'decodeResponse'));

// return
return $json;
}

/**
* Make the call
*
* @return array|bool|string|int
* @throws DeFactuurException
*/
private function doCall(
string $url,
?array $parameters = null,
string $method = 'GET',
bool $returnHeaders = false
) {
string $method = 'GET'
): ResponseInterface {
$data = null;

// add credentials
Expand Down Expand Up @@ -192,24 +229,7 @@ private function doCall(
throw new DeFactuurException('Invalid response (' . $response->getStatusCode() . ')', $response->getStatusCode());
}

// return the headers if needed
if($returnHeaders) return $response->getHeaders();

if (stristr($url, '.pdf')) {
// Return pdf contents immediately without tampering with them
return $response->getBody()->getContents();
}

$json = json_decode($response->getBody()->getContents(), true);

// validate json
if($json === false) throw new DeFactuurException('Invalid JSON-response');

// decode the response
array_walk_recursive($json, array(__CLASS__, 'decodeResponse'));

// return
return $json;
return $response;
}

/**
Expand Down Expand Up @@ -342,7 +362,7 @@ public function accountApiToken(string $username, string $password): string
public function clients(): array
{
$clients = array();
$rawData = $this->doCall('clients.json');
$rawData = $this->doCallAndReturnData('clients.json');
if (!empty($rawData)) {
foreach ($rawData as $data) {
$clients[] = Client::initializeWithRawData($data);
Expand All @@ -360,7 +380,7 @@ public function clients(): array
*/
public function clientsGet(string $id)
{
$rawData = $this->doCall('clients/' . $id . '.json');
$rawData = $this->doCallAndReturnData('clients/' . $id . '.json');
if(empty($rawData)) return false;

return Client::initializeWithRawData($rawData);
Expand All @@ -374,7 +394,7 @@ public function clientsGet(string $id)
*/
public function clientsGetByEmail(string $email)
{
$rawData = $this->doCall('clients.json', array('email' => $email));
$rawData = $this->doCallAndReturnData('clients.json', array('email' => $email));
if (empty($rawData)) {
return false;
}
Expand All @@ -395,7 +415,7 @@ function (array $clientData) {
public function clientsCreate(Client $client): Client
{
$parameters['client'] = $client->toArray(true);
$rawData = $this->doCall('clients.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('clients.json', $parameters, 'POST');

return Client::initializeWithRawData($rawData);
}
Expand All @@ -408,9 +428,9 @@ public function clientsCreate(Client $client): Client
public function clientsUpdate(string $id, Client $client): bool
{
$parameters['client'] = $client->toArray(true);
$rawData = $this->doCall('clients/' . $id . '.json', $parameters, 'PUT', true);
$rawData = $this->doCallAndReturnStatusCode('clients/' . $id . '.json', $parameters, 'PUT', true);

return ($rawData['http_code'] == 204);
return $rawData === 204;
}

/**
Expand All @@ -421,7 +441,7 @@ public function clientsUpdate(string $id, Client $client): bool
public function clientsIsEuropean(string $countryCode): bool
{
$parameters['country_code'] = $countryCode;
$rawData = $this->doCall('clients/is_european.json', $parameters);
$rawData = $this->doCallAndReturnData('clients/is_european.json', $parameters);

return $rawData['european'];
}
Expand All @@ -433,9 +453,9 @@ public function clientsIsEuropean(string $countryCode): bool
*/
public function clientsDelete(string $id): bool
{
$rawData = $this->doCall('clients/' . $id . '.json', null, 'DELETE', true);
$rawData = $this->doCallAndReturnStatusCode('clients/' . $id . '.json', null, 'DELETE', true);

return ($rawData['http_code'] == 204);
return $rawData === 204;
}

/**
Expand All @@ -446,9 +466,9 @@ public function clientsDelete(string $id): bool
public function clientsDisable(string $id, string $replacedById): bool
{
$parameters['replaced_by_id'] = $replacedById;
$rawData = $this->doCall('clients/' . $id . '/disable.json', $parameters, 'POST', true);
$rawData = $this->doCallAndReturnStatusCode('clients/' . $id . '/disable.json', $parameters, 'POST', true);

return ($rawData['http_code'] == 201);
return $rawData === 201;
}

/**
Expand All @@ -459,7 +479,7 @@ public function clientsDisable(string $id, string $replacedById): bool
public function clientsInvoices(int $id): array
{
$invoices = array();
$rawData = $this->doCall('clients/' . $id . '/invoices.json');
$rawData = $this->doCallAndReturnData('clients/' . $id . '/invoices.json');
if (!empty($rawData)) {
foreach ($rawData as $data) {
$invoices[] = Invoice::initializeWithRawData($data);
Expand Down Expand Up @@ -504,7 +524,7 @@ function($filter) use ($allowedFilters) {
}

$invoices = array();
$rawData = $this->doCall('invoices.json', $parameters);
$rawData = $this->doCallAndReturnData('invoices.json', $parameters);
if (!empty($rawData)) {
foreach ($rawData as $data) {
$invoices[] = Invoice::initializeWithRawData($data);
Expand All @@ -522,7 +542,7 @@ function($filter) use ($allowedFilters) {
*/
public function invoicesGet(string $id)
{
$rawData = $this->doCall('invoices/' . $id . '.json');
$rawData = $this->doCallAndReturnData('invoices/' . $id . '.json');
if(empty($rawData)) return false;

return Invoice::initializeWithRawData($rawData);
Expand All @@ -536,7 +556,7 @@ public function invoicesGet(string $id)
*/
public function invoicesGetAsPdf(string $id)
{
$rawData = $this->doCall('invoices/' . $id . '.pdf');
$rawData = $this->doCallAndReturnData('invoices/' . $id . '.pdf');

if (empty($rawData)) {
return false;
Expand All @@ -553,7 +573,7 @@ public function invoicesGetAsPdf(string $id)
*/
public function invoicesGetByIid(string $iid)
{
$rawData = $this->doCall('invoices/by_iid/' . $iid . '.json');
$rawData = $this->doCallAndReturnData('invoices/by_iid/' . $iid . '.json');
if(empty($rawData)) return false;

return Invoice::initializeWithRawData($rawData);
Expand All @@ -572,7 +592,7 @@ public function invoicesCreate(Invoice $invoice): Invoice
}

$parameters['invoice'] = $invoice->toArray(true);
$rawData = $this->doCall('invoices.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('invoices.json', $parameters, 'POST');

return Invoice::initializeWithRawData($rawData);
}
Expand All @@ -585,7 +605,7 @@ public function invoicesCreate(Invoice $invoice): Invoice
public function invoicesCreateCreditNote(string $id, Invoice $creditNote): Invoice
{
$parameters['credit_note'] = $creditNote->toArray(true);
$rawData = $this->doCall('invoices/' . $id . '/credit_notes.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('invoices/' . $id . '/credit_notes.json', $parameters, 'POST');

return Invoice::initializeWithRawData($rawData);
}
Expand All @@ -598,9 +618,9 @@ public function invoicesCreateCreditNote(string $id, Invoice $creditNote): Invoi
public function invoicesUpdate(string $id, Invoice $invoice): bool
{
$parameters['invoice'] = $invoice->toArray(true);
$rawData = $this->doCall('invoices/' . $id . '.json', $parameters, 'PUT', true);
$rawData = $this->doCallAndReturnStatusCode('invoices/' . $id . '.json', $parameters, 'PUT', true);

return ($rawData['http_code'] == 204);
return $rawData === 204;
}

/**
Expand All @@ -610,9 +630,9 @@ public function invoicesUpdate(string $id, Invoice $invoice): bool
*/
public function invoicesDelete(int $id): bool
{
$rawData = $this->doCall('invoices/' . $id . '.json', null, 'DELETE', true);
$rawData = $this->doCallAndReturnStatusCode('invoices/' . $id . '.json', null, 'DELETE', true);

return ($rawData['http_code'] == 204);
return $rawData === 204;
}

/**
Expand All @@ -635,7 +655,7 @@ public function invoiceSendByMail(
if($bcc !== null) $parameters['mail']['bcc'] = $bcc;
if($subject !== null) $parameters['mail']['subject'] = $subject;
if($text !== null) $parameters['mail']['text'] = $text;
$rawData = $this->doCall('invoices/' . $id . '/mails.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('invoices/' . $id . '/mails.json', $parameters, 'POST');
if(empty($rawData)) return false;

return Mail::initializeWithRawData($rawData);
Expand All @@ -651,7 +671,7 @@ public function invoiceMarkAsSentByMail(string $id, string $email): void
$parameters = array();
$parameters['by'] = 'mail';
$parameters['to'] = $email;
$this->doCall('invoices/' . $id . '/sent', $parameters, 'POST');
$this->doCallAndReturnData('invoices/' . $id . '/sent', $parameters, 'POST');
}

/**
Expand All @@ -662,7 +682,7 @@ public function invoiceMarkAsSentByMail(string $id, string $email): void
public function invoicesAddPayment(string $id, Payment $payment): Payment
{
$parameters['payment'] = $payment->toArray(true);
$rawData = $this->doCall('invoices/' . $id . '/payments.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('invoices/' . $id . '/payments.json', $parameters, 'POST');

return Payment::initializeWithRawData($rawData);
}
Expand All @@ -676,7 +696,7 @@ public function invoicesAddPayment(string $id, Payment $payment): Payment
*/
public function invoiceSendReminder(string $id): array
{
return $this->doCall('invoices/' . $id . '/reminders', array(), 'POST');
return $this->doCallAndReturnData('invoices/' . $id . '/reminders', array(), 'POST');
}

/**
Expand All @@ -688,7 +708,7 @@ public function invoicesVatRequired(string $countryCode, bool $isCompany): bool
{
$parameters['country_code'] = $countryCode;
$parameters['company'] = $isCompany;
$rawData = $this->doCall('invoices/vat_required.json', $parameters);
$rawData = $this->doCallAndReturnData('invoices/vat_required.json', $parameters);

return $rawData['vat_required'];
}
Expand All @@ -701,7 +721,7 @@ public function invoicesVatRequired(string $countryCode, bool $isCompany): bool
public function isValidVat(string $vatNumber): bool
{
$parameters['vat'] = $vatNumber;
$rawData = $this->doCall('vat/verify.json', $parameters);
$rawData = $this->doCallAndReturnData('vat/verify.json', $parameters);

return $rawData['valid'];
}
Expand All @@ -718,7 +738,7 @@ public function uploadCodaFile(string $filePath): array
'file_type' => 'coda',
);

return $this->doCall('payments/process_file.json', $parameters, 'POST');
return $this->doCallAndReturnData('payments/process_file.json', $parameters, 'POST');
}

/**
Expand All @@ -727,7 +747,7 @@ public function uploadCodaFile(string $filePath): array
public function products(): array
{
$products = array();
$rawData = $this->doCall('products.json');
$rawData = $this->doCallAndReturnData('products.json');

if (!empty($rawData)) {
foreach ($rawData as $data) {
Expand All @@ -744,7 +764,7 @@ public function products(): array
*/
public function productsGet(int $id)
{
$rawData = $this->doCall('products/' . $id . '.json');
$rawData = $this->doCallAndReturnData('products/' . $id . '.json');

if (empty($rawData)) {
return false;
Expand All @@ -760,7 +780,7 @@ public function productsCreate(Product $product): Product
{
$parameters['product'] = $product->toArray();

$rawData = $this->doCall('products.json', $parameters, 'POST');
$rawData = $this->doCallAndReturnData('products.json', $parameters, 'POST');

return Product::initializeWithRawData($rawData);
}
Expand Down

0 comments on commit 2c95f6f

Please sign in to comment.