src/Event/FreeFortuneTeller/FreeFortuneTellerSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Event\FreeFortuneTeller;
  3. use App\Entity\Coupon\Coupon;
  4. use App\Entity\FreeFortuneTellerScenario\FreeFortuneTellerScenario;
  5. use App\Entity\User\User;
  6. use App\Services\Coupon\CouponGenerator;
  7. use App\Utils\ApiMessage;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\ORM\Query;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Twig\Environment;
  12. class FreeFortuneTellerSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var EntityManagerInterface
  16.      */
  17.     private $em;
  18.     /**
  19.      * @var
  20.      */
  21.     private $engine;
  22.     /**
  23.      * @var
  24.      */
  25.     private $couponGenerator;
  26.     public function __construct(EntityManagerInterface $emEnvironment $engineCouponGenerator $couponGenerator)
  27.     {
  28.         $this->em $em;
  29.         $this->engine $engine;
  30.         $this->couponGenerator $couponGenerator;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             FreeFortuneTellerEvent::NAME => 'freeFortuneTeller'
  36.         ];
  37.     }
  38.     public function freeFortuneTeller(FreeFortuneTellerEvent $event)
  39.     {
  40.         $status_scenario $this->em->getRepository(FreeFortuneTellerScenario::class)->find(1);
  41.         if ($status_scenario->getIsActive() == 1) {
  42.             $qb $this->em->createQueryBuilder()
  43.                 ->select('o')
  44.                 ->from('App\Entity\Order\Order''o')
  45.                 ->where('o.ownerEmail = :owner_email''o.isPaid = 1')
  46.                 ->setParameter('owner_email'$event->getEmail())
  47.                 ->andWhere('o.updatedAt >= :date_updated')
  48.                 ->setParameter('date_updated', ($status_scenario->getUpdatedAt() == null) ? $status_scenario->getCreatedAt() : $status_scenario->getUpdatedAt())
  49.                 ->getQuery();
  50.             $query $qb->getResult(Query::HYDRATE_ARRAY);
  51.             if (count($query) == 5) {
  52.                     $coupon $this->couponGenerator->generateCoupon();
  53.                     $user $this->em->getRepository(User::class)->findOneBy([
  54.                         'email' => $event->getEmail()
  55.                     ]);
  56.                     $coupon_db = new Coupon();
  57.                     $coupon_db->setUuid($coupon);
  58.                     $coupon_db->setDiscountAmount('2799');
  59.                     $coupon_db->setIsUsed(0);
  60.                     $coupon_db->setOwner($user);
  61.                     $this->em->persist($coupon_db);
  62.                     $this->em->flush();
  63.                     $content $this->engine->render('email/free-fortune-telling-coupon-code.html.twig', [
  64.                         'userName' => sprintf('%s %s', ($user->firstName() ? $user->firstName() : "Guest"), ($user->lastName() ? $user->lastName() : "")),
  65.                         'couponCode' => $coupon,
  66.                     ]);
  67.                     // send email with message 34
  68.                     $apiMessage = new ApiMessage();
  69.                     $emailValues = array(
  70.                         'user' => (!$user->firstName() ? "Guest" $user->firstName()),
  71.                         'userLastName' => (!$user->lastName() ? "Guest" $user->lastName()),
  72.                         'email' => $event->getEmail(),
  73.                         'content' => $content,
  74.                         'fromName' => "FalveFal",
  75.                         'fromEmail' => "[email protected]",
  76.                         'reply' => "[email protected]",
  77.                         'subject' => "5 falını tamamladın ve 1 bedava fal kazandin! Hemen kullanmak için tıkla.",
  78.                     );
  79.                     // set_time_limit(60000);
  80.                     $apiMessage->mail($emailValues);
  81.             }
  82.         }
  83.     }
  84. }