166 lines
5.4 KiB
PHP
166 lines
5.4 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Service\MediosPago;
|
||
|
|
||
|
use HttpException;
|
||
|
use PDOException;
|
||
|
use Psr\Http\Client\ClientExceptionInterface;
|
||
|
use Psr\Http\Client\ClientInterface;
|
||
|
use Psr\Http\Message\ResponseInterface;
|
||
|
use Incoviba\Common\Define\Repository;
|
||
|
use Incoviba\Common\Implement\Exception\{EmptyResponse, EmptyResult};
|
||
|
use Incoviba\Common\Ideal\LoggerEnabled;
|
||
|
|
||
|
abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
|
||
|
{
|
||
|
public function __construct(protected ClientInterface $client) {}
|
||
|
|
||
|
/**
|
||
|
* @param ResponseInterface $response
|
||
|
* @param string $request_uri
|
||
|
* @param array $validStatus
|
||
|
* @param array $invalidStatus
|
||
|
* @return void
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function validateResponse(ResponseInterface $response, string $request_uri, array $validStatus, array $invalidStatus): void
|
||
|
{
|
||
|
$status = $response->getStatusCode();
|
||
|
$reason = $response->getReasonPhrase();
|
||
|
$contents = $response->getBody()->getContents();
|
||
|
|
||
|
if (in_array($status, $invalidStatus)) {
|
||
|
$exception = new HttpException("{$reason}\n{$contents}", $status);
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
if (!in_array($status, $validStatus)) {
|
||
|
$exception = new HttpException("{$reason}\n{$contents}", $status);
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $request_uri
|
||
|
* @param array $validStatus
|
||
|
* @param array $invalidStatus
|
||
|
* @return string
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function sendGet(string $request_uri, array $validStatus, array $invalidStatus): array
|
||
|
{
|
||
|
try {
|
||
|
$response = $this->client->get($request_uri);
|
||
|
} catch (ClientExceptionInterface $exception) {
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
|
||
|
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
|
||
|
|
||
|
return json_decode($response->getBody()->getContents(), true);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $request_uri
|
||
|
* @param array $data
|
||
|
* @param array $validStatus
|
||
|
* @param array $invalidStatus
|
||
|
* @return string
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
|
||
|
{
|
||
|
$params = $this->mapParams($data);
|
||
|
try {
|
||
|
$response = $this->client->post($request_uri, ['json' => $params]);
|
||
|
} catch (ClientExceptionInterface $exception) {
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
|
||
|
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
|
||
|
|
||
|
$contents = $response->getBody()->getContents();
|
||
|
$json = json_decode($contents, true);
|
||
|
return $this->save($json);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $request_uri
|
||
|
* @param array $data
|
||
|
* @param array $validStatus
|
||
|
* @param array $invalidStatus
|
||
|
* @return string
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool
|
||
|
{
|
||
|
$params = $this->mapParams($data);
|
||
|
try {
|
||
|
$response = $this->client->put($request_uri, ['json' => $params]);
|
||
|
} catch (ClientExceptionInterface $exception) {
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
|
||
|
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
|
||
|
|
||
|
$contents = $response->getBody()->getContents();
|
||
|
$json = json_decode($contents, true);
|
||
|
return $this->save($json);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $request_uri
|
||
|
* @param array $validStatus
|
||
|
* @param array $invalidStatus
|
||
|
* @return void
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void
|
||
|
{
|
||
|
try {
|
||
|
$response = $this->client->delete($request_uri);
|
||
|
} catch (ClientExceptionInterface $exception) {
|
||
|
throw new EmptyResponse($request_uri, $exception);
|
||
|
}
|
||
|
|
||
|
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
|
||
|
}
|
||
|
protected function doSave(Repository $repository, array $data): bool
|
||
|
{
|
||
|
try {
|
||
|
$repository->fetchById($data['id']);
|
||
|
return true;
|
||
|
} catch (EmptyResult) {
|
||
|
$mappedData = $this->mapSave($data);
|
||
|
$filteredData = $repository->filterData($mappedData);
|
||
|
$model = $repository->create($filteredData);
|
||
|
try {
|
||
|
$repository->save($model);
|
||
|
return true;
|
||
|
} catch (PDOException $exception) {
|
||
|
$this->logger->warning($exception);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param callable $callable
|
||
|
* @param string $id
|
||
|
* @param string $message
|
||
|
* @return array
|
||
|
* @throws EmptyResponse
|
||
|
*/
|
||
|
protected function doGetById(callable $callable, string $id, string $message): array
|
||
|
{
|
||
|
try {
|
||
|
$model = $callable($id);
|
||
|
return json_decode(json_encode($model), true);
|
||
|
} catch (EmptyResult $exception) {
|
||
|
throw new EmptyResponse($message, $exception);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
abstract protected function mapParams(array $data): array;
|
||
|
abstract protected function mapSave(array $data): array;
|
||
|
abstract protected function save(array $data): bool;
|
||
|
}
|