Files
emails/cli/common/Command/Mailboxes/Check.php
2023-06-12 21:14:07 -04:00

54 lines
1.8 KiB
PHP

<?php
namespace ProVM\Command\Mailboxes;
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;
use ProVM\Service\Mailboxes;
#[AsCommand(
name: 'mailboxes:check',
description: 'Check registered mailboxes for new emails',
hidden: false
)]
class Check extends Command
{
public function __construct(protected Mailboxes $service, string $name = null)
{
parent::__construct($name);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$section1 = $output->section();
$section2 = $output->section();
$io1 = new SymfonyStyle($input, $section1);
$io2 = new SymfonyStyle($input, $section2);
$io1->title('Checking for New Messages');
$mailboxes = $this->service->getAll();
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
$io1->text($notice);
if (count($mailboxes) > 0) {
$io1->section('Checking for new messages');
$io1->progressStart(count($mailboxes));
foreach ($mailboxes as $mailbox) {
$section2->clear();
$io2->text("Checking {$mailbox->name}");
if ($this->service->check($mailbox)) {
$io2->success("Found new emails in {$mailbox->name}");
} else {
$io2->info("No new emails in {$mailbox->name}");
}
$io1->progressAdvance();
}
$io1->progressFinish();
}
$section2->clear();
$io2->success('Done');
return Command::SUCCESS;
}
}