vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/EventListener/SimpleProductLockingListener.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\EventListener;
  12. use Doctrine\DBAL\LockMode;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Sylius\Component\Core\Model\ProductInterface;
  15. use Sylius\Component\Core\Model\ProductVariantInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Webmozart\Assert\Assert;
  18. final class SimpleProductLockingListener
  19. {
  20.     public function __construct(private EntityManagerInterface $manager)
  21.     {
  22.     }
  23.     /**
  24.      * @throws \InvalidArgumentException
  25.      */
  26.     public function lock(GenericEvent $event): void
  27.     {
  28.         $product $event->getSubject();
  29.         Assert::isInstanceOf($productProductInterface::class);
  30.         if ($product->isSimple()) {
  31.             /** @var ProductVariantInterface $productVariant */
  32.             $productVariant $product->getVariants()->first();
  33.             $this->manager->lock($productVariantLockMode::OPTIMISTIC$productVariant->getVersion());
  34.         }
  35.     }
  36. }