vendor/crehler/arrangements/src/Subscriber/ArrangementWriteSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   22.01.2021
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Arrangements\Subscriber;
  13. use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ArrangementWriteSubscriber implements EventSubscriberInterface
  20. {
  21.     private EntityRepositoryInterface $productRepository;
  22.     private EntityRepositoryInterface $arrangementRepository;
  23.     public function __construct(EntityRepositoryInterface $productRepositoryEntityRepositoryInterface $arrangementRepository)
  24.     {
  25.         $this->productRepository $productRepository;
  26.         $this->arrangementRepository $arrangementRepository;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'arrangement.written' => 'onArrangementWritten'
  32.         ];
  33.     }
  34.     public function onArrangementWritten(EntityWrittenEvent $event)
  35.     {
  36.         $ids $event->getIds();
  37.         $context $event->getContext();
  38.         $criteria = new Criteria();
  39.         $criteria->addFilter(new EqualsAnyFilter('id'$ids));
  40.         $result $this->arrangementRepository->search($criteria$context);
  41.         if ($result->getTotal() >= 1)
  42.         {
  43.             /** @var ArrangementEntity $arrangement */
  44.             $arrangement $result->first();
  45.             $this->productRepository->update([[
  46.                 'id' => $arrangement->getMainProductId(),
  47.                 'active' => $arrangement->isActive()
  48.             ]], $context);
  49.         }
  50.     }
  51. }