Full implemantation
This commit is contained in:
@ -1,83 +1,100 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Emails\Repository\Mailbox;
|
||||
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, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$source_mailboxes = $service->getAll();
|
||||
$mailboxes = [];
|
||||
foreach ($source_mailboxes as $mailbox) {
|
||||
$m = [
|
||||
'name' => $mailbox->getName(),
|
||||
'registered' => false
|
||||
];
|
||||
try {
|
||||
$s = $repository->fetchByName($mailbox->getName());
|
||||
$m['registered'] = true;
|
||||
$m['id'] = $s->getId();
|
||||
} catch (BlankResult $e) {
|
||||
$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;
|
||||
}
|
||||
$mailboxes []= $m;
|
||||
}
|
||||
return $this->withJson($response, compact('mailboxes'));
|
||||
}
|
||||
public function register(ServerRequestInterface $request, ResponseInterface $response, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$output = ['input' => $json, 'mailboxes' => []];
|
||||
foreach ($json->mailboxes as $mailbox) {
|
||||
try {
|
||||
$model = $repository->create([
|
||||
'name' => $mailbox
|
||||
]);
|
||||
$output['mailboxes'] []= ['name' => $mailbox, 'created' => true, 'id' => $model->getId()];
|
||||
} catch (\PDOException $e) {
|
||||
$output['mailboxes'] []= ['name' => $mailbox, 'created' => false];
|
||||
}
|
||||
}
|
||||
return $arr;
|
||||
}, $service->getAll()));
|
||||
$output = [
|
||||
'total' => count($mailboxes),
|
||||
'mailboxes' => $mailboxes
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function unregister(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): ResponseInterface
|
||||
public function registered(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$output = ['input' => $json, 'mailboxes' => []];
|
||||
foreach ($json->mailboxes as $id) {
|
||||
try {
|
||||
$mailbox = $repository->fetchById($id);
|
||||
try {
|
||||
$repository->delete($mailbox);
|
||||
$output['mailboxes'] []= ['name' => $mailbox->getName(), 'id' => $id, 'removed' => true];
|
||||
} catch (\PDOException $e) {
|
||||
$output['mailboxes'] []= ['name' => $mailbox->getName(), 'id' => $id, 'removed' => false];
|
||||
}
|
||||
} catch (\PDOException | BlankResult $e) {
|
||||
$output['mailboxes'] []= ['id' => $id, 'removed' => false];
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function registered(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$data = $repository->fetchAll();
|
||||
$mailboxes = array_map(function(\ProVM\Emails\Model\Mailbox $mailbox) {
|
||||
$mailboxes = array_map(function(Mailbox $mailbox) {
|
||||
return $mailbox->toArray();
|
||||
}, $data);
|
||||
return $this->withJson($response, compact('mailboxes'));
|
||||
}, $service->getRegistered());
|
||||
$output = [
|
||||
'total' => count($mailboxes),
|
||||
'mailboxes' => $mailboxes
|
||||
];
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository, int $mailbox_id): ResponseInterface
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface
|
||||
{
|
||||
$mailbox = $repository->fetchById($mailbox_id);
|
||||
$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 names');
|
||||
}
|
||||
$output = [
|
||||
'mailboxes' => $json->mailboxes,
|
||||
'total' => count($json->mailboxes),
|
||||
'unregistered' => 0
|
||||
];
|
||||
foreach ($json->mailboxes as $mailbox_name) {
|
||||
if ($service->unregister($mailbox_name)) {
|
||||
$output['unregistered'] ++;
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user