vendor/sulu/automation-bundle/EventSubscriber/PHPTaskEventSubscriber.php line 53

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\AutomationBundle\EventSubscriber;
  11. use Sulu\Bundle\AutomationBundle\Tasks\Model\TaskRepositoryInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Task\Event\Events;
  16. use Task\Event\TaskEvent;
  17. /**
  18.  * Fake-Request handling for tasks.
  19.  */
  20. class PHPTaskEventSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     private $requestStack;
  26.     /**
  27.      * @var TaskRepositoryInterface
  28.      */
  29.     private $taskRepository;
  30.     public function __construct(RequestStack $requestStackTaskRepositoryInterface $taskRepository)
  31.     {
  32.         $this->requestStack $requestStack;
  33.         $this->taskRepository $taskRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             Events::TASK_BEFORE => ['pushRequest'],
  39.             Events::TASK_AFTER => ['popRequest'],
  40.         ];
  41.     }
  42.     /**
  43.      * Create and push new request to requests-stack.
  44.      */
  45.     public function pushRequest(TaskEvent $event): void
  46.     {
  47.         $task $this->taskRepository->findByTaskId($event->getTask()->getUuid());
  48.         if (!$task) {
  49.             // current task is not managed by this bundle
  50.             return;
  51.         }
  52.         $request = new Request(
  53.             [],
  54.             [],
  55.             ['_task_id' => $event->getTask()->getUuid()],
  56.             [],
  57.             [],
  58.             ['SERVER_NAME' => $task->getHost(), 'HTTPS' => 'http' === $task->getScheme() ? 'off' 'on']
  59.         );
  60.         $this->requestStack->push($request);
  61.     }
  62.     /**
  63.      * Pop request from request stack.
  64.      */
  65.     public function popRequest(TaskEvent $event): void
  66.     {
  67.         $request $this->requestStack->getCurrentRequest();
  68.         if (!$request || $request->attributes->get('_task_id') !== $event->getTask()->getUuid()) {
  69.             // current request was not created for current task
  70.             return;
  71.         }
  72.         $this->requestStack->pop();
  73.     }
  74. }