toArray(); },$service->getAll()); $output = [ 'total' => count($attachments), 'attachments' => $attachments ]; return $this->withJson($response, $output); } public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, LoggerInterface $logger, int $attachment_id): ResponseInterface { $attachment = $service->getRepository()->fetchById($attachment_id); $response->withHeader('Content-Type', 'application/pdf'); $response->withHeader('Content-Disposition', "'attachment;filename='{$attachment->getFullFilename()}'"); $response->getBody()->write($service->getFile($attachment_id)); return $response; } public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, Service\Messages $messagesService): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); if (!isset($json->messages)) { throw new MissingArgument('messages', 'array', 'Messages UIDs'); } $output = [ 'messages' => $json->messages, 'attachments' => [], 'total' => 0 ]; foreach ($json->messages as $message_uid) { $message = $messagesService->getLocalMessage($message_uid); $attachments = $service->grab($message->getId()); $output['attachments'] = array_merge($output['attachments'], $attachments); $output['total'] += count($attachments); } return $this->withJson($response, $output); } }