<?php
declare(strict_types=1);
namespace Hitso\Bundle\CatalogBundle\EventListener;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Hitso\Bundle\CatalogBundle\Entity\Product;
use Hitso\Bundle\CatalogBundle\Entity\TaxRate;
use Hitso\Bundle\CatalogBundle\Service\Tax\Helper\TaxHelperInterface;
use Hitso\Bundle\CommonBundle\Doctrine\Event\EntityEvent;
use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
use Hitso\Bundle\SearchBundle\Manager\SearchManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class ProductEventListener
*
* @package Hitso\Bundle\CatalogBundle\EventListener
*/
class ProductEventListener implements EventSubscriberInterface
{
/**
* @var TaxHelperInterface
*/
private $taxHelper;
/**
* @var SearchManager
*/
private $searchManager;
/**
* @var ManagerCollection
*/
private $managerCollection;
/**
* TaxHelperInterface constructor.
*
* @param TaxHelperInterface $taxHelper
* @param SearchManager
*/
public function __construct(
TaxHelperInterface $taxHelper,
SearchManager $searchManager,
ManagerCollection $managerCollection
) {
$this->taxHelper = $taxHelper;
$this->searchManager = $searchManager;
$this->managerCollection = $managerCollection;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
'prePersist',
'preUpdate',
'product.post_create' => 'onPostCreate',
'product.post_update' => 'onPostUpdate',
];
}
/**
* @param LifecycleEventArgs $eventArgs
*/
public function prePersist(LifecycleEventArgs $eventArgs): void
{
if (method_exists($eventArgs, 'getEntity')) {
$product = $eventArgs->getEntity();
if ($product instanceof Product) {
$this->calculateProductPrice($product);
}
}
}
/**
* @param LifecycleEventArgs $eventArgs
*/
public function preUpdate(LifecycleEventArgs $eventArgs): void
{
if (method_exists($eventArgs, 'getEntity')) {
$product = $eventArgs->getEntity();
if ($product instanceof Product) {
$this->calculateProductPrice($product);
}
}
}
/**
* @param Product $product
*/
private function calculateProductPrice(Product $product): void
{
if ($product instanceof Product) {
if ($product->getTaxRate() instanceof TaxRate) {
$netAmount = round($product->getNetAmount(), 2);
$grossAmount = $this->taxHelper->calculateGrossAmount($netAmount, $product->getTaxRate()->getValue());
$product->setGrossAmount($grossAmount);
$product->setNetAmount($netAmount);
$product->setTaxAmount($grossAmount - $netAmount);
}
}
}
/**
* @param EntityEvent $event
*/
public function onPostCreate(EntityEvent $event): void
{
$entity = $event->getEntity();
if ($entity instanceof Product) {
try {
$type = $this->searchManager->getType('product');
$documents = new ArrayCollection();
$document = $type->createDocument($entity);
$documents->add($document);
$this->searchManager->addDocuments($documents, $type->getName());
} catch (\Exception $e) {
}
}
}
/**
* @param EntityEvent $event
*/
public function onPostUpdate(EntityEvent $event): void
{
$entity = $event->getEntity();
if ($entity instanceof Product) {
try {
$type = $this->searchManager->getType('product');
$document = $type->createDocument($entity);
$this->searchManager->updateDocument($document);
} catch (\Exception $e) {
}
}
}
}