custom/plugins/NetiNextProductDetailCms/src/Decoration/ProductRepositoryDecorator.php line 68

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextProductDetailCms\Decoration;
  4. use NetInventors\NetiNextProductDetailCms\Core\Content\Product\Aggregate\ProductCms\ProductCmsDefinition;
  5. use NetInventors\NetiNextProductDetailCms\Service\PluginConfig;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\CloneBehavior;
  16. class ProductRepositoryDecorator implements EntityRepositoryInterface
  17. {
  18.     private EntityRepositoryInterface $mainService;
  19.     private PluginConfig              $config;
  20.     private EntityRepositoryInterface $productCmsRepository;
  21.     public function __construct(
  22.         EntityRepositoryInterface $mainService,
  23.         PluginConfig              $config,
  24.         EntityRepositoryInterface $productCmsRepository
  25.     ) {
  26.         $this->mainService          $mainService;
  27.         $this->config               $config;
  28.         $this->productCmsRepository $productCmsRepository;
  29.     }
  30.     public function getDefinition(): EntityDefinition
  31.     {
  32.         return $this->mainService->getDefinition();
  33.     }
  34.     public function aggregate(Criteria $criteriaContext $context): AggregationResultCollection
  35.     {
  36.         return $this->mainService->aggregate($criteria$context);
  37.     }
  38.     public function searchIds(Criteria $criteriaContext $context): IdSearchResult
  39.     {
  40.         return $this->mainService->searchIds($criteria$context);
  41.     }
  42.     public function clone(
  43.         string        $id,
  44.         Context       $context,
  45.         ?string       $newId null,
  46.         CloneBehavior $behavior null
  47.     ): EntityWrittenContainerEvent {
  48.         $event $this->mainService->clone($id$context$newId$behavior);
  49.         $this->removeProductCmsRelationIfEnabled($event);
  50.         return $event;
  51.     }
  52.     public function search(Criteria $criteriaContext $context): EntitySearchResult
  53.     {
  54.         return $this->mainService->search($criteria$context);
  55.     }
  56.     public function update(array $dataContext $context): EntityWrittenContainerEvent
  57.     {
  58.         return $this->mainService->update($data$context);
  59.     }
  60.     public function upsert(array $dataContext $context): EntityWrittenContainerEvent
  61.     {
  62.         return $this->mainService->upsert($data$context);
  63.     }
  64.     public function create(array $dataContext $context): EntityWrittenContainerEvent
  65.     {
  66.         return $this->mainService->create($data$context);
  67.     }
  68.     public function delete(array $dataContext $context): EntityWrittenContainerEvent
  69.     {
  70.         return $this->mainService->delete($data$context);
  71.     }
  72.     public function createVersion(string $idContext $context, ?string $name null, ?string $versionId null): string
  73.     {
  74.         return $this->mainService->createVersion($id$context$name$versionId);
  75.     }
  76.     public function merge(string $versionIdContext $context): void
  77.     {
  78.         $this->mainService->merge($versionId$context);
  79.     }
  80.     private function removeProductCmsRelationIfEnabled(EntityWrittenContainerEvent $event): void
  81.     {
  82.         if ($this->config->isDisableCopyAssociation() !== true) {
  83.             return;
  84.         }
  85.         /** @var EntityWrittenEvent $nestedEvent */
  86.         foreach ($event->getEvents() as $nestedEvent) {
  87.             if ($nestedEvent->getEntityName() === ProductCmsDefinition::ENTITY_NAME) {
  88.                 foreach ($nestedEvent->getWriteResults() as $entityWriteResult) {
  89.                     $id $entityWriteResult->getPrimaryKey();
  90.                     $this->productCmsRepository->delete(
  91.                         [
  92.                             [
  93.                                 'id' => $id,
  94.                             ],
  95.                         ],
  96.                         $event->getContext()
  97.                     );
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }