You are here

class TestTaskWorker in Search API 8

Provides a task worker for testing purposes.

Hierarchy

  • class \Drupal\search_api_test_tasks\TestTaskWorker implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TestTaskWorker

1 string reference to 'TestTaskWorker'
search_api_test_tasks.services.yml in tests/search_api_test_tasks/search_api_test_tasks.services.yml
tests/search_api_test_tasks/search_api_test_tasks.services.yml
1 service uses TestTaskWorker
search_api_test_tasks.test_task_worker in tests/search_api_test_tasks/search_api_test_tasks.services.yml
Drupal\search_api_test_tasks\TestTaskWorker

File

tests/search_api_test_tasks/src/TestTaskWorker.php, line 13

Namespace

Drupal\search_api_test_tasks
View source
class TestTaskWorker implements EventSubscriberInterface {

  /**
   * The Search API task manager.
   *
   * @var \Drupal\search_api\Task\TaskManagerInterface
   */
  protected $taskManager;

  /**
   * Log for all received tasks.
   *
   * @var string[][]
   */
  protected $eventLog = [];

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['search_api.task.search_api_test_tasks.success'][] = [
      'success',
    ];
    $events['search_api.task.search_api_test_tasks.fail'][] = [
      'fail',
    ];
    $events['search_api.task.search_api_test_tasks.ignore'][] = [
      'ignore',
    ];
    return $events;
  }

  /**
   * Constructs an IndexTaskManager object.
   *
   * @param \Drupal\search_api\Task\TaskManagerInterface $task_manager
   *   The Search API task manager.
   */
  public function __construct(TaskManagerInterface $task_manager) {
    $this->taskManager = $task_manager;
  }

  /**
   * Handles a task event successfully.
   *
   * @param \Drupal\search_api\Task\TaskEvent $event
   *   The task event.
   */
  public function success(TaskEvent $event) {
    $this
      ->logEvent($event);
    $event
      ->stopPropagation();
  }

  /**
   * Handles a task event with an exception.
   *
   * @param \Drupal\search_api\Task\TaskEvent $event
   *   The task event.
   */
  public function fail(TaskEvent $event) {
    $this
      ->logEvent($event);
    $event
      ->stopPropagation();
    $event
      ->setException(new SearchApiException('fail'));
  }

  /**
   * Ignores a task event.
   *
   * @param \Drupal\search_api\Task\TaskEvent $event
   *   The task event.
   */
  public function ignore(TaskEvent $event) {
    $this
      ->logEvent($event);
  }

  /**
   * Retrieves the event log.
   *
   * @return string[][]
   *   The event log, with each event represented by an associative array of the
   *   task's properties.
   */
  public function getEventLog() {
    return $this->eventLog;
  }

  /**
   * Logs an event.
   *
   * @param \Drupal\search_api\Task\TaskEvent $event
   *   The event.
   */
  protected function logEvent(TaskEvent $event) {
    $this->eventLog[] = $event
      ->getTask()
      ->toArray();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestTaskWorker::$eventLog protected property Log for all received tasks.
TestTaskWorker::$taskManager protected property The Search API task manager.
TestTaskWorker::fail public function Handles a task event with an exception.
TestTaskWorker::getEventLog public function Retrieves the event log.
TestTaskWorker::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TestTaskWorker::ignore public function Ignores a task event.
TestTaskWorker::logEvent protected function Logs an event.
TestTaskWorker::success public function Handles a task event successfully.
TestTaskWorker::__construct public function Constructs an IndexTaskManager object.