Jobs setup
This commit is contained in:
@ -5,9 +5,8 @@ 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;
|
||||
use ProVM\Service\Mailboxes;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mailboxes:check',
|
||||
@ -16,33 +15,11 @@ use function Safe\json_decode;
|
||||
)]
|
||||
class Check extends Command
|
||||
{
|
||||
public function __construct(protected Communicator $communicator, protected int $min_check_days, string $name = null)
|
||||
public function __construct(protected Mailboxes $service, 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();
|
||||
@ -50,7 +27,7 @@ class Check extends Command
|
||||
$io1 = new SymfonyStyle($input, $section1);
|
||||
$io2 = new SymfonyStyle($input, $section2);
|
||||
$io1->title('Checking for New Messages');
|
||||
$mailboxes = $this->getMailboxes();
|
||||
$mailboxes = $this->service->getAll();
|
||||
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
|
||||
$io1->text($notice);
|
||||
if (count($mailboxes) > 0) {
|
||||
@ -59,7 +36,7 @@ class Check extends Command
|
||||
foreach ($mailboxes as $mailbox) {
|
||||
$section2->clear();
|
||||
$io2->text("Checking {$mailbox->name}");
|
||||
if ($this->checkMailbox($mailbox)) {
|
||||
if ($this->service->check($mailbox)) {
|
||||
$io2->success("Found new emails in {$mailbox->name}");
|
||||
} else {
|
||||
$io2->info("No new emails in {$mailbox->name}");
|
||||
|
Reference in New Issue
Block a user