<?php declare(strict_types=1);
namespace Nd\Stl\DataResolver;
use Nd\Stl\Struct\LookSliderStruct;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Framework\Uuid\Uuid;
class ShopTheLookSlider extends AbstractCmsElementResolver
{
private EntityRepository $lookRepository;
public function __construct(EntityRepository $lookRepository)
{
$this->lookRepository = $lookRepository;
}
public function getType(): string
{
return 'shop-the-look-slider';
}
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
return null;
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$selectedLooks = $slot->getConfig()['looks']['value'] ?? [];
$request = $resolverContext->getRequest();
$removeLookIds = [];
if ($request->get('_route') === 'frontend.look.detail') {
$currentLookId = $request->get('lookId');
if (is_string($currentLookId) && Uuid::isValid($currentLookId)) {
$removeLookIds = [$currentLookId];
}
}
$criteria = new Criteria();
$criteria->setTitle('cms::shop-the-look-slider');
$criteria->addAssociation('cover');
if (count($selectedLooks) === 0 && count($removeLookIds) > 0) {
$criteria->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [
new EqualsAnyFilter('id', $removeLookIds)
]));
}
if (count($selectedLooks) > 0) {
if (count($removeLookIds) > 0) {
$selectedLooks = array_diff($selectedLooks, $removeLookIds);
}
$criteria->addFilter(new EqualsAnyFilter('id', $selectedLooks));
}
$looks = $this->lookRepository->search($criteria, $resolverContext->getSalesChannelContext()->getContext());
$data = new LookSliderStruct();
$slot->setData($data);
$data->setLooks($looks);
}
}