src/Controller/FilterController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Filter;
  4. use App\Form\Type\FilterType;
  5. use App\Service\FilterJobsService;
  6. use App\Service\PoleEmploiService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Serializer\SerializerInterface;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. class FilterController extends AbstractController
  14. {
  15.     const TEMPORARY_FOLDER '/tmp';
  16.     /**
  17.      * @var PoleEmploiService
  18.      */
  19.     private PoleEmploiService $poleEmploiService;
  20.     /**
  21.      * @var FilterJobsService
  22.      */
  23.     private FilterJobsService $filterJobsService;
  24.     /**
  25.      * @var SerializerInterface
  26.      */
  27.     private SerializerInterface $serializer;
  28.     /**
  29.      * @var Filesystem
  30.      */
  31.     private Filesystem $filesystem;
  32.     /**
  33.      * @param  PoleEmploiService  $poleEmploiService
  34.      * @param  FilterJobsService  $filterJobsService
  35.      * @param  SerializerInterface  $serializer
  36.      * @param  Filesystem  $filesystem
  37.      */
  38.     public function __construct(
  39.         PoleEmploiService $poleEmploiService,
  40.         FilterJobsService $filterJobsService,
  41.         SerializerInterface $serializer,
  42.         Filesystem $filesystem
  43.     ) {
  44.         $this->poleEmploiService $poleEmploiService;
  45.         $this->filterJobsService $filterJobsService;
  46.         $this->serializer $serializer;
  47.         $this->filesystem $filesystem;
  48.     }
  49.     /**
  50.      * @param  Request  $request
  51.      * @return Response
  52.      * @throws ClientExceptionInterface
  53.      * @throws \Psr\Container\ContainerExceptionInterface
  54.      * @throws \Psr\Container\NotFoundExceptionInterface
  55.      * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
  56.      * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  57.      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  58.      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  59.      */
  60.     public function filterByRomeCodesAndKeywords(Request $request): Response
  61.     {
  62.         // Create form with FilterType and handle the request
  63.         $form $this->createForm(FilterType::class)
  64.             ->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             /** @var Filter $data */
  67.             $data $form->getData();
  68.             try {
  69.                 // Fetch jobs from ROME codes
  70.                 $jobs $this->poleEmploiService->getJobByRomeCode($data->getRomeCodes());
  71.             } catch (\Exception $exception) {
  72.                 // Set the flash message if error
  73.                 $this->addFlash('error'$exception->getMessage());
  74.                 // Get back with the flash message
  75.                 return $this->redirectToRoute($request->attributes->get('_route'));
  76.             }
  77.             if (!empty($data->getKeywords())) {
  78.                 try {
  79.                     // Filter jobs with keywords
  80.                     $filteredJobsByKeywords $this->filterJobsService->filterJobsByKeywords(
  81.                         $jobs['resultats'],
  82.                         $data->getKeywords()
  83.                     );
  84.                 } catch (\Exception $exception) {
  85.                     // Set the flash message if error
  86.                     $this->addFlash('error'$exception->getMessage());
  87.                     // Get back with the flash message
  88.                     return $this->redirectToRoute($request->attributes->get('_route'));
  89.                 }
  90.             }
  91.             if (!empty($data->getCompanyName())) {
  92.                 try {
  93.                     // Filter jobs with keywords
  94.                     $filteredJobsByCompanyName $this->filterJobsService->filterJobsByCompanyName(
  95.                         $jobs['resultats'],
  96.                         $data->getCompanyName()
  97.                     );
  98.                 } catch (\Exception $exception) {
  99.                     // Set the flash message if error
  100.                     $this->addFlash('error'$exception->getMessage());
  101.                     // Get back with the flash message
  102.                     return $this->redirectToRoute($request->attributes->get('_route'));
  103.                 }
  104.             }
  105.             // Merge the two results arrays
  106.             $merge $this->mergeJobsByKeywordsAndCompanyName(
  107.                 $filteredJobsByKeywords ?? [],
  108.                 $filteredJobsByCompanyName ?? []
  109.             );
  110.             // Add the file name into the variable
  111.             $download['jobs'] = [
  112.                 'file' => $this->saveTemporaryFileResult($this->serializeIntoCsv($jobs['resultats'])),
  113.                 'entries' => \count($jobs['resultats']),
  114.             ];
  115.             // Add the file name into the variable
  116.             $download['merge'] = [
  117.                 'file' => $this->saveTemporaryFileResult($this->serializeIntoCsv($merge)),
  118.                 'entries' => \count($merge),
  119.             ];
  120.         }
  121.         // Render the form
  122.         return $this->render('jobs/result.html.twig', [
  123.             'form' => $form->createView(),
  124.             'download' => $download ?? [],
  125.         ]);
  126.     }
  127.     /**
  128.      * @param  string  $content
  129.      * @return string
  130.      */
  131.     private function saveTemporaryFileResult(string $content): string
  132.     {
  133.         $filePath uniqid();
  134.         $fileName sprintf('%s/%s'self::TEMPORARY_FOLDER$filePath);
  135.         $this->filesystem->dumpFile($fileName$content);
  136.         return $filePath;
  137.     }
  138.     /**
  139.      * @param  array  $result
  140.      * @return string
  141.      */
  142.     private function serializeIntoCsv(array $result): string
  143.     {
  144.         return $this->serializer->serialize($result'csv');
  145.     }
  146.     /**
  147.      * @param  array  $jobsByKeywords
  148.      * @param  array  $jobsByCompanyName
  149.      * @return array
  150.      */
  151.     private function mergeJobsByKeywordsAndCompanyName(array $jobsByKeywords = [], array $jobsByCompanyName = []): array
  152.     {
  153.         return array_values(array_replace_recursive(
  154.             array_column($jobsByKeywordsnull'id'),
  155.             array_column($jobsByCompanyNamenull'id')
  156.         ));
  157.     }
  158. }