src/V4/Action/Export/EntityExportListAction.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\V4\Action\Export;
  3. use App\Security\User;
  4. use App\V4\Messenger\Handler\EntityExportRequestHandler;
  5. use DateTimeImmutable;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Security;
  14. /**
  15.  * @Route("/api/v4/export/list", name="export_list", methods={"GET"})
  16.  */
  17. class EntityExportListAction extends AbstractController
  18. {
  19.     public function __invoke(Security $securityKernelInterface $kernel): JsonResponse
  20.     {
  21.         $user $security->getUser();
  22.         if (!$user instanceof User) {
  23.             return new JsonResponse(['message' => 'need_authenticated'], Response::HTTP_UNAUTHORIZED);
  24.         }
  25.         $finder = new Finder();
  26.         $userExportPath sprintf(
  27.             '%s/%s/%s/',
  28.             $kernel->getProjectDir().EntityExportRequestHandler::EXPORT_PATH_COMPLETED,
  29.             $user->getCustomerId(),
  30.             $user->getUserId()
  31.         );
  32.         $fileSystem = new Filesystem();
  33.         if (!$fileSystem->exists($userExportPath)) {
  34.             return new JsonResponse(['files' => []]);
  35.         }
  36.         $finder->files()->in($userExportPath)->name('*.csv');
  37.         $files = [];
  38.         foreach ($finder as $file) {
  39.             if (!isset($files[$file->getRelativePath()])) {
  40.                 $files[$file->getRelativePath()] = [];
  41.             }
  42.             $files[$file->getRelativePath()][] = [
  43.                 'id' => $file->getFilenameWithoutExtension(),
  44.                 'requestedAt' => (new DateTimeImmutable())->setTimestamp($file->getCTime())->format(DateTimeImmutable::ISO8601),
  45.             ];
  46.         }
  47.         return new JsonResponse(['files' => $files]);
  48.     }
  49. }