src/EventSubscriber/ActionPlanSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\ActionPlan;
  5. use App\ActionPlan\ActionPlanPrepare;
  6. use App\Repository\ActionPlanRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. final class ActionPlanSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(private ActionPlanPrepare $actionPlanPrepare, private EntityManagerInterface $entityManager,private ActionPlanRepository $actionPlanRepository)
  15.     {
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             KernelEvents::VIEW => [
  21.                 ['preWriteActionPlan'EventPriorities::POST_WRITE]
  22.             ],
  23.         ];
  24.     }
  25.     public function preWriteActionPlan(ViewEvent $event): void
  26.     {
  27.         $actionPlan $event->getControllerResult();
  28.         $method $event->getRequest()->getMethod();
  29.         if(($actionPlan instanceof ActionPlan) && Request::METHOD_PUT === $method){
  30.             $idActionPlan $actionPlan->getId();
  31.             $sql "select * from action_plan where id = $idActionPlan";
  32.             $conn $this->entityManager->getConnection();
  33.             $stmt $conn->prepare($sql);
  34.             $oldActionPlan $stmt->executeQuery();
  35.             $oldActionPlan $oldActionPlan->fetchAssociative();
  36.             $this->actionPlanRepository->save($actionPlantrue);
  37.             if($oldActionPlan['user_in_charge_id'] != $actionPlan->getUserInCharge()?->getId()){
  38.                 $description_log 'edit action plan ' $actionPlan->getId() .'change user '$oldActionPlan['user_in_charge_id'] . ' to ' $actionPlan->getUserInCharge()?->getId() . 'created by user ' $actionPlan->getCreatedBy()->getId() . 'created at '$actionPlan->getCreatedAt()->format('d/m/Y H:i:s');
  39.                 $this->actionPlanPrepare->sendActionPlanEmail($actionPlan$description_log);
  40.             }
  41.             else{
  42.                 return;
  43.             }
  44.         }
  45.         if (!($actionPlan instanceof ActionPlan) || (Request::METHOD_POST !== $method)) {
  46.             return;
  47.         }
  48.         $description_log 'add action plan' $actionPlan->getId() . 'change user '$actionPlan->getUserInCharge()?->getId() . 'created by user ' $actionPlan->getCreatedBy()->getId(). 'created at '$actionPlan->getCreatedAt()->format('d/m/Y H:i:s');
  49.         $this->actionPlanPrepare->sendActionPlanEmail($actionPlan$description_log);
  50.     }
  51. }