Full implemantation

This commit is contained in:
2022-11-28 22:56:21 -03:00
parent 30ef4c6a35
commit c53eb4c7a6
55 changed files with 1505 additions and 1011 deletions

View File

@ -1,11 +1,12 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use function Safe\json_decode;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Attachments as Service;
use ProVM\Emails\Model\Attachment;
class Attachments
{
@ -13,47 +14,40 @@ class Attachments
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$attachments = array_map(function($attachment) {
$attachment['original_attachment'] = $attachment['original_attachment']->getRealPath();
if (isset($attachment['decrypted_attachment']) and $attachment['decrypted_attachment'] !== false) {
$attachment['decrypted_attachment'] = $attachment['decrypted_attachment']->getRealPath();
}
return $attachment;
}, $service->fetchFullAttachments());
return $this->withJson($response, compact('attachments'));
}
protected function fileToArray(\SplFileInfo $info): array
{
return [
'filename' => $info->getFilename(),
'path' => $info->getPath(),
'full_name' => $info->getRealPath(),
'extension' => $info->getExtension()
$attachments = array_map(function(Attachment $attachment) {
return $attachment->toArray();
},$service->getAll());
$output = [
'total' => count($attachments),
'attachments' => $attachments
];
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $request->getBody()->getContents();
$json = json_decode($body);
if (!is_array($json)) {
$json = [$json];
}
$output = ['input' => $json, 'attachments' => []];
foreach ($json as $attachment) {
$output['attachments'] []= $this->fileToArray($service->getAttachment($json->attachment));
}
return $this->withJson($response, $output);
}
public function decrypt(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface
{
$body = $request->getBody()->getContents();
$json = json_decode($body);
if (!is_array($json)) {
$json = [$json];
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'message UIDs');
}
$output = ['input' => $json, 'attachments' => []];
foreach ($json as $attachment) {
$output['attachments'] []= $this->fileToArray($service->removeEncryption($attachment));
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'saved' => [
'attachments' => [],
'total' => 0
]
];
foreach ($json->messages as $message_id) {
if (!$jobsService->isPending($message_id)) {
continue;
}
if ($service->grab($message_id)) {
$job = $jobsService->find($message_id);
$jobsService->execute($job->getId());
$output['saved']['attachments'] []= $job->toArray();
$output['saved']['total'] ++;
}
}
return $this->withJson($response, $output);
}