Jobs setup
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
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\Messages as Service;
|
||||
use ProVM\Common\Service;
|
||||
use ProVM\Emails\Model\Message;
|
||||
use function Safe\json_decode;
|
||||
|
||||
@ -13,7 +13,7 @@ class Messages
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $mailbox_id): ResponseInterface
|
||||
{
|
||||
$mailbox = $service->getMailboxes()->get($mailbox_id);
|
||||
$messages = array_map(function(Message $message) {
|
||||
@ -37,7 +37,7 @@ class Messages
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachments, int $mailbox_id): ResponseInterface
|
||||
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Attachments $attachments, int $mailbox_id): ResponseInterface
|
||||
{
|
||||
$mailbox = $service->getMailboxes()->get($mailbox_id);
|
||||
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
|
||||
@ -63,32 +63,48 @@ class Messages
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $message_id): ResponseInterface
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $message_id): ResponseInterface
|
||||
{
|
||||
$message = $service->getRepository()->fetchById($message_id);
|
||||
return $this->withJson($response, ['message' => $message->toArray()]);
|
||||
}
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachmentsService): ResponseInterface
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, \ProVM\Common\Service\Attachments $attachmentsService, $mailbox_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'mailbox_id' => $mailbox_id,
|
||||
'messages' => [],
|
||||
'count' => 0
|
||||
];
|
||||
$mailbox = $service->getMailboxes()->get($mailbox_id);
|
||||
$messages = $service->grab($mailbox->getName());
|
||||
foreach ($messages as $message_id) {
|
||||
$message = $service->getLocalMessage($message_id);
|
||||
if ($message->hasValidAttachments()) {
|
||||
$attachmentsService->create($message->getId());
|
||||
}
|
||||
}
|
||||
$output['messages'] = $messages;
|
||||
$output['count'] = count($messages);
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Jobs $jobsService): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
if (!isset($json->mailboxes)) {
|
||||
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
|
||||
$json = json_decode($body);
|
||||
if (!isset($json->messages)) {
|
||||
throw new MissingArgument('messages', 'array', 'messages IDs');
|
||||
}
|
||||
$output = [
|
||||
'mailboxes' => $json->mailboxes,
|
||||
'messages' => [],
|
||||
'message_count' => 0
|
||||
'messages' => $json->messages,
|
||||
'scheduled' => 0
|
||||
];
|
||||
foreach ($json->mailboxes as $mailbox_name) {
|
||||
$messages = $service->grab($mailbox_name);
|
||||
foreach ($messages as $message) {
|
||||
if ($message->hasValidAttachments()) {
|
||||
$attachmentsService->create($message);
|
||||
}
|
||||
foreach ($json->messages as $message_id) {
|
||||
if ($jobsService->queue('attachments:grab', [$message_id])) {
|
||||
$message = $service->getRepository()->fetchById($message_id);
|
||||
$message->doesHaveScheduledDownloads();
|
||||
$service->getRepository()->save($message);
|
||||
$output['scheduled'] ++;
|
||||
}
|
||||
$output['messages'] = array_merge($output['messages'], $messages);
|
||||
$output['message_count'] += count($messages);
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
Reference in New Issue
Block a user