setLogger($logger); } protected LoggerInterface $logger; public function getLogger(): LoggerInterface { return $this->logger; } public function setLogger(LoggerInterface $logger): Emails { $this->logger = $logger; return $this; } public function mailboxes(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface { $mailboxes = array_map(function(MailboxInterface $mailbox) { return ['name' => $mailbox->getName(), 'message_count' => $mailbox->count()]; }, array_values($service->getMailboxes()->getAll())); return $this->withJson($response, compact('mailboxes')); } public function messages(ServerRequestInterface $request, ResponseInterface $response, Service $service, Mailbox $repository): ResponseInterface { $body = $request->getBody()->getContents(); $json = \Safe\json_decode($body); $mailbox = $repository->fetchById($json->mailbox); $remote_mailbox = $service->getMailboxes()->get($mailbox->getName()); $messages = $service->getMessages($remote_mailbox, $json->start ?? 0, $json->amount ?? null); $mails = []; foreach ($messages as $message) { $mails []= [ 'position' => $message->getPosition(), 'uid' => $message->getUID(), 'subject' => $message->getSubject(), 'date' => $message->getDateTime()->format('Y-m-d H:i:s'), 'from' => $message->getFrom(), 'has_attachments' => $message->hasAttachments(), 'valid_attachments' => $message->hasValidAttachments(), 'downloaded_attachments' => $message->hasDownloadedAttachments() ]; } return $this->withJson($response, [ 'input' => $json, 'total' => $mailbox->count(), 'messages' => $mails ]); } public function withAttachments(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface { $body = $request->getBody()->getContents(); $json = \Safe\json_decode($body); $mailbox = $service->getMailboxes()->get($json->mailbox); $messages = $service->getValidMessages($mailbox, $json->start ?? 0, $json->amount ?? null); $mails = []; foreach ($messages as $message) { $mails []= [ 'position' => $message->getPosition(), 'uid' => $message->getUID(), 'subject' => $message->getSubject(), 'date' => $message->getDateTime()->format('Y-m-d H:i:s'), 'from' => $message->getFrom(), 'has_attachments' => $message->hasAttachments(), 'valid_attachments' => $message->hasValidAttachments(), 'downloaded_attachments' => $message->hasDownloadedAttachments() ]; } return $this->withJson($response, [ 'input' => $json, 'total' => $service->getMailboxes()->countValid($mailbox), 'messages' => $mails ]); } public function attachments(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface { $body = $request->getBody()->getContents(); $json = \Safe\json_decode($body); $mailbox = $service->getMailboxes()->get($json->mailbox); $messages = $service->getMessages($mailbox, $json->start ?? 0, $json->amount ?? null); $cnt = 0; $attachments = []; foreach ($messages as $message) { $attachments = array_merge($attachments, $service->saveAttachments($message, $json->extension ?? null)); $cnt ++; } return $this->withJson($response, [ 'input' => $json, 'messages_count' => $cnt, 'attachments_count' => count($attachments), 'attachments' => $attachments ]); } public function attachment(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface { $body = $request->getBody()->getContents(); $json = \Safe\json_decode($body); $mailbox = $service->getMailbox($json->mailbox); $cnt = 0; $attachments = []; $errors = []; foreach ($json->messages as $uid) { $message = $service->getMessage($mailbox, $uid); try { $attachments = array_merge($attachments, $service->saveAttachments($message, $json->extension ?? null)); } catch (FilesystemException $e) { $errors []= [ 'message' => $uid, 'error' => $e->getMessage() ]; } $cnt ++; } $output = [ 'input' => $json, 'messages_count' => $cnt, 'attachments_count' => count($attachments), 'attachments' => $attachments ]; if (count($errors) > 0) { $output['errors'] = $errors; } return $this->withJson($response, $output); } }