src/Controller/BackOffice/FirmwareVersionController.php line 131

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BackOffice;
  3. use App\Entity\Version\AppFirmwareRelation;
  4. use App\Entity\Version\APPVersion;
  5. use App\Entity\Version\FirmwareVersion;
  6. use App\Form\Type\BackOffice\Version\FirmwareVersionType;
  7. use App\Model\PaginatedSearchCriteria;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * Class AnalysisMethodsController
  17.  * @package App\Controller\BackOffice
  18.  * @Route(path="versions/firmware")
  19.  */
  20. class FirmwareVersionController extends AbstractController
  21. {
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $entityManager;
  26.     /**
  27.      * @var FormFactoryInterface
  28.      */
  29.     private $formFactory;
  30.     /**
  31.      * FirmwareVersionController constructor.
  32.      * @param EntityManagerInterface $entityManager
  33.      * @param FormFactoryInterface $formFactory
  34.      */
  35.     public function __construct(EntityManagerInterface $entityManagerFormFactoryInterface $formFactory)
  36.     {
  37.         $this->entityManager $entityManager;
  38.         $this->formFactory $formFactory;
  39.     }
  40.     /**
  41.      * @param Request $request
  42.      * @return Response
  43.      * @Route(methods={"GET"})
  44.      */
  45.     public function indexAction(Request $request): Response
  46.     {
  47.         $searchCriteria = new PaginatedSearchCriteria($request->query);
  48.         $searchCriteria->setPageSize(PaginatedSearchCriteria::BACKOFFICE_PAGINATION_SIZE);
  49.         $firmwareVersions $this->entityManager
  50.             ->getRepository(FirmwareVersion::class)
  51.             ->search($searchCriteria);
  52.         return $this->render('/backOffice/version/firmware/index.html.twig', [
  53.             'versions' => $firmwareVersions,
  54.         ]);
  55.     }
  56.     /**
  57.      * @param Request $request
  58.      * @return Response
  59.      * @Route(path="/new", methods={"GET", "POST"})
  60.      */
  61.     public function newAction(Request $request): Response
  62.     {
  63.         $firmwareVersion = new FirmwareVersion();
  64.         $appVersions $this->entityManager
  65.             ->getRepository(AppVersion::class)
  66.             ->findAll();
  67.         $versionForm $this->formFactory
  68.             ->create(FirmwareVersionType::class, $firmwareVersion, [
  69.                 'app_versions' => $appVersions,
  70.                 'compatible_apps' => [],
  71.             ]);
  72.         $versionForm->handleRequest($request);
  73.         if ($versionForm->isSubmitted() && $versionForm->isValid()) {
  74.             $file $versionForm->get('firmware')->getData();
  75.             $newFilename $firmwareVersion->getGitHash().'.bin';
  76.             $file->move(
  77.                 $this->getParameter('bin_directory'),
  78.                 $newFilename
  79.             );
  80.             $firmwareVersion $versionForm->getData();
  81.             $data $versionForm->get('appVersions')->getData();
  82.             /** @var APPVersion $version */
  83.             foreach ($appVersions as $index => $version) {
  84.                 if ($data[$index]) {
  85.                     $relation = new AppFirmwareRelation();
  86.                     $relation->setAppVersion($version);
  87.                     $relation->setFirmwareVersion($firmwareVersion);
  88.                     $this->entityManager->persist($relation);
  89.                 }
  90.             }
  91.             $this->entityManager->persist($firmwareVersion);
  92.             $this->entityManager->flush();
  93.             return $this->redirectToRoute('app_backoffice_firmwareversion_edit', [
  94.                 'firmwareVersion' => $firmwareVersion->getVersionId(),
  95.             ]);
  96.         }
  97.         return $this->render('backOffice/version/firmware/new.html.twig', [
  98.             'versionForm' => $versionForm->createView(),
  99.         ]);
  100.     }
  101.     /**
  102.      * @param Request $request
  103.      * @param $firmwareVersion
  104.      * @return Response
  105.      * @Route (path="/{firmwareVersion}", methods={"GET", "POST"})
  106.      * @noinspection DuplicatedCode
  107.      */
  108.     public function editAction(Request $request$firmwareVersion): Response
  109.     {
  110.         $firmwareVersion $this->entityManager
  111.             ->getRepository(FirmwareVersion::class)
  112.             ->findOneBy(['versionId' => $firmwareVersion]);
  113.         if (!$firmwareVersion) {
  114.             throw $this->createNotFoundException();
  115.         }
  116.         $compatible $this->entityManager
  117.             ->getRepository(AppFirmwareRelation::class)
  118.             ->findBy(['firmwareVersion' => $firmwareVersion]);
  119.         $indexed = [];
  120.         /** @var AppFirmwareRelation $v */
  121.         foreach ($compatible as $v) {
  122.             $indexed[] = $v->getAppVersion()->getVersionId();
  123.         }
  124.         $appVersions $this->entityManager
  125.             ->getRepository(APPVersion::class)
  126.             ->findAll();
  127.         $versionForm $this->formFactory
  128.             ->create(FirmwareVersionType::class, $firmwareVersion, [
  129.                 'app_versions' => $appVersions,
  130.                 'compatible_apps' => $indexed,
  131.                 'edit' => true,
  132.                 'gitHashDisabled' => true,
  133.             ]);
  134.         $versionForm->handleRequest($request);
  135.         if ($versionForm->isSubmitted() && $versionForm->isValid()) {
  136.             /** @var APPVersion $appVersion */
  137.             $firmwareVersion $versionForm->getData();
  138.             $data $versionForm->get('appVersions')->getData();
  139.             /** @var APPVersion $version */
  140.             foreach ($appVersions as $index => $version) {
  141.                 if ($data[$index]) {
  142.                     $relation = new AppFirmwareRelation();
  143.                     $relation->setAppVersion($version);
  144.                     $relation->setFirmwareVersion($firmwareVersion);
  145.                     $this->entityManager->persist($relation);
  146.                 } else {
  147.                     $relations $this->entityManager
  148.                         ->getRepository(AppFirmwareRelation::class)
  149.                         ->findBy(['firmwareVersion' => $firmwareVersion'appVersion' => $version]);
  150.                     foreach ($relations as $relation) {
  151.                         $this->entityManager->remove($relation);
  152.                     }
  153.                 }
  154.             }
  155.             $this->entityManager->persist($firmwareVersion);
  156.             $this->entityManager->flush();
  157.             return $this->redirectToRoute('app_backoffice_firmwareversion_index');
  158.         }
  159.         return $this->render('backOffice/version/firmware/edit.html.twig', [
  160.             'firmwareVersion' => $firmwareVersion,
  161.             'versionForm' => $versionForm->createView(),
  162.         ]);
  163.     }
  164.     /**
  165.      * @param $firmwareVersion
  166.      * @return BinaryFileResponse
  167.      * @Route (path="/{firmwareVersion}/download", methods={"GET"})
  168.      */
  169.     public function download($firmwareVersion): BinaryFileResponse
  170.     {
  171.         $firmware $this->entityManager
  172.             ->getRepository(FirmwareVersion::class)
  173.             ->find($firmwareVersion);
  174.         if (!$firmware) {
  175.             throw $this->createNotFoundException('Firmware version not found');
  176.         }
  177.         $filename $firmware->getGitHash().'.bin';
  178.         $filePath $this->getParameter('bin_directory').DIRECTORY_SEPARATOR.$filename;
  179.         $response $this->file($filePath$filename);
  180.         $response->setPrivate();
  181.         $response->setCharset('utf-8');
  182.         return $response;
  183.     }
  184. }