Files
emails/cli/common/Command/Messages/Grab.php
2023-06-09 00:54:34 -04:00

65 lines
1.9 KiB
PHP

<?php
namespace ProVM\Command\Messages;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use ProVM\Service\Communicator;
use function Safe\json_decode;
#[AsCommand(
name: 'messages:grab',
description: 'Run grab messages job for registered mailboxes',
hidden: false
)]
class Grab extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected function configure()
{
$this->addArgument('mailbox_id', InputArgument::REQUIRED, 'Mailbox ID to grab emails');
}
protected Communicator $communicator;
public function getCommunicator(): Communicator
{
return $this->communicator;
}
public function setCommunicator(Communicator $communicator): Grab
{
$this->communicator = $communicator;
return $this;
}
protected function grabMessages(int $mailbox_id): int
{
$response = $this->getCommunicator()->get("/mailbox/{$mailbox_id}/grab");
$body = $response->getBody()->getContents();
if (trim($body) === '') {
return 0;
}
return json_decode($body)->messages->count;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$mailbox_id = $input->getArgument('mailbox_id');
$io->title("Grabbing Messages for Mailbox ID {$mailbox_id}");
$io->section('Grabbing Messages');
$count = $this->grabMessages($mailbox_id);
$io->info("Found {$count} messages");
$io->success('Done.');
return Command::SUCCESS;
}
}