<?php
declare(strict_types=1);
namespace App\Module\Checkout\Subscriber;
use App\Constant\EventPriority;
use App\Module\Checkout\Notifier\OrderConfirmationNotifier;
use App\Module\Checkout\Provider\DeniosCustomerProviderInterface;
use App\Repository\CustomObjectRepository;
use BestIt\CommercetoolsODM\Repository\OrderRepository;
use App\Module\ShortSimpleCheckout\Event\Order\OrderCreatedEvent;
use App\Module\ShortSimpleCheckout\Events;
use Commercetools\Core\Model\Order\Order;
use Denios\SharedConstant\Payment\Type;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Handling events that happen after the order has been created
*
* @package App\Module\Checkout\Subscriber
* @see OrderAddressToCustomerSubscriber for events that happen just before the order is created
*/
class OrderCreatedSubscriber implements EventSubscriberInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
private CustomObjectRepository $customObjectRepository;
private OrderRepository $orderRepository;
private OrderConfirmationNotifier $orderConfirmationNotifier;
private DeniosCustomerProviderInterface $deniosCustomerProvider;
/**
* @param CustomObjectRepository $customObjectRepository
* @param OrderRepository $orderRepository
* @param OrderConfirmationNotifier $orderConfirmationNotifier
* @param DeniosCustomerProviderInterface $deniosCustomerProvider
*/
public function __construct(
CustomObjectRepository $customObjectRepository,
OrderRepository $orderRepository,
OrderConfirmationNotifier $orderConfirmationNotifier,
DeniosCustomerProviderInterface $deniosCustomerProvider
) {
$this->customObjectRepository = $customObjectRepository;
$this->orderRepository = $orderRepository;
$this->orderConfirmationNotifier = $orderConfirmationNotifier;
$this->deniosCustomerProvider = $deniosCustomerProvider;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
Events::ORDER_CREATED => [
['sendConfirmationMail', EventPriority::SEND_CONFIRMATION_MAIL],
]
];
}
/**
* @param Order $order
*
* @return string|null|Type::*
*/
private function getPaymentType(Order $order): ?string
{
$custom = $order->getCustom();
if (empty($custom)) {
return null;
}
return $custom->getFields()->get('paymentType');
}
/**
* Send confirmation mail for pre payment and invoice
* confirmation mail for credit card and paypal is handled \App\Module\Checkout\Subscriber\PaymentReceivedSubscriber::sendConfirmationMail
*
* @param OrderCreatedEvent $event
*
* @return void
*/
public function sendConfirmationMail(OrderCreatedEvent $event): void
{
$order = $event->getOrder();
$paymentType = $this->getPaymentType($order);
if (in_array($paymentType, [Type::PRE_PAYMENT, Type::INVOICE])) {
$this->logger->debug(sprintf(
'Sending confirmation mail for order %s with pre payment or invoice',
$order->getId()
));
$this->orderConfirmationNotifier->sendConfirmationMail($order);
}
}
}