<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 15/04/2019
* Time: 10:15 PM
*/
namespace App\EventSubscriber\MaintenanceElement;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Doctrine\ORM\EntityManagerInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Repository\App\MaintenanceElementRepository;
use App\Repository\App\MediaObjectRepository;
use App\Entity\App\MaintenanceElement;
use App\Services\UtilsService;
class MaintenanceElementPostWriteSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $mediaObjectRepository;
private $elementRepository;
private $utilsService;
public function __construct(
MediaObjectRepository $mediaObjectRepository,
MaintenanceElementRepository $elementRepository,
EntityManagerInterface $entityManager,
UtilsService $utilsService)
{
$this->entityManager = $entityManager;
$this->mediaObjectRepository = $mediaObjectRepository;
$this->elementRepository = $elementRepository;
$this->utilsService = $utilsService;
}
/**
* @param ViewEvent $event
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$maintenanceElement = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
if (!($maintenanceElement instanceof MaintenanceElement) ||
(Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
)
return;
$content = $request->getContent();
$params = json_decode($content, true);
if (isset($params['mediaContent']) &&
is_array($params['mediaContent']) &&
count($params['mediaContent']) > 0
) {
if (!$this->entityManager->isOpen()) {
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
$getMediaObjects = $this->mediaObjectRepository->findBy(['maintenanceElement' => $maintenanceElement->getId()]);
foreach ($getMediaObjects as $media) {
if (!in_array($media->getId(), $params['mediaContent'])){
$this->entityManager->remove($media);
}
}
foreach ($params['mediaContent'] as $mediaId) {
$mediaObject = $this->mediaObjectRepository->find($mediaId);
$mediaObject->setMaintenanceElement($maintenanceElement);
$this->entityManager->persist($mediaObject);
}
$this->entityManager->flush();
}
if(isset($params['children']) &&
is_array($params['children']) &&
count($params['children']) > 0
){
if (!$this->entityManager->isOpen()) {
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
foreach ($maintenanceElement->getChildren() as $child) {
$search = array_search($child->getId(), array_column($params['children'], 'id'));
if ( !is_integer($search) && !$search ){
$child->setParent(null);
$this->entityManager->persist($child);
}
}
foreach ($params['children'] as $child) {
if (isset($child['id'])) {
$children = $this->elementRepository->find($child['id']);
$children->setParent($maintenanceElement);
$this->entityManager->persist($children);
}
}
$this->entityManager->flush();
}
if (Request::METHOD_POST === $method) {
$identify = $this->utilsService->generateIdentify($maintenanceElement);
$maintenanceElement->setIdentify($identify);
$this->entityManager->persist($maintenanceElement);
$this->entityManager->flush();
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_WRITE]
];
}
}