src/EventSubscriber/PurchaseSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Point;
  4. use App\Entity\Roulette;
  5. use App\Entity\User;
  6. use App\Entity\UserMovePoint;
  7. use App\Events\PurchaseEvent;
  8. use App\Repository\PointRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class PurchaseSubscriber implements EventSubscriberInterface
  12. {
  13.     private EntityManagerInterface $em;
  14.     private PointRepository $pointRepository;
  15.     public function __construct(
  16.         EntityManagerInterface $em,
  17.         PointRepository $pointRepository
  18.     ) {
  19.         $this->em $em;
  20.         $this->pointRepository $pointRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             PurchaseEvent::PURCHASED => 'onPurchased',
  26.         ];
  27.     }
  28.     public function onPurchased(PurchaseEvent $event): void
  29.     {
  30.         $purchase $event->getPurchase();
  31.         $user $purchase->getUser();
  32.         $point $purchase->getPoint();
  33.         if (boolval(preg_match('/^PACK_/'$point->getAlias(), $matches))) {
  34.             $this->setPack($user$point);
  35.         }
  36.         if (boolval(preg_match('/^RECURRENT_/'$point->getAlias(), $matches))) {
  37.             $this->setRecurrent($user$point);
  38.         }
  39.     }
  40.     private function setPack(User $userPoint $point): void
  41.     {
  42.         $ump = new UserMovePoint($user);
  43.         $ump->setPoint($point);
  44.         $this->em->persist($ump);
  45.         $this->em->flush();
  46.     }
  47.     private function setRecurrent(User $userPoint $point): void
  48.     {
  49.         $recurrent_start = new \DateTime('now');
  50.         $recurrent_finish null;
  51.         $total_months 0;
  52.         switch ($point->getAlias()) {
  53.             case 'RECURRENT_BASIC_1M':
  54.                 $total_months 1;
  55.                 break;
  56.             case 'RECURRENT_STAR_3M':
  57.                 $total_months 3;
  58.                 break;
  59.             case 'RECURRENT_GOLD_6M':
  60.                 $total_months 6;
  61.                 break;
  62.             case 'RECURRENT_PREMIUM_12M':
  63.                 $total_months 12;
  64.                 break;
  65.         }
  66.         $recurrent_finish = new \DateTime(sprintf('+%d month'$total_months));
  67.         $total_days $recurrent_start->diff($recurrent_finish)->days;
  68.         $total_weeks = (int)($total_days 7);
  69.         $user->setRecurrentStartDate($recurrent_start);
  70.         $user->setRecurrentFinishDate($recurrent_finish);
  71.         $user->setIsRecurrent(true);
  72.         $this->em->persist($user);
  73.         $this->em->flush();
  74.         /*
  75.         for ($month = 0; $month < $total_months; $month++) {
  76.             $ump = new UserMovePoint($user);
  77.             //$ump->setPoint($point);
  78.             $ump->setSubject(sprintf('%s: MONTH %d', $point->getAlias(), $month));
  79.             $ump->setQtyIce($point->getQtyIce());
  80.             $ump->setQtySuperice($point->getQtySuperice());
  81.             $ump->setIsUnlimitedRewind($point->getIsUnlimitedRewind());
  82.             $ump->setIsUnlimitedLike($point->getIsUnlimitedLike());
  83.             if ($month !== 0) {
  84.                 $ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
  85.                 $ump->setStartDate(new \DateTime(sprintf('+%d month', $month)));
  86.             }
  87.             $this->em->persist($ump);
  88.         }
  89.         $this->em->flush();
  90.         */
  91.         for ($week 0$week $total_weeks$week++) {
  92.             $ump = new UserMovePoint($user);
  93.             //$ump->setPoint($point);
  94.             $ump->setSubject(sprintf('%s: WEEK %d'$point->getAlias(), $week));
  95.             $ump->setQtyIce($point->getQtyIce());
  96.             $ump->setQtySuperice($point->getQtySuperice());
  97.             $ump->setQtyFeatured($point->getQtyFeatured());
  98.             $ump->setIsUnlimitedRewind($point->getIsUnlimitedRewind());
  99.             $ump->setIsUnlimitedLike($point->getIsUnlimitedLike());
  100.             if ($week !== 0) {
  101.                 $ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
  102.                 $ump->setStartDate(new \DateTime(sprintf('+%d week'$week)));
  103.             }
  104.             $this->em->persist($ump);
  105.         }
  106.         $this->em->flush();
  107.         /*
  108.         for ($day = 0; $day < $total_days; $day++) {
  109.             $ump = new UserMovePoint($user);
  110.             //$ump->setPoint($point);
  111.             $ump->setSubject(sprintf('%s: DAY %d', $point->getAlias(), $day));
  112.             $ump->setQtySuperice($point->getQtySuperice());
  113.             if ($day !== 0) {
  114.                 $ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
  115.                 $ump->setStartDate(new \DateTime(sprintf('+%d day', $day)));
  116.             }
  117.             $this->em->persist($ump);
  118.         }
  119.         $this->em->flush();
  120.         */
  121.     }
  122. }