src/EventListener/OrderPartPayedListener.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Gos\OrderProductVariant;
  4. use App\Entity\Gos\UserFiles;
  5. use App\Event\OrderPartPayedEvent;
  6. use App\Utils\Email\SendMail;
  7. use App\Utils\VoucherCoupon;
  8. use App\Utils\Order\GiftNotificationUtils;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class OrderPartPayedListener implements EventSubscriberInterface
  12. {
  13.     private $entityManager;
  14.     private $giftNotificationUtils;
  15.     private $couponService;
  16.     private $mailer;
  17.     public function __construct(
  18.         EntityManagerInterface $entityManager,
  19.         GiftNotificationUtils  $giftNotificationUtils,
  20.         SendMail               $mailer,
  21.         VoucherCoupon          $couponService
  22.     )
  23.     {
  24.         $this->entityManager         $entityManager;
  25.         $this->giftNotificationUtils $giftNotificationUtils;
  26.         $this->couponService         $couponService;
  27.         $this->mailer                $mailer;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             OrderPartPayedEvent::NAME => [
  33.                 ['sendNotifyEmail'10],
  34.                 ['giveAccessToFile'5],
  35.                 ['sendGifts'0],
  36.             ],
  37.         ];
  38.     }
  39.     public function sendNotifyEmail(OrderPartPayedEvent $orderPayedEvent): void
  40.     {
  41.         $orderPart $orderPayedEvent->getOrderPart();
  42.         $user      $orderPart->getOrders()->getUser();
  43.         $this->mailer->sendMail(
  44.             'order_online_payment_confirm',
  45.             $user->getEmailCanonical(),
  46.             [
  47.                 'user_first_name' => $user->getFirstName(),
  48.                 'user_last_name'  => $user->getLastName(),
  49.                 'user_email'      => $user->getEmail(),
  50.                 'ordTran'         => $orderPart->getOrdTran(),
  51.             ],
  52.             $orderPart->getOrders()->getPortalSettings()->getHash(),
  53.             true
  54.         );
  55.     }
  56.     public function sendGifts(OrderPartPayedEvent $orderPayedEvent): void
  57.     {
  58.         $orderPart $orderPayedEvent->getOrderPart();
  59.         if ($orderPart->getOrderProductVariantsToGift()->isEmpty())
  60.         {
  61.             return;
  62.         }
  63.         /** @var OrderProductVariant $orderProductVariants */
  64.         foreach ($orderPart->getOrderProductVariantsToGift() as $orderProductVariants)
  65.         {
  66.             if (null !== $gift $orderProductVariants->getGift())
  67.             {
  68.                 if ($gift->getGiftDiscountCode() === null)
  69.                 {
  70.                     $this->couponService->generateGiftVoucherCoupon($orderProductVariants);
  71.                 }
  72.                 if (
  73.                     $gift->getRecipientEmail()
  74.                     && (
  75.                         $gift->getSendAt() === null
  76.                         || $gift->getSendAt()->format('Y-m-d') <= (new \DateTime())->format('Y-m-d')
  77.                     )
  78.                 )
  79.                 {
  80.                     $this->giftNotificationUtils->send($gift);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.     public function giveAccessToFile(OrderPartPayedEvent $orderPayedEvent): void
  86.     {
  87.         $order     $orderPayedEvent->getOrderPart()->getOrders();
  88.         $userFiles $this->entityManager->getRepository(UserFiles::class)->findBy(['orders' => $order]);
  89.         if (empty($userFiles))
  90.         {
  91.             return;
  92.         }
  93.         foreach ($userFiles as $userFile)
  94.         {
  95.             $userFile->setHasAccess(true);
  96.         }
  97.         $this->entityManager->flush();
  98.     }
  99. }