<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 26/04/2019
* Time: 11:56 AM
*/
namespace App\EventSubscriber\Location;
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\LocationRepository;
use App\Repository\App\MediaObjectRepository;
use App\Repository\App\ResourceRepository;
use App\Repository\App\UserRepository;
use App\Repository\App\VendorStaffRepository;
use App\Entity\App\Location;
use App\Entity\App\Role;
use App\Services\VendorService;
use App\Services\UtilsService;
class LocationPostWriteSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $mediaObjectRepository;
private $locationRepository;
private $elementRepository;
private $resourceRepository;
private $userRepository;
private $vendorStaffRepository;
private $vendorService;
private $utilsService;
public function __construct(
MediaObjectRepository $mediaObjectRepository,
LocationRepository $locationRepository,
MaintenanceElementRepository $elementRepository,
ResourceRepository $resourceRepository,
UserRepository $userRepository,
VendorStaffRepository $vendorStaffRepository,
VendorService $vendorService,
UtilsService $utilsService,
EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
$this->mediaObjectRepository = $mediaObjectRepository;
$this->locationRepository = $locationRepository;
$this->elementRepository = $elementRepository;
$this->resourceRepository = $resourceRepository;
$this->userRepository = $userRepository;
$this->vendorStaffRepository = $vendorStaffRepository;
$this->vendorService = $vendorService;
$this->utilsService = $utilsService;
}
/**
* @param ViewEvent $event
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function onKernelView(ViewEvent $event)
{
$location = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
if (!($location instanceof Location) ||
(Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
)
return;
$content = $request->getContent();
$params = json_decode($content, true);
if (!$this->entityManager->isOpen()) {
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
if (isset($params['mediaContent']) &&
is_array($params['mediaContent'])
) {
$getMediaObjects = $this->mediaObjectRepository->findBy(['location' => $location->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->setLocation($location);
$this->entityManager->persist($mediaObject);
}
$this->entityManager->flush();
}
/*
* /-----------------------------deprecated---------------------------------
if(isset($params['maintenanceElements']) &&
is_array($params['maintenanceElements'])
){
if (!$this->entityManager->isOpen()) {
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
foreach ($location->getMaintenanceElements() as $element) {
$search = array_search($element->getId(), array_column($params['maintenanceElements'], 'id'));
if ( !is_integer($search) && !$search ){
$element->setLocation(null);
$this->entityManager->persist($element);
}
}
foreach ($params['maintenanceElements'] as $element) {
if (isset($element['id'])) {
$getElement = $this->elementRepository->find($element['id']);
$getElement->setLocation($location);
$this->entityManager->persist($getElement);
}
}
$this->entityManager->flush();
}
if(isset($params['resources']) &&
is_array($params['resources'])
){
if (!$this->entityManager->isOpen()) {
$this->entityManager = $this->entityManager->create(
$this->entityManager->getConnection(),
$this->entityManager->getConfiguration()
);
}
foreach ($location->getResources() as $resource) {
$search = array_search($resource->getId(), array_column($params['resources'], 'id'));
if ( !is_integer($search) && !$search ){
$resource->setLocation(null);
$this->entityManager->persist($resource);
}
}
foreach ($params['resources'] as $resource) {
if (isset($resource['id'])) {
$getResource = $this->resourceRepository->find($resource['id']);
$getResource->setLocation($location);
$this->entityManager->persist($getResource);
}
}
$this->entityManager->flush();
}
-----------------------------deprecated---------------------------------
*/
if(isset($params['users']) &&
is_array($params['users'])
){
$roleLocation = [Role::ROLE_TASKMASTER, Role::ROLE_OPERATOR, Role::ROLE_USER];
foreach ($location->getVendorStaff() as $vendorStaff) {
$search = array_search($vendorStaff->getUser()->getId(), $params['users']);
if ( !is_integer($search) && !$search ) {
if ($this->vendorService->isUserRoleInToVendor($location->getVendor(), $vendorStaff->getUser(), $roleLocation)) {
$vendorStaff->removeLocation($location);
}
}
}
foreach ($params['users'] as $userId) {
$user = $this->userRepository->find($userId);
$vendorStaff = $this->vendorStaffRepository->findOneBy([
'user' => $user,
'vendor' => $location->getVendor()
]);
$vendorStaff->addLocation($location);
$this->entityManager->persist($vendorStaff);
}
$this->entityManager->flush();
}
if (Request::METHOD_POST === $method) {
$identify = $this->utilsService->generateIdentify($location);
$location->setIdentify($identify);
$this->entityManager->persist($location);
$this->entityManager->flush();
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_WRITE]
];
}
}