src/Sylius/Bundle/CoreBundle/EventListener/SimpleProductLockingListener.php line 41

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 Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
  17. use Symfony\Component\EventDispatcher\GenericEvent;
  18. use Webmozart\Assert\Assert;
  19. final class SimpleProductLockingListener
  20. {
  21.     /** @var EntityManagerInterface */
  22.     private $manager;
  23.     /** @var ProductVariantResolverInterface */
  24.     private $variantResolver;
  25.     public function __construct(EntityManagerInterface $managerProductVariantResolverInterface $variantResolver)
  26.     {
  27.         $this->manager $manager;
  28.         $this->variantResolver $variantResolver;
  29.     }
  30.     /**
  31.      * @throws \InvalidArgumentException
  32.      */
  33.     public function lock(GenericEvent $event): void
  34.     {
  35.         $product $event->getSubject();
  36.         Assert::isInstanceOf($productProductInterface::class);
  37.         if ($product->isSimple()) {
  38.             /** @var ProductVariantInterface $productVariant */
  39.             $productVariant $this->variantResolver->getVariant($product);
  40.             $this->manager->lock($productVariantLockMode::OPTIMISTIC$productVariant->getVersion());
  41.         }
  42.     }
  43. }