src/EventSubscriber/DeleteEmployeeRemovedFromIncompatibleHolidaysSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Employee;
  5. use App\Entity\HolidayIncompatibility;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class DeleteEmployeeRemovedFromIncompatibleHolidaysSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private $entityManager;
  17.     public function __construct(EntityManagerInterface $entityManager)
  18.     {
  19.         $this->entityManager $entityManager;
  20.     }
  21.     public function deleteEmployeeRemovedFromIncompatibleHolidays(ViewEvent $viewEvent) {
  22.         $employee $viewEvent->getControllerResult();
  23.         $method $viewEvent->getRequest()->getMethod();
  24.         if (!$employee instanceof Employee || Request::METHOD_PUT !== $method) {
  25.             return;
  26.         }
  27.         if (!$employee->isActive()) {
  28.             /** @var HolidayIncompatibility $holidayIncompatibilities */
  29.             $holidayIncompatibilities $employee->getHolidayIncompatibilities();
  30.             /** @var HolidayIncompatibility $holidayIncompatibility */
  31.             foreach ($holidayIncompatibilities as $holidayIncompatibility) {
  32. //                $employees = $holidayIncompatibility->getEmployees();
  33.                 $holidayIncompatibility->removeEmployee($employee);
  34.                 $holidayIncompatibility->setMinNumberEmployees($holidayIncompatibility->getMinNumberEmployees()-1);
  35.                 if (count($holidayIncompatibility->getEmployees()) == 1) {
  36. //                    $aEmployee = $holidayIncompatibility->getEmployees()[0];
  37. //                    $holidayIncompatibility->removeEmployee($aEmployee);
  38.                     $this->entityManager->remove($holidayIncompatibility);
  39.                     $this->entityManager->flush();
  40.                 } else {
  41.                     $this->entityManager->persist($holidayIncompatibility);
  42.                     $this->entityManager->flush();
  43.                 }
  44. //
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * @inheritDoc
  50.      */
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             KernelEvents::VIEW => ['deleteEmployeeRemovedFromIncompatibleHolidays'EventPriorities::POST_WRITE],
  55.         ];
  56.     }
  57. }