src/Module/Checkout/Subscriber/OrderCreatedSubscriber.php line 87

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Checkout\Subscriber;
  4. use App\Constant\EventPriority;
  5. use App\Module\Checkout\Notifier\OrderConfirmationNotifier;
  6. use App\Module\Checkout\Provider\DeniosCustomerProviderInterface;
  7. use App\Repository\CustomObjectRepository;
  8. use BestIt\CommercetoolsODM\Repository\OrderRepository;
  9. use App\Module\ShortSimpleCheckout\Event\Order\OrderCreatedEvent;
  10. use App\Module\ShortSimpleCheckout\Events;
  11. use Commercetools\Core\Model\Order\Order;
  12. use Denios\SharedConstant\Payment\Type;
  13. use Psr\Log\LoggerAwareInterface;
  14. use Psr\Log\LoggerAwareTrait;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. /**
  17. * Handling events that happen after the order has been created
  18. *
  19. * @package App\Module\Checkout\Subscriber
  20. * @see OrderAddressToCustomerSubscriber for events that happen just before the order is created
  21. */
  22. class OrderCreatedSubscriber implements EventSubscriberInterface, LoggerAwareInterface
  23. {
  24. use LoggerAwareTrait;
  25. private CustomObjectRepository $customObjectRepository;
  26. private OrderRepository $orderRepository;
  27. private OrderConfirmationNotifier $orderConfirmationNotifier;
  28. private DeniosCustomerProviderInterface $deniosCustomerProvider;
  29. /**
  30. * @param CustomObjectRepository $customObjectRepository
  31. * @param OrderRepository $orderRepository
  32. * @param OrderConfirmationNotifier $orderConfirmationNotifier
  33. * @param DeniosCustomerProviderInterface $deniosCustomerProvider
  34. */
  35. public function __construct(
  36. CustomObjectRepository $customObjectRepository,
  37. OrderRepository $orderRepository,
  38. OrderConfirmationNotifier $orderConfirmationNotifier,
  39. DeniosCustomerProviderInterface $deniosCustomerProvider
  40. ) {
  41. $this->customObjectRepository = $customObjectRepository;
  42. $this->orderRepository = $orderRepository;
  43. $this->orderConfirmationNotifier = $orderConfirmationNotifier;
  44. $this->deniosCustomerProvider = $deniosCustomerProvider;
  45. }
  46. /**
  47. * @return array
  48. */
  49. public static function getSubscribedEvents(): array
  50. {
  51. return [
  52. Events::ORDER_CREATED => [
  53. ['sendConfirmationMail', EventPriority::SEND_CONFIRMATION_MAIL],
  54. ]
  55. ];
  56. }
  57. /**
  58. * @param Order $order
  59. *
  60. * @return string|null|Type::*
  61. */
  62. private function getPaymentType(Order $order): ?string
  63. {
  64. $custom = $order->getCustom();
  65. if (empty($custom)) {
  66. return null;
  67. }
  68. return $custom->getFields()->get('paymentType');
  69. }
  70. /**
  71. * Send confirmation mail for pre payment and invoice
  72. * confirmation mail for credit card and paypal is handled \App\Module\Checkout\Subscriber\PaymentReceivedSubscriber::sendConfirmationMail
  73. *
  74. * @param OrderCreatedEvent $event
  75. *
  76. * @return void
  77. */
  78. public function sendConfirmationMail(OrderCreatedEvent $event): void
  79. {
  80. $order = $event->getOrder();
  81. $paymentType = $this->getPaymentType($order);
  82. if (in_array($paymentType, [Type::PRE_PAYMENT, Type::INVOICE])) {
  83. $this->logger->debug(sprintf(
  84. 'Sending confirmation mail for order %s with pre payment or invoice',
  85. $order->getId()
  86. ));
  87. $this->orderConfirmationNotifier->sendConfirmationMail($order);
  88. }
  89. }
  90. }