-
Notifications
You must be signed in to change notification settings - Fork 10
Feat/ecommerce core #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| <?php | ||
|
|
||
| namespace MailerLite\Common; | ||
|
|
||
| use MailerLite\Http\ClientInterface; | ||
| use MailerLite\Http\RequestFactoryInterface; | ||
| use MailerLite\Http\HttpErrorMapper; | ||
| use Psr\Http\Message\ResponseInterface; | ||
|
|
||
| final class HttpLayerPsr | ||
| { | ||
| /** @var ClientInterface */ | ||
| private $client; | ||
|
|
||
| /** @var RequestFactoryInterface */ | ||
| private $requestFactory; | ||
|
|
||
| /** @var string */ | ||
| private $apiKey; | ||
|
|
||
| /** @var string */ | ||
| private $baseUrl; | ||
|
|
||
| /** @var array<string,string|string[]> */ | ||
| private $defaultHeaders; | ||
|
|
||
| /** | ||
| * @param array<string,string|string[]> $defaultHeaders | ||
| */ | ||
| public function __construct( | ||
| ClientInterface $client, | ||
| RequestFactoryInterface $requestFactory, | ||
| string $apiKey, | ||
| string $baseUrl = 'https://connect.mailerlite.com', | ||
| array $defaultHeaders = [ | ||
| 'Content-Type' => 'application/json', | ||
| 'Accept' => 'application/json', | ||
| ] | ||
| ) { | ||
| $this->client = $client; | ||
| $this->requestFactory = $requestFactory; | ||
| $this->apiKey = $apiKey; | ||
| $this->baseUrl = $baseUrl; | ||
| $this->defaultHeaders = $defaultHeaders; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<mixed>|bool|float|int|string|null> $query | ||
| */ | ||
| public function get(string $path, array $query = []): ResponseInterface | ||
| { | ||
| $uri = $this->buildUri($path, $query); | ||
| $req = $this->requestFactory->create('GET', $uri, $this->headers()); | ||
| $res = $this->client->sendRequest($req); | ||
| HttpErrorMapper::throwIfError($res); | ||
| return $res; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $payload | ||
| */ | ||
| public function post(string $path, array $payload = []): ResponseInterface | ||
| { | ||
| $uri = $this->buildUri($path); | ||
| $body = $this->encodeJsonBody($payload); | ||
| $req = $this->requestFactory->create('POST', $uri, $this->headers(), $body); | ||
| $res = $this->client->sendRequest($req); | ||
| HttpErrorMapper::throwIfError($res); | ||
| return $res; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $payload | ||
| */ | ||
| public function put(string $path, array $payload = []): ResponseInterface | ||
| { | ||
| $uri = $this->buildUri($path); | ||
| $body = $this->encodeJsonBody($payload); | ||
| $req = $this->requestFactory->create('PUT', $uri, $this->headers(), $body); | ||
| $res = $this->client->sendRequest($req); | ||
| HttpErrorMapper::throwIfError($res); | ||
| return $res; | ||
| } | ||
|
|
||
| public function delete(string $path): ResponseInterface | ||
| { | ||
| $uri = $this->buildUri($path); | ||
| $req = $this->requestFactory->create('DELETE', $uri, $this->headers()); | ||
| $res = $this->client->sendRequest($req); | ||
| HttpErrorMapper::throwIfError($res); | ||
| return $res; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<mixed>|bool|float|int|string|null> $query | ||
| */ | ||
| private function buildUri(string $path, array $query = []): string | ||
| { | ||
| $uri = rtrim($this->baseUrl, '/') . '/' . ltrim($path, '/'); | ||
| if ($query !== []) { | ||
| $uri .= '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986); | ||
| } | ||
| return $uri; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed> $payload | ||
| */ | ||
| private function encodeJsonBody(array $payload): ?string | ||
| { | ||
| if ($payload === []) { | ||
| return null; | ||
| } | ||
| return json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string,string|string[]> | ||
| */ | ||
| private function headers(): array | ||
| { | ||
| return ['Authorization' => 'Bearer ' . $this->apiKey] + $this->defaultHeaders; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?php | ||
|
|
||
| namespace MailerLite\Common; | ||
|
|
||
| use MailerLite\Exceptions\MailerLiteException; | ||
| use Psr\Http\Message\ResponseInterface; | ||
|
|
||
| final class HttpLayerPsrBridge extends HttpLayer | ||
| { | ||
| /** @var HttpLayerPsr */ | ||
| private $psrLayer; | ||
|
|
||
| public function __construct(HttpLayerPsr $psrLayer) | ||
| { | ||
| $this->psrLayer = $psrLayer; | ||
| } | ||
|
|
||
| /** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */ | ||
| public function get(string $uri, array $body = []): array | ||
| { | ||
| /** @var array<string, array<mixed>|bool|float|int|string|null> $query */ | ||
| $query = $this->sanitizeQuery($body); | ||
| return $this->executeAndConvert(fn() => $this->psrLayer->get($uri, $query)); | ||
| } | ||
|
|
||
| /** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */ | ||
| public function post(string $uri, array $body = []): array | ||
| { | ||
| return $this->executeAndConvert(fn() => $this->psrLayer->post($uri, $body)); | ||
| } | ||
|
|
||
| /** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */ | ||
| public function put(string $uri, array $body): array | ||
| { | ||
| return $this->executeAndConvert(fn() => $this->psrLayer->put($uri, $body)); | ||
| } | ||
|
|
||
| /** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */ | ||
| public function delete(string $uri, array $body = []): array | ||
| { | ||
| // Note: $body parameter is ignored for DELETE requests as per HTTP semantics | ||
| return $this->executeAndConvert(fn() => $this->psrLayer->delete($uri)); | ||
| } | ||
|
|
||
| /** @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} */ | ||
| public function request(string $method, string $uri, string $body = ''): array | ||
| { | ||
| $method = strtoupper($method); | ||
| $decodedBody = $this->decodeJsonBody($body); | ||
|
|
||
| switch ($method) { | ||
| case 'GET': | ||
| return $this->get($uri, []); | ||
| case 'POST': | ||
| return $this->post($uri, $decodedBody); | ||
| case 'PUT': | ||
| return $this->put($uri, $decodedBody); | ||
| case 'DELETE': | ||
| return $this->delete($uri, []); | ||
| default: | ||
| throw new MailerLiteException("Unsupported HTTP method: {$method}"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Execute PSR layer method and convert response to compatibility format | ||
| * @param callable(): ResponseInterface $psrMethod | ||
| * @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} | ||
| */ | ||
| private function executeAndConvert(callable $psrMethod): array | ||
| { | ||
| $response = $psrMethod(); | ||
| return $this->compatResponse($response); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<mixed> | ||
| */ | ||
| private function decodeJsonBody(string $body): array | ||
| { | ||
| if ($body === '') { | ||
| return []; | ||
| } | ||
|
|
||
| $decoded = json_decode($body, true); | ||
| return $decoded !== null ? (array) $decoded : []; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $in | ||
| * @return array<string, array<mixed>|bool|float|int|string|null> | ||
| */ | ||
| private function sanitizeQuery(array $in): array | ||
| { | ||
| /** @var array<string, array<mixed>|bool|float|int|string|null> $out */ | ||
| $out = []; | ||
| foreach ($in as $k => $v) { | ||
| if (is_array($v)) { | ||
| $out[$k] = array_map( | ||
| /** @return array<mixed>|bool|float|int|string|null */ | ||
| static function ($x) { | ||
| if (is_bool($x) || is_int($x) || is_float($x) || is_string($x) || $x === null) { | ||
| return $x; | ||
| } | ||
| if (is_object($x) && method_exists($x, '__toString')) { | ||
| return (string) $x; | ||
| } | ||
| $json = json_encode($x); | ||
| return $json !== false ? $json : get_debug_type($x); | ||
| }, | ||
| $v | ||
| ); | ||
| } elseif (is_bool($v) || is_int($v) || is_float($v) || is_string($v) || $v === null) { | ||
| $out[$k] = $v; | ||
| } else { | ||
| if (is_object($v) && method_exists($v, '__toString')) { | ||
| $out[$k] = (string) $v; | ||
| } else { | ||
| $json = json_encode($v); | ||
| $out[$k] = $json !== false ? $json : get_debug_type($v); | ||
| } | ||
| } | ||
| } | ||
| return $out; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{status_code:int,headers:array<string,string[]>,body:mixed,response:ResponseInterface} | ||
| */ | ||
| private function compatResponse(ResponseInterface $response): array | ||
| { | ||
| $headers = $response->getHeaders(); | ||
| $statusCode = $response->getStatusCode(); | ||
|
|
||
| $contentTypes = $response->getHeader('Content-Type'); | ||
| $contentType = $response->hasHeader('Content-Type') ? (string) reset($contentTypes) : null; | ||
|
|
||
| switch (true) { | ||
| case $contentType !== null && stripos($contentType, 'application/json') === 0: | ||
| $bodyStr = (string) $response->getBody(); | ||
| $body = $bodyStr !== '' ? json_decode($bodyStr, true) : null; | ||
| break; | ||
| default: | ||
| $body = (string) $response->getBody(); | ||
| } | ||
|
|
||
| return [ | ||
| 'status_code' => $statusCode, | ||
| 'headers' => $headers, | ||
| 'body' => $body, | ||
| 'response' => $response, | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.