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 []= $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 $this->withJson($response, $output); } public function unregister(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): 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) { return $mailbox->toArray(); }, $data); return $this->withJson($response, compact('mailboxes')); } public function get(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository, int $mailbox_id): ResponseInterface { $mailbox = $repository->fetchById($mailbox_id); return $this->withJson($response, ['mailbox' => $mailbox->toArray()]); } }