PluginProbe
Billingo Official for WooCommerce / 4.2.9
Billingo Official for WooCommerce v4.2.9
4.3.3 4.3.2 trunk 0.0.2 1.0.0 2.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 All 82 releases
billingo / src / Service / BillingoConnector.php

BillingoConnector.php in Billingo Official for WooCommerce 4.2.9, at src/Service/BillingoConnector.php

121 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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