src/EntityCallback/ProductCallback.php line 85

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XCart\EntityCallback;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Doctrine\Persistence\Event\LifecycleEventArgs;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use XCart\Doctrine\CallbackManager\CallbackManager;
  11. use XCart\Event\Service\DeferrerCallbackRunEvent;
  12. use XLite\Model\Product;
  13. final class ProductCallback
  14. {
  15.     public function __construct(
  16.         private CallbackManager $callbackManager,
  17.         private EntityManagerInterface $entityManager
  18.     ) {
  19.     }
  20.     public function prePersist(Product $productLifecycleEventArgs $args): void
  21.     {
  22.         $time \XLite\Core\Converter::time();
  23.         if (!$product->getDate()) {
  24.             $product->setDate($time);
  25.         }
  26.         if (!$product->getArrivalDate()) {
  27.             $product->setArrivalDate(
  28.                 mktime(000date('m'$time), date('j'$time), date('Y'$time))
  29.             );
  30.         }
  31.         $this->preUpdate($product$args);
  32.     }
  33.     public function preUpdate(Product $productLifecycleEventArgs $args): void
  34.     {
  35.         $product->setUpdateDate(\XLite\Core\Converter::time());
  36.         if (\XLite\Core\Converter::isEmptyString($product->getSku())) {
  37.             $product->setSku(null);
  38.         }
  39.     }
  40.     public function postUpdate(Product $productLifecycleEventArgs $args): void
  41.     {
  42.         if (
  43.             $product->isLowLimitReached()
  44.             && $product->isShouldSend()
  45.             && $product->isInventoryChanged()
  46.         ) {
  47.             $product->sendLowLimitNotification();
  48.         }
  49.         $context $this->callbackManager->getContextValue();
  50.         if (!in_array($context, ['import''load-fixtures''remove-data'], true)) {
  51.             $this->callbackManager->addDeferredCallback(
  52.                 'quickDataProductRecalculate',
  53.                 [
  54.                     $product->getProductId()
  55.                 ]
  56.             );
  57.         }
  58.     }
  59.     public function postPersist(Product $productLifecycleEventArgs $args): void
  60.     {
  61.         $context $this->callbackManager->getContextValue();
  62.         if (!in_array($context, ['import''load-fixtures'], true)) {
  63.             $this->callbackManager->addDeferredCallback(
  64.                 'quickDataProductRecalculate',
  65.                 [
  66.                     $product->getProductId()
  67.                 ]
  68.             );
  69.         }
  70.     }
  71.     public function runQuickData(
  72.         DeferrerCallbackRunEvent $callbackRunEvent,
  73.         string $eventName,
  74.         EventDispatcherInterface $dispatcher
  75.     ): void {
  76.         if ($dtos $callbackRunEvent->getCallbackDto('quickDataProductRecalculate')) {
  77.             foreach ($dtos as $dto) {
  78.                 if ($productId $dto->getArgs()[0]) {
  79.                     $this->entityManager
  80.                         ->getRepository(Product::class)
  81.                         ->find($productId)
  82.                         ?->updateQuickData();
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }