vendor/uvdesk/automation-bundle/EventListener/PreparedResponseListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\AutomationBundle\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\GenericEvent;
  5. use Webkul\UVDesk\AutomationBundle\Entity\PreparedResponses;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Webkul\UVDesk\AutomationBundle\PreparedResponse\Action as PreparedResponseAction;
  8. class PreparedResponseListener
  9. {
  10. private $container;
  11. private $entityManager;
  12. private $registeredPreparedResponseActions = [];
  13. public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager)
  14. {
  15. $this->container = $container;
  16. $this->entityManager = $entityManager;
  17. }
  18. public function registerPreparedResponseAction(PreparedResponseAction $serviceTag)
  19. {
  20. $this->registeredPreparedResponseActions[] = $serviceTag;
  21. }
  22. public function getRegisteredPreparedResponseActions()
  23. {
  24. return $this->registeredPreparedResponseActions;
  25. }
  26. public function executePreparedResponse(GenericEvent $event)
  27. {
  28. $preparedResponse = $this->entityManager->getRepository(PreparedResponses::class)->getPreparedResponse($event->getSubject());
  29. if (! empty($preparedResponse)) {
  30. $this->applyPreparedResponseActions($preparedResponse , $event->getArgument('entity'));
  31. }
  32. }
  33. private function applyPreparedResponseActions(PreparedResponses $preparedResponse, $entity)
  34. {
  35. foreach ($preparedResponse->getActions() as $attributes) {
  36. if (empty($attributes['type'])) {
  37. continue;
  38. }
  39. foreach ($this->getRegisteredPreparedResponseActions() as $preparedResponseAction) {
  40. if ($preparedResponseAction->getId() == $attributes['type']) {
  41. $preparedResponseAction->applyAction($this->container, $entity, isset($attributes['value']) ? $attributes['value']: '');
  42. }
  43. }
  44. }
  45. }
  46. }