<?php
declare(strict_types=1);
namespace NetInventors\NetiNextProductDetailCms\Decoration;
use NetInventors\NetiNextProductDetailCms\Core\Content\Product\Aggregate\ProductCms\ProductCmsDefinition;
use NetInventors\NetiNextProductDetailCms\Service\PluginConfig;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Write\CloneBehavior;
class ProductRepositoryDecorator implements EntityRepositoryInterface
{
private EntityRepositoryInterface $mainService;
private PluginConfig $config;
private EntityRepositoryInterface $productCmsRepository;
public function __construct(
EntityRepositoryInterface $mainService,
PluginConfig $config,
EntityRepositoryInterface $productCmsRepository
) {
$this->mainService = $mainService;
$this->config = $config;
$this->productCmsRepository = $productCmsRepository;
}
public function getDefinition(): EntityDefinition
{
return $this->mainService->getDefinition();
}
public function aggregate(Criteria $criteria, Context $context): AggregationResultCollection
{
return $this->mainService->aggregate($criteria, $context);
}
public function searchIds(Criteria $criteria, Context $context): IdSearchResult
{
return $this->mainService->searchIds($criteria, $context);
}
public function clone(
string $id,
Context $context,
?string $newId = null,
CloneBehavior $behavior = null
): EntityWrittenContainerEvent {
$event = $this->mainService->clone($id, $context, $newId, $behavior);
$this->removeProductCmsRelationIfEnabled($event);
return $event;
}
public function search(Criteria $criteria, Context $context): EntitySearchResult
{
return $this->mainService->search($criteria, $context);
}
public function update(array $data, Context $context): EntityWrittenContainerEvent
{
return $this->mainService->update($data, $context);
}
public function upsert(array $data, Context $context): EntityWrittenContainerEvent
{
return $this->mainService->upsert($data, $context);
}
public function create(array $data, Context $context): EntityWrittenContainerEvent
{
return $this->mainService->create($data, $context);
}
public function delete(array $data, Context $context): EntityWrittenContainerEvent
{
return $this->mainService->delete($data, $context);
}
public function createVersion(string $id, Context $context, ?string $name = null, ?string $versionId = null): string
{
return $this->mainService->createVersion($id, $context, $name, $versionId);
}
public function merge(string $versionId, Context $context): void
{
$this->mainService->merge($versionId, $context);
}
private function removeProductCmsRelationIfEnabled(EntityWrittenContainerEvent $event): void
{
if ($this->config->isDisableCopyAssociation() !== true) {
return;
}
/** @var EntityWrittenEvent $nestedEvent */
foreach ($event->getEvents() as $nestedEvent) {
if ($nestedEvent->getEntityName() === ProductCmsDefinition::ENTITY_NAME) {
foreach ($nestedEvent->getWriteResults() as $entityWriteResult) {
$id = $entityWriteResult->getPrimaryKey();
$this->productCmsRepository->delete(
[
[
'id' => $id,
],
],
$event->getContext()
);
}
}
}
}
}