custom/plugins/CrswCleverReachOfficial/src/Core/BusinessLogic/TaskExecution/QueueService.php line 123

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution;
  3. use Crsw\CleverReachOfficial\Core\BusinessLogic\API\Http\Proxy;
  4. use Crsw\CleverReachOfficial\Core\BusinessLogic\Authorization\Contracts\AuthorizationService;
  5. use Crsw\CleverReachOfficial\Core\BusinessLogic\Authorization\Tasks\Composite\ConnectTask;
  6. use Crsw\CleverReachOfficial\Core\BusinessLogic\Scheduler\ScheduleCheckTask;
  7. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemAbortedEvent;
  8. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemEnqueuedEvent;
  9. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemFailedEvent;
  10. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemFinishedEvent;
  11. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemRequeuedEvent;
  12. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemStartedEvent;
  13. use Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution\Events\QueueItemStateTransitionEventBus;
  14. use Crsw\CleverReachOfficial\Core\BusinessLogic\Utility\Tasks\TaskCleanupTask;
  15. use Crsw\CleverReachOfficial\Core\Infrastructure\ServiceRegister;
  16. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\ExecutionRequirementsNotMetException;
  17. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Interfaces\Priority;
  18. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem;
  19. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueService as BaseService;
  20. use Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Task;
  21. use Crsw\CleverReachOfficial\Core\Infrastructure\Utility\Events\Event;
  22. /**
  23.  * Class QueueService
  24.  *
  25.  * @package Crsw\CleverReachOfficial\Core\BusinessLogic\TaskExecution
  26.  */
  27. class QueueService extends BaseService
  28. {
  29.     const CLASS_NAME __CLASS__;
  30.     /**
  31.      * @inheritDoc
  32.      *
  33.      * @param string $queueName
  34.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Task $task
  35.      * @param string $context
  36.      * @param int $priority
  37.      *
  38.      * @return \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem
  39.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueStorageUnavailableException
  40.      */
  41.     public function enqueue($queueNameTask $task$context ''$priority Priority::NORMAL)
  42.     {
  43.         $item parent::enqueue($queueName$task$context$priority);
  44.         $this->fireStateTransitionEvent(new QueueItemEnqueuedEvent($queueName$task$context$priority));
  45.         return $item;
  46.     }
  47.     /**
  48.      * @inheritDoc
  49.      *
  50.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  51.      *
  52.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\AbortTaskExecutionException
  53.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueItemDeserializationException
  54.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueStorageUnavailableException
  55.      */
  56.     public function start(QueueItem $queueItem)
  57.     {
  58.         parent::start($queueItem);
  59.         $this->fireStateTransitionEvent(new QueueItemStartedEvent($queueItem));
  60.     }
  61.     public function finish(QueueItem $queueItem)
  62.     {
  63.         parent::finish($queueItem);
  64.         $this->fireStateTransitionEvent(new QueueItemFinishedEvent($queueItem));
  65.     }
  66.     /**
  67.      * @inheritDoc
  68.      *
  69.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  70.      *
  71.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueStorageUnavailableException
  72.      */
  73.     public function requeue(QueueItem $queueItem)
  74.     {
  75.         parent::requeue($queueItem);
  76.         $this->fireStateTransitionEvent(new QueueItemRequeuedEvent($queueItem));
  77.     }
  78.     /**
  79.      * @inheritDoc
  80.      *
  81.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  82.      * @param string $abortDescription
  83.      *
  84.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueStorageUnavailableException
  85.      */
  86.     public function abort(QueueItem $queueItem$abortDescription)
  87.     {
  88.         parent::abort($queueItem$abortDescription);
  89.         $this->fireStateTransitionEvent(new QueueItemAbortedEvent($queueItem$abortDescription));
  90.     }
  91.     /**
  92.      * @inheritDoc
  93.      *
  94.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  95.      * @param string $failureDescription
  96.      *
  97.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueStorageUnavailableException
  98.      */
  99.     public function fail(QueueItem $queueItem$failureDescription)
  100.     {
  101.         parent::fail($queueItem$failureDescription);
  102.         $this->fireStateTransitionEvent(new QueueItemFailedEvent($queueItem$failureDescription));
  103.     }
  104.     /**
  105.      * Performs execution requirements validation validation.
  106.      *
  107.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  108.      *
  109.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\ExecutionRequirementsNotMetException
  110.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueItemDeserializationException
  111.      */
  112.     public function validateExecutionRequirements(QueueItem $queueItem)
  113.     {
  114.         if (!$this->getApiStatusProxy()->isAPIActive()) {
  115.             throw new ExecutionRequirementsNotMetException('API not operational.');
  116.         }
  117.         if (!$this->requiresAuthorization($queueItem)) {
  118.             return;
  119.         }
  120.         if (!$this->getAuthService()->getFreshOfflineStatus()) {
  121.             return;
  122.         }
  123.         throw new ExecutionRequirementsNotMetException('User is offline.');
  124.     }
  125.     /**
  126.      * Checks if task requires authorization for execution.
  127.      *
  128.      * @param \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\QueueItem $queueItem
  129.      *
  130.      * @return bool
  131.      * @throws \Crsw\CleverReachOfficial\Core\Infrastructure\TaskExecution\Exceptions\QueueItemDeserializationException
  132.      */
  133.     protected function requiresAuthorization(QueueItem $queueItem)
  134.     {
  135.         $authorizationFreeTasks = array(
  136.             ConnectTask::getClassName(),
  137.             ScheduleCheckTask::getClassName(),
  138.             TaskCleanupTask::getClassName(),
  139.         );
  140.         return !in_array($queueItem->getTaskType(), $authorizationFreeTaskstrue);
  141.     }
  142.     /**
  143.      * @param Event $event
  144.      */
  145.     public function fireStateTransitionEvent(Event $event)
  146.     {
  147.         QueueItemStateTransitionEventBus::getInstance()->fire($event);
  148.     }
  149.     /**
  150.      * Retrieves api status proxy.
  151.      *
  152.      * @return Proxy
  153.      */
  154.     private function getApiStatusProxy()
  155.     {
  156.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  157.         return ServiceRegister::getService(Proxy::CLASS_NAME);
  158.     }
  159.     /**
  160.      * Retrieves authorization service.
  161.      *
  162.      * @return AuthorizationService
  163.      */
  164.     private function getAuthService()
  165.     {
  166.         /** @noinspection PhpIncompatibleReturnTypeInspection */
  167.         return ServiceRegister::getService(AuthorizationService::CLASS_NAME);
  168.     }
  169. }