custom/plugins/FTBPlugin/src/Subscriber/FTBSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FTBPlugin\Subscriber;
  3. use Monolog\Logger;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  9. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  10. use Shopware\Storefront\Page\Suggest\SuggestPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class FTBSubscriber implements EventSubscriberInterface
  13. {
  14.     private $search_notfound_repository;
  15.     private $logger;
  16.     public function __construct(Logger $loggerEntityRepositoryInterface $dreiwm_search_notfound_repo)
  17.     {
  18.         $this->logger $logger;
  19.         $this->search_notfound_repository $dreiwm_search_notfound_repo;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             SuggestPageLoadedEvent::class => 'onSearch',
  25.             SearchPageLoadedEvent::class => 'onSearchPage',
  26.             CustomerEvents:: CUSTOMER_REGISTER_EVENT => 'customerRegistered',
  27.         ];
  28.     }
  29.     /**
  30.      * Suche (Ergebniss-Seite):
  31.      * Wenn bei einer Suche keine Ergebnisse gefunden werden, soll der String, nach dem gesucht wird,
  32.      * in die DB gespeichert werden
  33.      */
  34.     public function onSearchPage(SearchPageLoadedEvent $event)
  35.     {
  36.         if ($event->getPage()->getListing()->getTotal() === 0) {
  37.             $this->saveSearchTerm(
  38.                 $event->getPage()->getSearchTerm(),
  39.                 $event->getContext()
  40.             );
  41.         }
  42.     }
  43.     /**
  44.      * Ajax-Suche (Live):
  45.      * Wenn bei einer Suche keine Ergebnisse gefunden werden, soll der String, nach dem gesucht wird,
  46.      * in die DB gespeichert werden
  47.      */
  48.     public function onSearch(SuggestPageLoadedEvent $event)
  49.     {
  50.         if ($event->getPage()->getSearchResult()->getTotal() === 0) {
  51.             $this->saveSearchTerm(
  52.                 $event->getPage()->getSearchTerm(),
  53.                 $event->getContext()
  54.             );
  55.         }
  56.     }
  57.     /**
  58.      * schreiben den Searchterm in die DB
  59.      */
  60.     private function saveSearchTerm($as_searchTerm$context)
  61.     {
  62.         $sessionId session_id();
  63.         try {
  64.             if ($this->searchTermNotWritten($as_searchTerm$sessionId$context)) {
  65.                 $this->search_notfound_repository
  66.                     ->create([
  67.                         [
  68.                             'searchTerm' => strip_tags($as_searchTerm),
  69.                             'session' => $sessionId ?: '',
  70.                             'date' => new \DateTime()
  71.                         ]
  72.                     ], $context);
  73.             }
  74.         } catch (\Exception $e) {
  75.             $this->logger->error($e->getMessage(), ['exception' => $e]);
  76.         }
  77.     }
  78.     /**
  79.      * Return true, wenn es bereits nicht geschrieben wurde
  80.      * retunr false, wenn es bereits min einen Eintrag gibt.
  81.      * @param $as_searchTerm
  82.      * @param $as_sessionId
  83.      * @param $context
  84.      * @return bool
  85.      */
  86.     private function searchTermNotWritten($as_searchTerm$as_sessionId$context)
  87.     {
  88.         $criteria = new Criteria([]);
  89.         $criteria->addFilter(new EqualsFilter('session'$as_sessionId));
  90.         $criteria->addFilter(new EqualsFilter('searchTerm'$as_searchTerm));
  91.         $criteria->addFilter(new RangeFilter('date', [
  92.             RangeFilter::GTE => date('Y-m-d H:i:s'strtotime('now -2min')),
  93.         ]));
  94.         $res $this->search_notfound_repository->search($criteria$context);
  95.         return $res->count() === 0;
  96.     }
  97. }