<?php
namespace App\EventListener;
use App\Entity\Gos\OrderProductVariant;
use App\Entity\Gos\UserFiles;
use App\Event\OrderPartPayedEvent;
use App\Utils\Email\SendMail;
use App\Utils\VoucherCoupon;
use App\Utils\Order\GiftNotificationUtils;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderPartPayedListener implements EventSubscriberInterface
{
private $entityManager;
private $giftNotificationUtils;
private $couponService;
private $mailer;
public function __construct(
EntityManagerInterface $entityManager,
GiftNotificationUtils $giftNotificationUtils,
SendMail $mailer,
VoucherCoupon $couponService
)
{
$this->entityManager = $entityManager;
$this->giftNotificationUtils = $giftNotificationUtils;
$this->couponService = $couponService;
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [
OrderPartPayedEvent::NAME => [
['sendNotifyEmail', 10],
['giveAccessToFile', 5],
['sendGifts', 0],
],
];
}
public function sendNotifyEmail(OrderPartPayedEvent $orderPayedEvent): void
{
$orderPart = $orderPayedEvent->getOrderPart();
$user = $orderPart->getOrders()->getUser();
$this->mailer->sendMail(
'order_online_payment_confirm',
$user->getEmailCanonical(),
[
'user_first_name' => $user->getFirstName(),
'user_last_name' => $user->getLastName(),
'user_email' => $user->getEmail(),
'ordTran' => $orderPart->getOrdTran(),
],
$orderPart->getOrders()->getPortalSettings()->getHash(),
true
);
}
public function sendGifts(OrderPartPayedEvent $orderPayedEvent): void
{
$orderPart = $orderPayedEvent->getOrderPart();
if ($orderPart->getOrderProductVariantsToGift()->isEmpty())
{
return;
}
/** @var OrderProductVariant $orderProductVariants */
foreach ($orderPart->getOrderProductVariantsToGift() as $orderProductVariants)
{
if (null !== $gift = $orderProductVariants->getGift())
{
if ($gift->getGiftDiscountCode() === null)
{
$this->couponService->generateGiftVoucherCoupon($orderProductVariants);
}
if (
$gift->getRecipientEmail()
&& (
$gift->getSendAt() === null
|| $gift->getSendAt()->format('Y-m-d') <= (new \DateTime())->format('Y-m-d')
)
)
{
$this->giftNotificationUtils->send($gift);
}
}
}
}
public function giveAccessToFile(OrderPartPayedEvent $orderPayedEvent): void
{
$order = $orderPayedEvent->getOrderPart()->getOrders();
$userFiles = $this->entityManager->getRepository(UserFiles::class)->findBy(['orders' => $order]);
if (empty($userFiles))
{
return;
}
foreach ($userFiles as $userFile)
{
$userFile->setHasAccess(true);
}
$this->entityManager->flush();
}
}