vendor/dachcom-digital/dynamic-search-data-provider-crawler/src/DsWebCrawlerBundle/EventSubscriber/AbortEventSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace DsWebCrawlerBundle\EventSubscriber;
  3. use DsWebCrawlerBundle\DsWebCrawlerBundle;
  4. use DsWebCrawlerBundle\DsWebCrawlerEvents;
  5. use DynamicSearchBundle\DynamicSearchEvents;
  6. use DynamicSearchBundle\Event\ErrorEvent;
  7. use DynamicSearchBundle\EventDispatcher\DynamicSearchEventDispatcherInterface;
  8. use DynamicSearchBundle\Logger\LoggerInterface;
  9. use DynamicSearchBundle\Normalizer\Resource\ResourceMetaInterface;
  10. use Symfony\Contracts\EventDispatcher\Event;
  11. use Symfony\Component\EventDispatcher\GenericEvent;
  12. use VDB\Spider\Event\SpiderEvents;
  13. class AbortEventSubscriber implements EventSubscriberInterface
  14. {
  15.     protected bool $dispatched;
  16.     protected string $contextName;
  17.     protected string $contextDispatchType;
  18.     protected string $crawlType;
  19.     protected ?ResourceMetaInterface $resourceMeta null;
  20.     protected LoggerInterface $logger;
  21.     protected DynamicSearchEventDispatcherInterface $eventDispatcher;
  22.     public function __construct(DynamicSearchEventDispatcherInterface $eventDispatcher)
  23.     {
  24.         $this->dispatched false;
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     public function setContextName(string $contextName): void
  28.     {
  29.         $this->contextName $contextName;
  30.     }
  31.     public function setContextDispatchType(string $contextDispatchType): void
  32.     {
  33.         $this->contextDispatchType $contextDispatchType;
  34.     }
  35.     public function setCrawlType(string $crawlType): void
  36.     {
  37.         $this->crawlType $crawlType;
  38.     }
  39.     public function setResourceMeta(?ResourceMetaInterface $resourceMeta): void
  40.     {
  41.         $this->resourceMeta $resourceMeta;
  42.     }
  43.     public function setLogger(LoggerInterface $logger): void
  44.     {
  45.         $this->logger $logger;
  46.     }
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             SpiderEvents::SPIDER_CRAWL_USER_STOPPED  => 'stoppedCrawler',
  51.             DsWebCrawlerEvents::DS_WEB_CRAWLER_ERROR => 'errorCrawler',
  52.         ];
  53.     }
  54.     public function stoppedCrawler(Event $event): void
  55.     {
  56.         // only trigger once.
  57.         if ($this->dispatched === true) {
  58.             return;
  59.         }
  60.         $this->dispatched true;
  61.         $newDataEvent = new ErrorEvent($this->contextName'crawler has been stopped by user'DsWebCrawlerBundle::PROVIDER_NAME);
  62.         $this->eventDispatcher->dispatch($newDataEventDynamicSearchEvents::ERROR_DISPATCH_ABORT);
  63.     }
  64.     public function errorCrawler(GenericEvent $event): void
  65.     {
  66.         // only trigger once.
  67.         if ($this->dispatched === true) {
  68.             return;
  69.         }
  70.         $this->dispatched true;
  71.         $errorEvent = new ErrorEvent($this->contextName$event->getArgument('message'), DsWebCrawlerBundle::PROVIDER_NAME$event->getArgument('exception'));
  72.         $this->eventDispatcher->dispatch($errorEventDynamicSearchEvents::ERROR_DISPATCH_CRITICAL);
  73.     }
  74. }