custom/static-plugins/GlobusSW6/src/Subscriber/DemoValidatorSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use GlobusSW6\Service\Ghs\HazardPictogramService;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  13. use Shopware\Core\Content\Product\ProductCollection;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. class DemoValidatorSubscriber implements EventSubscriberInterface {
  16.     /**
  17.      * @var Connection
  18.      */
  19.     private $connection;
  20.     /** @var EntityRepositoryInterface */
  21.     private $productRepository;
  22.     /**
  23.      * DemoValidatorSubscriber constructor.
  24.      * @param Connection $connection
  25.      * @param EntityRepositoryInterface $productRepository
  26.      */
  27.     public function __construct(Connection $connectionEntityRepositoryInterface $productRepository)
  28.     {
  29.         $this->connection $connection;
  30.         $this->productRepository $productRepository;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'product.search.result.loaded' => 'onProductLoaded'
  36.         ];
  37.     }
  38.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  39.     {
  40.         // do not fire event in backend context
  41.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  42.             if ($event->getContext()->getSource()->isAdmin()) {
  43.                 return;
  44.             }
  45.         }
  46.         /** @var ProductCollection $products */
  47.         $products $event->getResult()->getEntities();
  48.         if($products === null){
  49.             return;
  50.         }
  51.         $productIds $products->getIds();
  52.         $demoData = ['0680500957''0779052680''0782281251'];
  53.         $demoDataIds $this->getProductIdsByUnitradeIds($demoData);
  54.         $intersection array_intersect($productIds$demoDataIds);
  55.         if (count($intersection) == 0){
  56.             return;
  57.         }
  58.         // check if demo products contain expected extension
  59.         foreach ($intersection as $productId){
  60.             $product $products->get($productId);
  61.             $this->validateProduct($product);
  62.         }
  63.     }
  64.     private function validateProduct(ProductEntity $product){
  65.         return;
  66.         $extensions $product->getExtensions();
  67.         $product->setExtensions($extensions);
  68.         return;
  69.     }
  70.     private function getProductIdsByUnitradeIds(array $productNmbrs){
  71.         $criteria = new Criteria();
  72.         $criteria->addFilter(new EqualsAnyFilter('productNumber'$productNmbrs));
  73.         $productIds $this->productRepository->search($criteriaContext::createDefaultContext())->getIds();
  74.         return $productIds;
  75.     }
  76. }