Job Queue

This commit is contained in:
Juan Pablo Vial
2025-05-10 12:30:35 -04:00
parent 61324f159b
commit 8b2de31e02
6 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace Incoviba\Service\Worker;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Model;
class Request extends Ideal\Service
{
public function __construct(LoggerInterface $logger, protected ClientInterface $client)
{
parent::__construct($logger);
}
/**
* @param Model\Job $job
* @return bool
* @throws EmptyResponse
*/
public function execute(Model\Job $job): bool
{
$url = $job->configuration['url'];
$method = strtolower($job->configuration['method']);
$body = $job->configuration['body'];
try {
$response = $this->client->{$method}($url, [
'json' => $body,
]);
} catch (ClientExceptionInterface $exception) {
throw new EmptyResponse($url, $exception);
}
$statusCode = $response->getStatusCode();
if ((int) floor($statusCode / 100) !== 2) {
throw new EmptyResponse($url);
}
if ($statusCode !== 204) {
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);
if (!isset($data['success']) or !$data['success']) {
throw new EmptyResponse($url);
}
}
return true;
}
}