src/Hitso/Bundle/CatalogBundle/EventListener/ProductEventListener.php line 132

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CatalogBundle\EventListener;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Event\LifecycleEventArgs;
  6. use Hitso\Bundle\CatalogBundle\Entity\Product;
  7. use Hitso\Bundle\CatalogBundle\Entity\TaxRate;
  8. use Hitso\Bundle\CatalogBundle\Service\Tax\Helper\TaxHelperInterface;
  9. use Hitso\Bundle\CommonBundle\Doctrine\Event\EntityEvent;
  10. use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
  11. use Hitso\Bundle\SearchBundle\Manager\SearchManager;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * Class ProductEventListener
  15.  *
  16.  * @package Hitso\Bundle\CatalogBundle\EventListener
  17.  */
  18. class ProductEventListener implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var TaxHelperInterface
  22.      */
  23.     private $taxHelper;
  24.     /**
  25.      * @var SearchManager
  26.      */
  27.     private $searchManager;
  28.     /**
  29.      * @var ManagerCollection
  30.      */
  31.     private $managerCollection;
  32.     /**
  33.      * TaxHelperInterface constructor.
  34.      *
  35.      * @param TaxHelperInterface $taxHelper
  36.      * @param SearchManager
  37.      */
  38.     public function __construct(
  39.         TaxHelperInterface $taxHelper,
  40.         SearchManager $searchManager,
  41.         ManagerCollection $managerCollection
  42.     ) {
  43.         $this->taxHelper         $taxHelper;
  44.         $this->searchManager     $searchManager;
  45.         $this->managerCollection $managerCollection;
  46.     }
  47.     /**
  48.      * @return array
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             'prePersist',
  54.             'preUpdate',
  55.             'product.post_create' => 'onPostCreate',
  56.             'product.post_update' => 'onPostUpdate',
  57.         ];
  58.     }
  59.     /**
  60.      * @param LifecycleEventArgs $eventArgs
  61.      */
  62.     public function prePersist(LifecycleEventArgs $eventArgs): void
  63.     {
  64.         if (method_exists($eventArgs'getEntity')) {
  65.             $product $eventArgs->getEntity();
  66.             if ($product instanceof Product) {
  67.                 $this->calculateProductPrice($product);
  68.             }
  69.         }
  70.     }
  71.     /**
  72.      * @param LifecycleEventArgs $eventArgs
  73.      */
  74.     public function preUpdate(LifecycleEventArgs $eventArgs): void
  75.     {
  76.         if (method_exists($eventArgs'getEntity')) {
  77.             $product $eventArgs->getEntity();
  78.             if ($product instanceof Product) {
  79.                 $this->calculateProductPrice($product);
  80.             }
  81.         }
  82.     }
  83.     /**
  84.      * @param Product $product
  85.      */
  86.     private function calculateProductPrice(Product $product): void
  87.     {
  88.         if ($product instanceof Product) {
  89.             if ($product->getTaxRate() instanceof TaxRate) {
  90.                 $netAmount   round($product->getNetAmount(), 2);
  91.                 $grossAmount $this->taxHelper->calculateGrossAmount($netAmount$product->getTaxRate()->getValue());
  92.                 $product->setGrossAmount($grossAmount);
  93.                 $product->setNetAmount($netAmount);
  94.                 $product->setTaxAmount($grossAmount $netAmount);
  95.             }
  96.         }
  97.     }
  98.     /**
  99.      * @param EntityEvent $event
  100.      */
  101.     public function onPostCreate(EntityEvent $event): void
  102.     {
  103.         $entity $event->getEntity();
  104.         if ($entity instanceof Product) {
  105.             try {
  106.                 $type      $this->searchManager->getType('product');
  107.                 $documents = new ArrayCollection();
  108.                 $document  $type->createDocument($entity);
  109.                 $documents->add($document);
  110.                 $this->searchManager->addDocuments($documents$type->getName());
  111.             } catch (\Exception $e) {
  112.             }
  113.         }
  114.     }
  115.     /**
  116.      * @param EntityEvent $event
  117.      */
  118.     public function onPostUpdate(EntityEvent $event): void
  119.     {
  120.         $entity $event->getEntity();
  121.         if ($entity instanceof Product) {
  122.             try {
  123.                 $type     $this->searchManager->getType('product');
  124.                 $document $type->createDocument($entity);
  125.                 $this->searchManager->updateDocument($document);
  126.             } catch (\Exception $e) {
  127.             }
  128.         }
  129.     }
  130. }