custom/plugins/NetiNextStoreLocator/src/Subscriber/EntitySubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace NetInventors\NetiNextStoreLocator\Subscriber;
  3. use NetInventors\NetiNextStoreLocator\Components\EntityFilter;
  4. use NetInventors\NetiNextStoreLocator\Components\GeoLocation\GeoLocation;
  5. use NetInventors\NetiNextStoreLocator\Components\GeoLocation\Struct\Address;
  6. use NetInventors\NetiNextStoreLocator\Core\Content\Store\StoreEntity;
  7. use NetInventors\NetiNextStoreLocator\Storefront\Framework\Seo\SeoUrlRoute\StorePageSeoUrlRoute;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\Log\LoggingService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class EntitySubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EntityFilter
  19.      */
  20.     private $entityFilter;
  21.     /**
  22.      * @var GeoLocation
  23.      */
  24.     private $geoLocation;
  25.     private $cnt;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $repository;
  30.     /**
  31.      * @var LoggerInterface
  32.      */
  33.     private $logger;
  34.     /**
  35.      * @var SeoUrlUpdater
  36.      */
  37.     private $seoUrlUpdater;
  38.     public function __construct(
  39.         EntityFilter              $entityFilter,
  40.         GeoLocation               $geoLocation,
  41.         EntityRepositoryInterface $repository,
  42.         LoggerInterface           $logger,
  43.         SeoUrlUpdater             $seoUrlUpdater
  44.     ) {
  45.         $this->entityFilter  $entityFilter;
  46.         $this->geoLocation   $geoLocation;
  47.         $this->repository    $repository;
  48.         $this->logger        $logger;
  49.         $this->seoUrlUpdater $seoUrlUpdater;
  50.     }
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             'neti_store_locator.written' => 'onWritten',
  55.         ];
  56.     }
  57.     public function onWritten(EntityWrittenEvent $event): void
  58.     {
  59.         $this->seoUrlUpdater->update(StorePageSeoUrlRoute::ROUTE_NAME$event->getIds());
  60.         $ids $this->entityFilter->getAffectedIDs($event);
  61.         if (!empty($ids)) {
  62.             /**
  63.              * @psalm-suppress MixedArgumentTypeCoercion
  64.              *
  65.              * This is the correct way to search for a specific ID
  66.              */
  67.             $criteria = new Criteria($ids);
  68.             $entities $this->repository->search($criteria$event->getContext());
  69.             $updates  = [];
  70.             /** @var StoreEntity $entity */
  71.             foreach ($entities as $entity) {
  72.                 $address Address::createFrom($entity);
  73.                 try {
  74.                     $coords   $this->geoLocation->getCoords($address);
  75.                     $timezone $this->geoLocation->getTimezone($coords);
  76.                     $updates[] = [
  77.                         'id'            => $entity->getId(),
  78.                         'longitude'     => $coords->getLongitude(),
  79.                         'latitude'      => $coords->getLatitude(),
  80.                         'googlePlaceID' => $coords->getPlaceId(),
  81.                         'timezone'      => $timezone,
  82.                     ];
  83.                 } catch (\Exception $ex) {
  84.                     $this->logger->notice(
  85.                         'Unable to get the coordinates for the address of a store',
  86.                         [
  87.                             'exception' => $ex->getMessage(),
  88.                         ]
  89.                     );
  90.                 }
  91.             }
  92.             if (!empty($updates)) {
  93.                 $this->repository->update($updates$event->getContext());
  94.             }
  95.         }
  96.     }
  97. }