<?php
namespace GlobusSW6\Subscriber;
use Doctrine\DBAL\Connection;
use GlobusSW6\Service\Ghs\HazardPictogramService;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
class DemoValidatorSubscriber implements EventSubscriberInterface {
/**
* @var Connection
*/
private $connection;
/** @var EntityRepositoryInterface */
private $productRepository;
/**
* DemoValidatorSubscriber constructor.
* @param Connection $connection
* @param EntityRepositoryInterface $productRepository
*/
public function __construct(Connection $connection, EntityRepositoryInterface $productRepository)
{
$this->connection = $connection;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents()
{
return [
'product.search.result.loaded' => 'onProductLoaded'
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event)
{
// do not fire event in backend context
if ($event->getContext()->getSource() instanceof AdminApiSource) {
if ($event->getContext()->getSource()->isAdmin()) {
return;
}
}
/** @var ProductCollection $products */
$products = $event->getResult()->getEntities();
if($products === null){
return;
}
$productIds = $products->getIds();
$demoData = ['0680500957', '0779052680', '0782281251'];
$demoDataIds = $this->getProductIdsByUnitradeIds($demoData);
$intersection = array_intersect($productIds, $demoDataIds);
if (count($intersection) == 0){
return;
}
// check if demo products contain expected extension
foreach ($intersection as $productId){
$product = $products->get($productId);
$this->validateProduct($product);
}
}
private function validateProduct(ProductEntity $product){
return;
$extensions = $product->getExtensions();
$product->setExtensions($extensions);
return;
}
private function getProductIdsByUnitradeIds(array $productNmbrs){
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('productNumber', $productNmbrs));
$productIds = $this->productRepository->search($criteria, Context::createDefaultContext())->getIds();
return $productIds;
}
}