62 lines
2.0 KiB
PHP
62 lines
2.0 KiB
PHP
<?php
|
|
namespace ProVM\Service;
|
|
|
|
use DateTimeImmutable;
|
|
use ProVM\Exception\Response\EmptyResponse;
|
|
use ProVM\Exception\Response\MissingResponse;
|
|
use Psr\Log\LoggerInterface;
|
|
use function Safe\json_decode;
|
|
|
|
class Mailboxes
|
|
{
|
|
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, protected int $min_check_days) {}
|
|
|
|
public function getAll(): array
|
|
{
|
|
$this->logger->info('Getting all registered mailboxes');
|
|
$response = $this->communicator->get('/mailboxes/registered');
|
|
$body = $response->getBody()->getContents();
|
|
if (trim($body) === '') {
|
|
return [];
|
|
}
|
|
$json = json_decode($body);
|
|
if (!isset($json->mailboxes)) {
|
|
return [];
|
|
}
|
|
return $json->mailboxes;
|
|
}
|
|
public function check(object $mailbox): bool
|
|
{
|
|
$this->logger->info("Checking mailbox {$mailbox->id}");
|
|
if ((new DateTimeImmutable())->diff(new DateTimeImmutable($mailbox->last_checked->date->date))->days < $this->min_check_days) {
|
|
return true;
|
|
}
|
|
$uri = "/mailbox/{$mailbox->id}/check";
|
|
$response = $this->communicator->get($uri);
|
|
$body = $response->getBody()->getContents();
|
|
if (trim($body) === '') {
|
|
throw new EmptyResponse($uri);
|
|
}
|
|
$json = json_decode($body);
|
|
if (!isset($json->status)) {
|
|
throw new MissingResponse('status');
|
|
}
|
|
return $json->status;
|
|
}
|
|
public function grabMessages(int $mailbox_id): int
|
|
{
|
|
$this->logger->info("Grabbing messages for {$mailbox_id}");
|
|
$uri = "/mailbox/{$mailbox_id}/messages/grab";
|
|
$response = $this->communicator->get($uri);
|
|
$body = $response->getBody()->getContents();
|
|
if (trim($body) === '') {
|
|
return 0;
|
|
}
|
|
$json = json_decode($body);
|
|
if (!isset($json->count)) {
|
|
return 0;
|
|
}
|
|
return $json->count;
|
|
}
|
|
}
|