vendor/store.shopware.com/viocustomerprice/src/Subscriber/EntityWrittenContainerSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace VioCustomerPrice\Subscriber;
  3. use Exception;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use VioCustomerPrice\Entity\CustomerPriceEntity;
  14. class EntityWrittenContainerSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepositoryInterface $entityRepository;
  17.     public function __construct(
  18.         EntityRepositoryInterface $entityRepository
  19.     )
  20.     {
  21.         $this->entityRepository $entityRepository;
  22.     }
  23.     /**
  24.      * @inheritDoc
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             EntityWrittenContainerEvent::class => "onEntityWrittenContainerEvent"
  30.         ];
  31.     }
  32.     /**
  33.      * @param EntityWrittenContainerEvent $event
  34.      * @throws InconsistentCriteriaIdsException
  35.      * @throws Exception
  36.      */
  37.     public function onEntityWrittenContainerEvent(EntityWrittenContainerEvent $event): void
  38.     {
  39.         foreach ($event->getEvents() as $nestedEvent) {
  40.             if ($nestedEvent instanceof EntityWrittenEvent) {
  41.                 foreach ($nestedEvent->getWriteResults() as $entityWriteResult) {
  42.                     if ($entityWriteResult->getEntityName() === 'vio_customer_price'
  43.                         && $entityWriteResult->getExistence() !== null
  44.                         && ($entityWriteResult->getOperation() === 'update' || $entityWriteResult->getOperation() === 'insert')
  45.                         && !array_key_exists('quantityEnd'$entityWriteResult->getPayload())
  46.                     ) {
  47.                         $this->reorderPrices($entityWriteResult->getPrimaryKey(), $event->getContext());
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.     }
  53.     /**
  54.      * reorder the related prices to the given price
  55.      * @param string $priceId
  56.      * @param Context $context
  57.      * @throws InconsistentCriteriaIdsException
  58.      * @throws Exception
  59.      */
  60.     private function reorderPrices(string $priceIdContext $context): void
  61.     {
  62.         /** @var CustomerPriceEntity $customerPrice */
  63.         $customerPrice $this->entityRepository->search(new Criteria([$priceId]), $context)->first();
  64.         if ($customerPrice !== null) {
  65.             $criteria = new Criteria();
  66.             $criteria
  67.                 ->addFilter(new EqualsFilter('customerId'$customerPrice->getCustomerId()))
  68.                 ->addFilter(new EqualsFilter('productId'$customerPrice->getProductId()))
  69.                 ->addSorting(new FieldSorting('quantityStart'FieldSorting::ASCENDING));
  70.             $prices $this->entityRepository->search($criteria$context);
  71.             if($prices->count() > 1){
  72.                 /** @var CustomerPriceEntity|null $lastPrice */
  73.                 $lastPrice null;
  74.                 $updateData = [];
  75.                 /** @var CustomerPriceEntity $curPrice */
  76.                 foreach ($prices as $curPrice){
  77.                     if($lastPrice !== null) {
  78.                         $updateData[] = ['id' => $lastPrice->getId(), 'quantityEnd' => $curPrice->getQuantityStart() - 1];
  79.                     }
  80.                     $lastPrice $curPrice;
  81.                 }
  82.                 $this->entityRepository->update($updateData$context);
  83.             }
  84.         }
  85.     }
  86. }