Tests de Repositories
This commit is contained in:
165
app/src/Service/MediosPago/AbstractEndPoint.php
Normal file
165
app/src/Service/MediosPago/AbstractEndPoint.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?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;
|
||||
}
|
42
app/src/Service/MediosPago/EndPoint.php
Normal file
42
app/src/Service/MediosPago/EndPoint.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\MediosPago;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
|
||||
interface EndPoint
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @return array
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function getById(string $id): array;
|
||||
/**
|
||||
* @param string $id
|
||||
* @return array
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function get(string $id): array;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function add(array $data): bool;
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function edit(string $id, array $data): bool;
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @return void
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
public function delete(string $id): void;
|
||||
}
|
Reference in New Issue
Block a user