Cleanup of cli
This commit is contained in:
76
cli/common/Command/Mailboxes/Check.php
Normal file
76
cli/common/Command/Mailboxes/Check.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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 ProVM\Service\Communicator;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use function Safe\json_decode;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mailboxes:check',
|
||||
description: 'Check registered mailboxes for new emails',
|
||||
hidden: false
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected int $min_check_days, string $name = null)
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
protected function getMailboxes(): array
|
||||
{
|
||||
$response = $this->communicator->get('/mailboxes/registered');
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return [];
|
||||
}
|
||||
return json_decode($body)->mailboxes;
|
||||
}
|
||||
protected function checkMailbox($mailbox): bool
|
||||
{
|
||||
if ((new \DateTimeImmutable())->diff(new \DateTimeImmutable($mailbox->last_checked->date->date))->days < $this->min_check_days) {
|
||||
return true;
|
||||
}
|
||||
$response = $this->communicator->get("/mailbox/{$mailbox->id}/check");
|
||||
$body = $response->getBody()->getContents();
|
||||
if (trim($body) === '') {
|
||||
return true;
|
||||
}
|
||||
return json_decode($body)->status;
|
||||
}
|
||||
|
||||
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->getMailboxes();
|
||||
$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->checkMailbox($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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user