Jobs setup
This commit is contained in:
@ -1,19 +1,20 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service\Attachments as Service;
|
||||
use ProVM\Emails\Model\Attachment;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service;
|
||||
use ProVM\Emails\Model\Attachment;
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
use function Safe\json_decode;
|
||||
|
||||
class Attachments
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service): ResponseInterface
|
||||
{
|
||||
$attachments = array_map(function(Attachment $attachment) {
|
||||
return $attachment->toArray();
|
||||
@ -24,35 +25,7 @@ class Attachments
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
if (!isset($json->messages)) {
|
||||
throw new MissingArgument('messages', 'array', 'message UIDs');
|
||||
}
|
||||
$output = [
|
||||
'messages' => $json->messages,
|
||||
'total' => count($json->messages),
|
||||
'saved' => [
|
||||
'attachments' => [],
|
||||
'total' => 0
|
||||
]
|
||||
];
|
||||
foreach ($json->messages as $message_id) {
|
||||
if (!$jobsService->isPending($message_id)) {
|
||||
continue;
|
||||
}
|
||||
if ($service->grab($message_id)) {
|
||||
$job = $jobsService->find($message_id);
|
||||
$jobsService->execute($job->getId());
|
||||
$output['saved']['attachments'] []= $job->toArray();
|
||||
$output['saved']['total'] ++;
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
|
||||
{
|
||||
$attachment = $service->getRepository()->fetchById($attachment_id);
|
||||
|
||||
@ -61,4 +34,25 @@ class Attachments
|
||||
$response->getBody()->write($service->getFile($attachment_id));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, Service\Messages $messagesService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
if (!isset($json->messages)) {
|
||||
throw new MissingArgument('messages', 'array', 'Messages UIDs');
|
||||
}
|
||||
$output = [
|
||||
'messages' => $json->messages,
|
||||
'attachments' => [],
|
||||
'total' => 0
|
||||
];
|
||||
foreach ($json->messages as $message_uid) {
|
||||
$message = $messagesService->getLocalMessage($message_uid);
|
||||
$attachments = $service->grab($message->getId());
|
||||
$output['attachments'] = array_merge($output['attachments'], $attachments);
|
||||
$output['total'] += count($attachments);
|
||||
}
|
||||
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user