<?php
namespace App\Event\FreeFortuneTeller;
use App\Entity\Coupon\Coupon;
use App\Entity\FreeFortuneTellerScenario\FreeFortuneTellerScenario;
use App\Entity\User\User;
use App\Services\Coupon\CouponGenerator;
use App\Utils\ApiMessage;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class FreeFortuneTellerSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var
*/
private $engine;
/**
* @var
*/
private $couponGenerator;
public function __construct(EntityManagerInterface $em, Environment $engine, CouponGenerator $couponGenerator)
{
$this->em = $em;
$this->engine = $engine;
$this->couponGenerator = $couponGenerator;
}
public static function getSubscribedEvents()
{
return [
FreeFortuneTellerEvent::NAME => 'freeFortuneTeller'
];
}
public function freeFortuneTeller(FreeFortuneTellerEvent $event)
{
$status_scenario = $this->em->getRepository(FreeFortuneTellerScenario::class)->find(1);
if ($status_scenario->getIsActive() == 1) {
$qb = $this->em->createQueryBuilder()
->select('o')
->from('App\Entity\Order\Order', 'o')
->where('o.ownerEmail = :owner_email', 'o.isPaid = 1')
->setParameter('owner_email', $event->getEmail())
->andWhere('o.updatedAt >= :date_updated')
->setParameter('date_updated', ($status_scenario->getUpdatedAt() == null) ? $status_scenario->getCreatedAt() : $status_scenario->getUpdatedAt())
->getQuery();
$query = $qb->getResult(Query::HYDRATE_ARRAY);
if (count($query) == 5) {
$coupon = $this->couponGenerator->generateCoupon();
$user = $this->em->getRepository(User::class)->findOneBy([
'email' => $event->getEmail()
]);
$coupon_db = new Coupon();
$coupon_db->setUuid($coupon);
$coupon_db->setDiscountAmount('2799');
$coupon_db->setIsUsed(0);
$coupon_db->setOwner($user);
$this->em->persist($coupon_db);
$this->em->flush();
$content = $this->engine->render('email/free-fortune-telling-coupon-code.html.twig', [
'userName' => sprintf('%s %s', ($user->firstName() ? $user->firstName() : "Guest"), ($user->lastName() ? $user->lastName() : "")),
'couponCode' => $coupon,
]);
// send email with message 34
$apiMessage = new ApiMessage();
$emailValues = array(
'user' => (!$user->firstName() ? "Guest" : $user->firstName()),
'userLastName' => (!$user->lastName() ? "Guest" : $user->lastName()),
'email' => $event->getEmail(),
'content' => $content,
'fromName' => "FalveFal",
'fromEmail' => "[email protected]",
'reply' => "[email protected]",
'subject' => "5 falını tamamladın ve 1 bedava fal kazandin! Hemen kullanmak için tıkla.",
);
// set_time_limit(60000);
$apiMessage->mail($emailValues);
}
}
}
}