<?php
namespace App\Controller\BackOffice;
use App\Entity\Version\AppFirmwareRelation;
use App\Entity\Version\APPVersion;
use App\Entity\Version\FirmwareVersion;
use App\Form\Type\BackOffice\Version\AppVersionType;
use App\Model\PaginatedSearchCriteria;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AnalysisMethodsController
* @package App\Controller\BackOffice
* @Route(path="versions/app")
*/
class AppVersionController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* AppVersionController constructor.
* @param EntityManagerInterface $entityManager
* @param FormFactoryInterface $formFactory
*/
public function __construct(EntityManagerInterface $entityManager, FormFactoryInterface $formFactory)
{
$this->entityManager = $entityManager;
$this->formFactory = $formFactory;
}
/**
* @param Request $request
* @return Response
* @Route(methods={"GET"})
*/
public function indexAction(Request $request): Response
{
$searchCriteria = new PaginatedSearchCriteria($request->query);
$searchCriteria->setPageSize(PaginatedSearchCriteria::BACKOFFICE_PAGINATION_SIZE);
$appVersions = $this->entityManager
->getRepository(APPVersion::class)
->search($searchCriteria);
return $this->render('/backOffice/version/app/index.html.twig',[
'versions' => $appVersions
]);
}
/**
* @param Request $request
* @return Response
* @Route (path="/new", methods={"GET", "POST"})
* @noinspection DuplicatedCode
*/
public function newAction(Request $request): Response
{
$appVersion = new APPVersion();
$firmwareVersions = $this->entityManager
->getRepository(FirmwareVersion::class)
->findAll();
$versionForm = $this->formFactory
->create(AppVersionType::class, $appVersion, [
'firmware_versions' => $firmwareVersions
]);
$versionForm->handleRequest($request);
if($versionForm->isSubmitted() && $versionForm->isValid()){
/** @var APPVersion $appVersion */
$appVersion = $versionForm->getData();
$data = $versionForm->get('firmwareVersions')->getData();
/** @var FirmwareVersion $version */
foreach ($firmwareVersions as $index => $version){
if($data[$index]){
$relation = new AppFirmwareRelation();
$relation->setAppVersion($appVersion);
$relation->setFirmwareVersion($version);
$this->entityManager->persist($relation);
}
}
$this->entityManager->persist($appVersion);
$this->entityManager->flush();
return $this->redirectToRoute('app_backoffice_appversion_index',[
'appVersion' => $appVersion->getVersionId()
]);
}
return $this->render('backOffice/version/app/new.html.twig',[
"versionForm" => $versionForm->createView()
]);
}
/**
* @param Request $request
* @param $appVersion
* @return Response
* @Route (path="/{appVersion}", methods={"GET", "POST"})
* @noinspection DuplicatedCode
*/
public function editAction(Request $request, $appVersion): Response
{
$appVersion = $this->entityManager
->getRepository(APPVersion::class)
->findOneBy(['versionId' => $appVersion]);
if(!$appVersion){
throw $this->createNotFoundException();
}
$compatible = $this->entityManager
->getRepository(AppFirmwareRelation::class)
->findBy(['appVersion' => $appVersion]);
$indexed = [];
/** @var AppFirmwareRelation $v */
foreach ($compatible as $v){
$indexed[] = $v->getFirmwareVersion()->getVersionId();
}
$firmwareVersions = $this->entityManager
->getRepository(FirmwareVersion::class)
->findAll();
$versionForm = $this->formFactory
->create(AppVersionType::class, $appVersion, [
'gitHashDisabled' => true,
'firmware_versions' => $firmwareVersions,
'compatible_firmwares' => $indexed
]);
$versionForm->handleRequest($request);
if($versionForm->isSubmitted() && $versionForm->isValid()){
/** @var APPVersion $appVersion */
$appVersion = $versionForm->getData();
$data = $versionForm->get('firmwareVersions')->getData();
/** @var FirmwareVersion $version */
foreach ($firmwareVersions as $index => $version){
/** @var AppFirmwareRelation[] $relations */
$relations = $this->entityManager
->getRepository(AppFirmwareRelation::class)
->findBy(['firmwareVersion' => $version, 'appVersion' => $appVersion]);
if($data[$index]){
if(empty($relations)) {
$relation = new AppFirmwareRelation();
$relation->setAppVersion($appVersion);
$relation->setFirmwareVersion($version);
$this->entityManager->persist($relation);
}
} else {
foreach ($relations as $relation){
$this->entityManager->remove($relation);
}
}
}
$this->entityManager->persist($appVersion);
$this->entityManager->flush();
return $this->redirectToRoute('app_backoffice_appversion_index');
}
return $this->render('backOffice/version/app/edit.html.twig',[
"versionForm" => $versionForm->createView()
]);
}
}