Skip to content

Commit

Permalink
Merge pull request #12 from Morning-Train/hotfix/economic-response-data
Browse files Browse the repository at this point in the history
Hotfix/economic response data
  • Loading branch information
matbaek authored May 17, 2024
2 parents b19c515 + 62844cb commit 93e0899
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/Drivers/WordPressEconomicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Morningtrain\Economic\Classes\EconomicResponse;
use Morningtrain\Economic\Interfaces\EconomicDriver;
use Morningtrain\Economic\Services\EconomicLoggerService;
use Morningtrain\WpEconomic\Exceptions\ExceptionWithData;

class WordPressEconomicDriver implements EconomicDriver
{
Expand Down Expand Up @@ -45,13 +46,16 @@ public function get(string $url, array $queryArgs = []): EconomicResponse
$body = json_decode(wp_remote_retrieve_body($response), true);

if (isset($body['message'])) {
throw new Exception($body['message']);
throw new ExceptionWithData(
message: $body['message'],
extraData: $body,
);
}

throw new Exception($response['response']['message']);
}

return new EconomicResponse($responseCode, json_decode(wp_remote_retrieve_body($response), true));
return new EconomicResponse($responseCode, $this->prepareResponseBody($response));
}

public function post(string $url, array $body = []): EconomicResponse
Expand All @@ -77,13 +81,16 @@ public function post(string $url, array $body = []): EconomicResponse
$body = json_decode(wp_remote_retrieve_body($response), true);

if (isset($body['message'])) {
throw new Exception($body['message']);
throw new ExceptionWithData(
message: $body['message'],
extraData: $body,
);
}

throw new Exception($response['response']['message']);
}

return new EconomicResponse(wp_remote_retrieve_response_code($response), json_decode(wp_remote_retrieve_body($response), true));
return new EconomicResponse(wp_remote_retrieve_response_code($response), $this->prepareResponseBody($response));

}

Expand Down Expand Up @@ -111,13 +118,16 @@ public function put(string $url, array $body = []): EconomicResponse
$body = json_decode(wp_remote_retrieve_body($response), true);

if (isset($body['message'])) {
throw new Exception($body['message']);
throw new ExceptionWithData(
message: $body['message'],
extraData: $body,
);
}

throw new Exception($response['response']['message']);
}

return new EconomicResponse(wp_remote_retrieve_response_code($response), json_decode(wp_remote_retrieve_body($response), true));
return new EconomicResponse(wp_remote_retrieve_response_code($response), $this->prepareResponseBody($response));

}

Expand All @@ -143,13 +153,16 @@ public function delete(string $url): EconomicResponse
$body = json_decode(wp_remote_retrieve_body($response), true);

if (isset($body['message'])) {
throw new Exception($body['message']);
throw new ExceptionWithData(
message: $body['message'],
extraData: $body,
);
}

throw new Exception($response['response']['message']);
}

return new EconomicResponse(wp_remote_retrieve_response_code($response), json_decode(wp_remote_retrieve_body($response), true) ?? []);
return new EconomicResponse(wp_remote_retrieve_response_code($response), $this->prepareResponseBody($response));

}

Expand All @@ -176,7 +189,7 @@ public function patch(string $url, array $body = []): EconomicResponse
throw new Exception($response['response']['message']);
}

return new EconomicResponse(wp_remote_retrieve_response_code($response), json_decode(wp_remote_retrieve_body($response), true) ?? []);
return new EconomicResponse(wp_remote_retrieve_response_code($response), $this->prepareResponseBody($response));

}

Expand All @@ -193,4 +206,15 @@ private function isSuccessful(int|string $responseCode): bool
{
return ($responseCode >= 200 && $responseCode < 300) || $responseCode === 404; // We consider 404 as a successful response, since it just mean that the resource not exists so we can return null;
}

private function prepareResponseBody($response): array|string
{
$contentType = wp_remote_retrieve_header($response, 'content-type');

if ($contentType === 'application/pdf') {
return $response['body'];
}

return json_decode(wp_remote_retrieve_body($response), true);
}
}
59 changes: 59 additions & 0 deletions src/Exceptions/ExceptionWithData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Morningtrain\WpEconomic\Exceptions;

use Exception;
use Throwable;

class ExceptionWithData extends Exception
{
protected array $extraData;

public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null, array $extraData = [])
{
parent::__construct($message, $code, $previous);

$this->extraData = $extraData;
}

public function getDetailedMessage(): string
{
$parts = [];

foreach ($this->extraData as $key => $value) {
if (is_array($value)) {
$parts[] = '<strong>'.ucfirst($key).'</strong>:'.PHP_EOL.$this->arrayToString($value);

continue;

}

$parts[] = '<strong>'.ucfirst($key).'</strong>:'.PHP_EOL.$value;
}

return implode(PHP_EOL, $parts);
}

private function arrayToString(array $array, int $indent = 0): string
{
$parts = [];

foreach ($array as $key => $value) {
if (is_array($value)) {
$parts[] = $this->arrayToString($value, $indent + 2);

continue;
}

if (is_numeric($key)) {
$parts[] = str_repeat(' ', $indent).'- '.$value;

continue;
}

$parts[] = str_repeat(' ', $indent).ucfirst($key).'- '.": $value";
}

return implode(PHP_EOL, $parts);
}
}

0 comments on commit 93e0899

Please sign in to comment.