ERP/TaskBundle/EventSubscriber/ActionableMeetingNoteCreatedSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace ERP\TaskBundle\EventSubscriber;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Eav\AttributeValue;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use ERP\MeetingNoteBundle\Entity\MeetingNote;
  9. use ERP\TaskBundle\Entity\Task;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Workflow\Registry;
  15. class ActionableMeetingNoteCreatedSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var Registry
  19.      */
  20.     private $registry;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $entityManager;
  25.     /**
  26.      * @var IriConverterInterface
  27.      */
  28.     private $iriConverter;
  29.     public function __construct(Registry $registryEntityManagerInterface $entityManagerIriConverterInterface $iriConverter)
  30.     {
  31.         $this->registry $registry;
  32.         $this->entityManager $entityManager;
  33.         $this->iriConverter $iriConverter;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             KernelEvents::VIEW => ['taskCreated'EventPriorities::POST_WRITE],
  39.         ];
  40.     }
  41.     public function taskCreated(ViewEvent $event): void
  42.     {
  43.         $meetingNote $event->getControllerResult();
  44.         $method $event->getRequest()->getMethod();
  45.         if (!$meetingNote instanceof MeetingNote || Request::METHOD_POST !== $method) {
  46.             return;
  47.         }
  48.         /** @var AttributeValue $actionable */
  49.         if (null === ($actionable $meetingNote->getAttributeValueByName('actionable')) || true !== $actionable->getValue()) {
  50.             return;
  51.         }
  52.         /** @var AttributeValue $assigneeValue */
  53.         if (null === ($assigneeValue $meetingNote->getAttributeValueByName('assignee'))) {
  54.             return;
  55.         }
  56.         /** @var User $assignee */
  57.         $assignee $this->iriConverter->getItemFromIri($assigneeValue->getValue());
  58.         if (null === ($dueDate $meetingNote->getAttributeValueByName('due_date'))) {
  59.             return;
  60.         }
  61.         $task = new Task();
  62.         $task->setOwner($meetingNote->getUser());
  63.         $task->setAssignee($assignee);
  64.         $task->setDueDate(new \DateTime($dueDate->getValue()));
  65.         $task->setTitle($meetingNote->getMeeting()->getTitle());
  66.         $task->setDescription($meetingNote->getContent());
  67.         $this->entityManager->persist($task);
  68.         $this->entityManager->flush();
  69.     }
  70. }