<?php
namespace App\EventListener;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\JvnTask;
use App\Entity\JvnWinteringTemplateChapter;
use App\Handler\DataPersisterHandler;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class SaveProvidedDataEventSubscriber implements EventSubscriberInterface
{
private DataPersisterHandler $dataPersisterHandler;
private EntityManagerInterface $entityManager;
public function __construct(DataPersisterHandler $dataPersisterHandler, EntityManagerInterface $entityManager)
{
$this->setDataPersisterHandler($dataPersisterHandler)->setEntityManager($entityManager);
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
/**
* Just after the data provider recovers data from database and before deserialization
* In this point we've got the current database object without modifications
*/
return [
KernelEvents::VIEW => ['saveData', EventPriorities::POST_VALIDATE],
];
}
/**
* @param ViewEvent $event
* @noinspection PhpUnused Called dynamically as Event Handler
*/
public function saveData(ViewEvent $event): void
{
if( $event->getRequest()->attributes->get('_route') == 'api_jvn_wintering_template_chapters_patch_item' ) {
$data = json_decode($event->getRequest()->getContent(), JSON_OBJECT_AS_ARRAY);
if( array_key_exists('checkPoints', $data) && is_array($data['checkPoints']) ) {
$checkPointsOrder = new ArrayCollection();
$position = 1;
foreach($data['checkPoints'] as $checkPoint ) {
if( array_key_exists('checkPoint', $checkPoint) ) {
$key = str_replace('/', '|', substr($checkPoint['checkPoint'], 1));
} elseif( array_key_exists('id', $checkPoint) ) {
$key = str_replace('/', '|', substr($checkPoint['id'], 1));
} else {
throw new BadRequestHttpException('Contenido invalido');
}
$checkPointsOrder->set($key, $position++);
}
$this->getDataPersisterHandler()->setCheckPointsOrder($checkPointsOrder);
}
} elseif(
in_array($event->getRequest()->getMethod(), ['PUT', 'PATCH']) &&
($data = $event->getRequest()->attributes->get('previous_data')) instanceof JvnTask
) {
// Clone and save the original object for further use and checks.
$this->getDataPersisterHandler()->setPreviousTask($data);
}
}
/**
* @return DataPersisterHandler
*/
public function getDataPersisterHandler(): DataPersisterHandler
{
return $this->dataPersisterHandler;
}
/**
* @param DataPersisterHandler $dataPersisterHandler
* @return SaveProvidedDataEventSubscriber
*/
public function setDataPersisterHandler(DataPersisterHandler $dataPersisterHandler): SaveProvidedDataEventSubscriber
{
$this->dataPersisterHandler = $dataPersisterHandler;
return $this;
}
/**
* @return EntityManagerInterface
*/
public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
/**
* @param EntityManagerInterface $entityManager
* @return SaveProvidedDataEventSubscriber
*/
public function setEntityManager(EntityManagerInterface $entityManager): SaveProvidedDataEventSubscriber
{
$this->entityManager = $entityManager;
return $this;
}
}