Files
emails/api/common/Controller/Mailboxes.php

109 lines
4.2 KiB
PHP

<?php
namespace ProVM\Common\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Ddeboer\Imap\MailboxInterface;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Mailboxes as Service;
use ProVM\Emails\Model\Mailbox;
class Mailboxes
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$mailboxes = array_values(array_map(function(MailboxInterface $mailbox) use ($service) {
$arr = ['name' => $mailbox->getName(), 'registered' => false];
if ($service->isRegistered($mailbox->getName())) {
$mb = $service->getLocalMailbox($mailbox->getName());
$arr['id'] = $mb->getId();
$arr['registered'] = true;
}
return $arr;
}, $service->getAll()));
$output = [
'total' => count($mailboxes),
'mailboxes' => $mailboxes
];
return $this->withJson($response, $output);
}
public function registered(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$mailboxes = array_map(function(Mailbox $mailbox) {
return $mailbox->toArray();
}, $service->getRegistered());
$output = [
'total' => count($mailboxes),
'mailboxes' => $mailboxes
];
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface
{
$mailbox = $service->getRepository()->fetchById($mailbox_id);
return $this->withJson($response, ['mailbox' => $mailbox->toArray()]);
}
public function register(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
}
$output = [
'mailboxes' => $json->mailboxes,
'total' => count($json->mailboxes),
'registered' => [
'total' => 0,
'mailboxes' => []
]
];
foreach ($json->mailboxes as $mailbox_name) {
$arr = [
'id' => '',
'name' => $mailbox_name,
'registered' => false
];
if ($service->register($mailbox_name)) {
$mailbox = $service->getLocalMailbox($mailbox_name);
$arr['id'] = $mailbox->getId();
$arr['registered'] = true;
$output['registered']['total'] ++;
$output['registered']['mailboxes'] []= $arr;
$messagesService->grab($mailbox_name);
}
}
return $this->withJson($response, $output, 201);
}
public function unregister(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes ids');
}
$output = [
'mailboxes' => $json->mailboxes,
'total' => count($json->mailboxes),
'unregistered' => [
'total' => 0,
'mailboxes' => []
]
];
foreach ($json->mailboxes as $mailbox_id) {
$mailbox = $service->getRepository()->fetchById($mailbox_id);
if ($service->unregister($mailbox->getName())) {
$output['unregistered']['total'] ++;
$output['unregistered']['mailboxes'] []= [
'id' => $mailbox->getId(),
'name' => $mailbox->getName(),
'unregistered' => true
];
}
}
return $this->withJson($response, $output);
}
}