45 lines
1.5 KiB
PHP
45 lines
1.5 KiB
PHP
<?php
|
|
namespace ProVM\Common\Controller;
|
|
|
|
use ProVM\Emails\Model\Job;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use ProVM\Common\Exception\Request\MissingArgument;
|
|
use ProVM\Common\Implement\Controller\Json;
|
|
use ProVM\Common\Service\Jobs as Service;
|
|
|
|
class Jobs
|
|
{
|
|
use Json;
|
|
|
|
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $jobsService): ResponseInterface
|
|
{
|
|
$body = $request->getBody();
|
|
$json = \Safe\json_decode($body->getContents());
|
|
if (!isset($json->messages)) {
|
|
throw new MissingArgument('messages', 'array', 'messages ids');
|
|
}
|
|
$output = [
|
|
'messages' => $json->messages,
|
|
'total' => count($json->messages),
|
|
'scheduled' => 0
|
|
];
|
|
foreach ($json->messages as $message_id) {
|
|
if ($jobsService->schedule($message_id)) {
|
|
$output['scheduled'] ++;
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $jobsService): ResponseInterface
|
|
{
|
|
$pending = array_map(function(Job $job) {
|
|
return $job->toArray();
|
|
}, $jobsService->getPending());
|
|
$output = [
|
|
'total' => count($pending),
|
|
'pending' => $pending
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
} |