<?php
namespace App\EventListener;
use App\Service\MailerService;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\PreOrder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class PreOrderCreatedListener implements EventSubscriberInterface
{
private $mailerService;
private EntityManagerInterface $entityManager;
public function __construct(MailerService $mailerService, EntityManagerInterface $entityManager)
{
$this->mailerService = $mailerService;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['sendPreOrderEmail', EventPriorities::POST_WRITE],
];
}
public function sendPreOrderEmail(ViewEvent $event)
{
$pre_order = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$pre_order instanceof PreOrder || Request::METHOD_POST !== $method) {
return;
}
$newPreOrder = $this->entityManager->getRepository(PreOrder::class)->find($pre_order->getId());
// echo '<pre>';
// print_r($newPreOrder->getClient()->getEmail()); die;
// print_r($pre_order->getId()); die;
// file_put_contents('Emai.log', print_r($pre_order->getClient(), true) . "\n", FILE_APPEND);
// $userEmail = $newPreOrder->getClient()->getEmail(); // Отримати email з вашого об'єкта користувача
// $subject = 'KRAMAR LTD замовлення #' . $pre_order->getId();
// $body = 'Вітаємо, замовлення успішно оформлено. Чекайте відповідь менеджера ';
// $this->mailerService->sendRegistrationEmail($userEmail, $subject, $body);
}
}