Job Queue
This commit is contained in:
50
app/src/Service/Worker/Request.php
Normal file
50
app/src/Service/Worker/Request.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user