<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCoupon\Decorator;
use NetInventors\NetiNextEasyCoupon\Struct\PluginConfigStruct;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
use Shopware\Core\Content\Cms\DataResolver\Element\CmsElementResolverInterface;
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class ProductCmsElementResolverDecorator implements CmsElementResolverInterface
{
protected CmsElementResolverInterface $decoratedService;
protected PluginConfigStruct $pluginConfig;
public function __construct(
CmsElementResolverInterface $decoratedService,
PluginConfigStruct $pluginConfig
) {
$this->decoratedService = $decoratedService;
$this->pluginConfig = $pluginConfig;
}
public function getType(): string
{
return $this->decoratedService->getType();
}
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$newCriteriaCollection = new CriteriaCollection();
$criteriaCollection = $this->decoratedService->collect($slot, $resolverContext);
if (!$criteriaCollection) {
return $criteriaCollection;
}
/** @var array<string, array<string, Criteria>> $allCriteria */
$allCriteria = $criteriaCollection->all();
foreach ($allCriteria as $definition => $criteria) {
foreach ($criteria as $key => $criterion) {
if (ProductDefinition::class === $definition || SalesChannelProductDefinition::class === $definition) {
$criterion->addAssociation('netiEasyCouponProduct.extraOptions');
}
$newCriteriaCollection->add($key, $definition, $criterion);
}
}
return $newCriteriaCollection;
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$this->decoratedService->enrich($slot, $resolverContext, $result);
}
}