| 1 |
<?php |
| 2 |
|
| 3 |
namespace App\Billingo\Service; |
| 4 |
|
| 5 |
use App\Billingo\Enums\BillingoErrorEnum; |
| 6 |
use App\Billingo\Enums\HttpMethodEnum; |
| 7 |
use GuzzleHttp\Client; |
| 8 |
use GuzzleHttp\Exception\GuzzleException; |
| 9 |
use GuzzleHttp\Psr7\Response; |
| 10 |
|
| 11 |
class BillingoConnector |
| 12 |
{ |
| 13 |
const BASE_URI = 'https://api.billingo.hu/v3/'; |
| 14 |
private readonly Client $client; |
| 15 |
private array $header; |
| 16 |
private static ?BillingoConnector $instance = null; |
| 17 |
|
| 18 |
public function __construct(string $apiKey) |
| 19 |
{ |
| 20 |
$this->client = new Client(['base_uri' => self::BASE_URI]); |
| 21 |
$this->header = [ |
| 22 |
'X-API-KEY' => $apiKey, |
| 23 |
'Content-Type' => 'application/json', |
| 24 |
'Accept' => 'application/json', |
| 25 |
'X-Plugin-Name' => 'Woccomerce 4.0', |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
public static function getInstance(string $apiKey = ''): BillingoConnector |
| 30 |
{ |
| 31 |
if (self::$instance === null) { |
| 32 |
self::$instance = new self($apiKey); |
| 33 |
} |
| 34 |
|
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
public function send(ApiContext $apiContext, string $outputFile = null): BillingoResponse |
| 39 |
{ |
| 40 |
if (!$apiContext->selfControl()) { |
| 41 |
|
| 42 |
return new BillingoResponse( |
| 43 |
BillingoErrorEnum::BAD_API_CONTEXT->value, |
| 44 |
0, |
| 45 |
errors: ['bad context'], |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
try { |
| 50 |
$response = $this->client->request( |
| 51 |
$apiContext->getMethod()->value, |
| 52 |
$apiContext->getUrl(), |
| 53 |
[ |
| 54 |
'headers' => $this->header, |
| 55 |
'query' => $apiContext->getMethod() == HttpMethodEnum::GET |
| 56 |
? $apiContext->getContent() |
| 57 |
: null, |
| 58 |
'body' => $apiContext->getMethod() != HttpMethodEnum::GET |
| 59 |
? json_encode($apiContext->getContent()) |
| 60 |
: null, |
| 61 |
] |
| 62 |
); |
| 63 |
|
| 64 |
$contentType = $response->getHeaderLine('Content-Type'); |
| 65 |
|
| 66 |
return str_contains($contentType, 'application/json') |
| 67 |
? $this->getJsonResponse($response) |
| 68 |
: $this->getPdfResponse($response, $outputFile); |
| 69 |
|
| 70 |
} catch (GuzzleException $exception) { |
| 71 |
|
| 72 |
$response = $exception->getResponse(); |
| 73 |
|
| 74 |
return new BillingoResponse( |
| 75 |
$response->getReasonPhrase(), |
| 76 |
$response->getStatusCode(), |
| 77 |
data: billingoCollection([]), |
| 78 |
errors: json_decode($response->getBody()->getContents(), true) |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
protected function getPdfResponse(Response $response, ?string $outputFile): BillingoResponse |
| 84 |
{ |
| 85 |
$this->header['Accept'] = $response->getHeaderLine('Content-Type'); |
| 86 |
|
| 87 |
$pdfContent = $response->getBody()->getContents(); |
| 88 |
|
| 89 |
$billingoResponse = new BillingoResponse($response->getReasonPhrase(), $response->getStatusCode()); |
| 90 |
|
| 91 |
if ($outputFile) { |
| 92 |
file_put_contents($outputFile, $pdfContent); |
| 93 |
|
| 94 |
$billingoResponse->setData(["message" => "PDF saved to $outputFile"]); |
| 95 |
|
| 96 |
} else { |
| 97 |
|
| 98 |
$billingoResponse->setData(["content" => $pdfContent]); |
| 99 |
} |
| 100 |
|
| 101 |
return $billingoResponse; |
| 102 |
} |
| 103 |
|
| 104 |
protected function getJsonResponse(Response $response): BillingoResponse |
| 105 |
{ |
| 106 |
$body = json_decode($response->getBody()->getContents(), true); |
| 107 |
|
| 108 |
$meta = isset($body['data']) ? array_diff_key($body, ['data' => '']) : []; |
| 109 |
$data = !empty($meta) |
| 110 |
? (empty($body['data']) ? billingoCollection([]) : $body['data']) |
| 111 |
: $body; |
| 112 |
|
| 113 |
return new BillingoResponse( |
| 114 |
$response->getReasonPhrase(), |
| 115 |
$response->getStatusCode(), |
| 116 |
$meta, |
| 117 |
$data, |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|