src/EventSubscriber/WorkflowLoggerSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Workflow\WorkflowLoggableInterface;
  4. use App\Entity\Workflow\WorkflowTransitionLog;
  5. use App\Services\TokenService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use ERP\CommentBundle\Entity\Comment;
  8. use ERP\CommentBundle\Entity\CommentableInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Workflow\Event\Event;
  11. class WorkflowLoggerSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var TokenService
  15.      */
  16.     private $tokenService;
  17.     private EntityManagerInterface $entityManager;
  18.     public function __construct(TokenService $tokenServiceEntityManagerInterface $entityManager)
  19.     {
  20.         $this->tokenService $tokenService;
  21.         $this->entityManager $entityManager;
  22.     }
  23.     public function onEntered(Event $event)
  24.     {
  25.         $entity $event->getSubject();
  26.         if (!$entity instanceof WorkflowLoggableInterface || !$event->getTransition()) {
  27.             return;
  28.         }
  29.         $workflowTransitionLog = new WorkflowTransitionLog();
  30.         $workflowTransitionLog->setName($event->getWorkflowName());
  31.         $workflowTransitionLog->setFrom(implode(','$event->getTransition()->getFroms()));
  32.         $workflowTransitionLog->setTo(implode(','$event->getTransition()->getTos()));
  33.         if (($user $this->tokenService->getUser())) {
  34.             $workflowTransitionLog->setUser($user);
  35.         }
  36.         if ($entity instanceof CommentableInterface) {
  37.             foreach ($entity->getComments() as $comment) {
  38.                 if (isset($comment->workflow_comment) && true === $comment->workflow_comment) {
  39.                     unset($comment->workflow_comment);
  40.                     if ($comment instanceof Comment) {
  41.                         $workflowTransitionLog->setComment($comment);
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         $this->entityManager->persist($workflowTransitionLog);
  47.         $entity->addWorkflowTransitionLog($workflowTransitionLog);
  48.     }
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             'workflow.entered' => 'onEntered',
  53.         ];
  54.     }
  55. }