Files
emails/cli/common/Command/GrabAttachments.php
2022-11-29 08:57:56 -03:00

65 lines
2.1 KiB
PHP

<?php
namespace ProVM\Common\Command;
use ProVM\Common\Service\Communicator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'attachments:grab',
description: 'Grab attachments from pending messages',
aliases: ['attachments:get'],
hidden: false
)]
class GrabAttachments extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $service;
public function getCommunicator(): Communicator
{
return $this->service;
}
public function setCommunicator(Communicator $service): GrabAttachments
{
$this->service = $service;
return $this;
}
protected function getMessages(): array
{
$response = $this->getCommunicator()->get('/messages/pending');
return \Safe\json_decode($response->getBody()->getContents())->messages;
}
protected function grabAttachments(int $message_uid): int
{
$response = $this->getCommunicator()->put('/attachments/grab', ['messages' => [$message_uid]]);
return \Safe\json_decode($response->getBody()->getContents())->attachment_count;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Grab Attachments');
$io->section('Grabbing Messages');
$messages = $this->getMessages();
$io->text('Found ' . count($messages) . ' messages.');
$io->section('Grabbing Attachments');
foreach ($messages as $job) {
$message = $job->message;
$attachments = $this->grabAttachments($message->uid);
$io->text("Found {$attachments} attachments for message UID:{$message->uid}.");
}
$io->success('Done.');
return Command::SUCCESS;
}
}