<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\PrestashopConnectorBundle\EventSubscriber;
use App\Services\Configuration;
use Bluue\ApiCmsBundle\Repository\MappingRepository;
use Bluue\SalesBundle\Entity\Order;
use Bluue\SalesBundle\Event\EditStateForOrderEvent;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluue\PrestashopConnectorBundle\Message\UpdateStateForOrderToPrestaMessage;
class EditStateForOrderSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface $messageBus
*/
protected MessageBusInterface $messageBus;
/**
* @var MappingRepository
*/
protected MappingRepository $mappingRepo;
/**
* @var Configuration
*/
protected Configuration $configuration;
public function __construct(
MessageBusInterface $messageBus,
MappingRepository $mappingRepo,
Configuration $configuration
) {
$this->messageBus = $messageBus;
$this->mappingRepo = $mappingRepo;
$this->configuration = $configuration;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
EditStateForOrderEvent::POST_SUBMIT => 'postSubmit'
];
}
/**
* @param EditStateForOrderEvent $event
* @return void
*/
public function postSubmit(EditStateForOrderEvent $event): void
{
$order = $event->getOrder();
$context = $order->getContext();
$this->messageBus->dispatch(new UpdateStateForOrderToPrestaMessage(
$order->getId()->toRfc4122(),
$order->getOrderState()->getId()->toRfc4122()
));
$generateInvoice = $this->configuration->get(
'prestashop_connector_bundle__invoices_generated_in_bluue',
$context
);
$invoiceLimitDate = $this->configuration->get(
'prestashop_connector_bundle__limit_invoice_date',
$context
);
$mappingOrder = $this->mappingRepo->findMapping(
$order->getId(),
'prestashop',
Order::class,
null,
$context->getId()
);
if (
$mappingOrder
&& (
!$generateInvoice
|| !$invoiceLimitDate
|| $order->getCreatedAt()->format('Y-m-d') < $invoiceLimitDate
)
) {
$event->setCancelInvoice(true);
}
}
}