<?php
namespace App\Controller\BackOffice;
use App\Entity\Version\APIVersion;
use App\Entity\Version\AppApiRelation;
use App\Entity\Version\APPVersion;
use App\Form\Type\BackOffice\Version\ApiVersionsType;
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/api")
*/
class ApiVersionController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* ApiVersionController 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);
$apiVersions = $this->entityManager
->getRepository(APIVersion::class)
->search($searchCriteria);
return $this->render('/backOffice/version/api/index.html.twig',[
'versions' => $apiVersions
]);
}
/**
* @param Request $request
* @return Response
* @Route (path="/new", methods={"GET", "POST"})
* @noinspection DuplicatedCode
*/
public function newAction(Request $request): Response
{
$apiVersion = new APIVersion();
$appVersions = $this->entityManager
->getRepository(APPVersion::class)
->findAll();
$versionForm = $this->formFactory
->create(ApiVersionsType::class, $apiVersion, [
'app_versions' => $appVersions,
]);
$versionForm->handleRequest($request);
if($versionForm->isSubmitted() && $versionForm->isValid()){
/** @var APIVersion $appVersion */
$apiVersion = $versionForm->getData();
$data = $versionForm->get('appVersions')->getData();
/** @var APPVersion $version */
foreach ($appVersions as $index => $version){
if($data[$index]){
$relation = new AppApiRelation();
$relation->setAppVersion($version);
$relation->setApiVersion($apiVersion);
$this->entityManager->persist($relation);
}
}
$this->entityManager->persist($apiVersion);
$this->entityManager->flush();
return $this->redirectToRoute('app_backoffice_apiversion_index',[
'apiVersion' => $apiVersion->getVersionId()
]);
}
return $this->render('backOffice/version/api/new.html.twig',[
"versionForm" => $versionForm->createView()
]);
}
/**
* @param Request $request
* @param $apiVersion
* @return Response
* @Route (path="/{apiVersion}", methods={"GET", "POST"})
* @noinspection DuplicatedCode
*/
public function editAction(Request $request, $apiVersion): Response
{
$apiVersion = $this->entityManager
->getRepository(APIVersion::class)
->findOneBy(['versionId' => $apiVersion]);
if(!$apiVersion){
throw $this->createNotFoundException();
}
$compatible = $this->entityManager
->getRepository(AppApiRelation::class)
->findBy(['apiVersion' => $apiVersion]);
$indexed = [];
/** @var AppApiRelation $v */
foreach ($compatible as $v){
$indexed[] = $v->getAppVersion()->getVersionId();
}
$appVersions = $this->entityManager
->getRepository(APPVersion::class)
->findAll();
$versionForm = $this->formFactory
->create(ApiVersionsType::class, $apiVersion, [
'gitHashDisabled' => true,
'app_versions' => $appVersions,
'compatible_apps' => $indexed
]);
$versionForm->handleRequest($request);
if($versionForm->isSubmitted() && $versionForm->isValid()){
/** @var APIVersion $apiVersion */
$apiVersion = $versionForm->getData();
$data = $versionForm->get('appVersions')->getData();
/** @var APPVersion $version */
foreach ($appVersions as $index => $version){
/** @var AppApiRelation[] $relations */
$relations = $this->entityManager
->getRepository(AppApiRelation::class)
->findBy(['apiVersion' => $apiVersion, 'appVersion' => $version]);
if($data[$index]){
if (empty($relations)) {
$relation = new AppApiRelation();
$relation->setAppVersion($version);
$relation->setApiVersion($apiVersion);
$this->entityManager->persist($relation);
}
} else {
foreach ($relations as $relation){
$this->entityManager->remove($relation);
}
}
}
$this->entityManager->persist($apiVersion);
$this->entityManager->flush();
return $this->redirectToRoute('app_backoffice_apiversion_index');
}
return $this->render('backOffice/version/api/edit.html.twig',[
"versionForm" => $versionForm->createView()
]);
}
}