<?php
namespace App\EventSubscriber;
use App\Event\QuotationUpdatedEvent;
use App\Service\UserLog;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class QuotationUpdatedEventSubscriber implements EventSubscriberInterface
{
protected $em;
protected $userLog;
public function __construct(
EntityManagerInterface $em,
UserLog $userLog
)
{
$this->userLog = $userLog;
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
QuotationUpdatedEvent::REQUESTED_QUANTITIES => [
['updateTargetPrices'],
// ['logAction']
]
];
}
public function updateTargetPrices(QuotationUpdatedEvent $event)
{
$quotation = $event->getQuotation();
foreach ($quotation->getCustomerTargetPrices() as $targetPrice) {
$prices = $targetPrice->getPrice();
foreach ($quotation->getRequestedQuantities() as $quantity) {
if (!isset($prices[$quantity['value']])) {
$prices[$quantity['value']] = null;
}
}
$targetPrice->setPrice($prices);
}
$this->em->flush();
}
// public function logAction(QuotationUpdatedEvent $event)
// {
// $quotation = $event->getQuotation();
//
// $this->userLog->log(
// 'quotation',
// QuotationUpdatedEvent::REQUESTED_QUANTITIES,
// $quotation->getId(),
// 'Quotation updated. #' . $quotation->getOrderNumber()
// );
// }
}