<?php
namespace App\EventSubscriber;
use App\Entity\Point;
use App\Entity\Roulette;
use App\Entity\User;
use App\Entity\UserMovePoint;
use App\Events\PurchaseEvent;
use App\Repository\PointRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PurchaseSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $em;
private PointRepository $pointRepository;
public function __construct(
EntityManagerInterface $em,
PointRepository $pointRepository
) {
$this->em = $em;
$this->pointRepository = $pointRepository;
}
public static function getSubscribedEvents(): array
{
return [
PurchaseEvent::PURCHASED => 'onPurchased',
];
}
public function onPurchased(PurchaseEvent $event): void
{
$purchase = $event->getPurchase();
$user = $purchase->getUser();
$point = $purchase->getPoint();
if (boolval(preg_match('/^PACK_/', $point->getAlias(), $matches))) {
$this->setPack($user, $point);
}
if (boolval(preg_match('/^RECURRENT_/', $point->getAlias(), $matches))) {
$this->setRecurrent($user, $point);
}
}
private function setPack(User $user, Point $point): void
{
$ump = new UserMovePoint($user);
$ump->setPoint($point);
$this->em->persist($ump);
$this->em->flush();
}
private function setRecurrent(User $user, Point $point): void
{
$recurrent_start = new \DateTime('now');
$recurrent_finish = null;
$total_months = 0;
switch ($point->getAlias()) {
case 'RECURRENT_BASIC_1M':
$total_months = 1;
break;
case 'RECURRENT_STAR_3M':
$total_months = 3;
break;
case 'RECURRENT_GOLD_6M':
$total_months = 6;
break;
case 'RECURRENT_PREMIUM_12M':
$total_months = 12;
break;
}
$recurrent_finish = new \DateTime(sprintf('+%d month', $total_months));
$total_days = $recurrent_start->diff($recurrent_finish)->days;
$total_weeks = (int)($total_days / 7);
$user->setRecurrentStartDate($recurrent_start);
$user->setRecurrentFinishDate($recurrent_finish);
$user->setIsRecurrent(true);
$this->em->persist($user);
$this->em->flush();
/*
for ($month = 0; $month < $total_months; $month++) {
$ump = new UserMovePoint($user);
//$ump->setPoint($point);
$ump->setSubject(sprintf('%s: MONTH %d', $point->getAlias(), $month));
$ump->setQtyIce($point->getQtyIce());
$ump->setQtySuperice($point->getQtySuperice());
$ump->setIsUnlimitedRewind($point->getIsUnlimitedRewind());
$ump->setIsUnlimitedLike($point->getIsUnlimitedLike());
if ($month !== 0) {
$ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
$ump->setStartDate(new \DateTime(sprintf('+%d month', $month)));
}
$this->em->persist($ump);
}
$this->em->flush();
*/
for ($week = 0; $week < $total_weeks; $week++) {
$ump = new UserMovePoint($user);
//$ump->setPoint($point);
$ump->setSubject(sprintf('%s: WEEK %d', $point->getAlias(), $week));
$ump->setQtyIce($point->getQtyIce());
$ump->setQtySuperice($point->getQtySuperice());
$ump->setQtyFeatured($point->getQtyFeatured());
$ump->setIsUnlimitedRewind($point->getIsUnlimitedRewind());
$ump->setIsUnlimitedLike($point->getIsUnlimitedLike());
if ($week !== 0) {
$ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
$ump->setStartDate(new \DateTime(sprintf('+%d week', $week)));
}
$this->em->persist($ump);
}
$this->em->flush();
/*
for ($day = 0; $day < $total_days; $day++) {
$ump = new UserMovePoint($user);
//$ump->setPoint($point);
$ump->setSubject(sprintf('%s: DAY %d', $point->getAlias(), $day));
$ump->setQtySuperice($point->getQtySuperice());
if ($day !== 0) {
$ump->setStatus(UserMovePoint::STATUS_UNPUBLISHED);
$ump->setStartDate(new \DateTime(sprintf('+%d day', $day)));
}
$this->em->persist($ump);
}
$this->em->flush();
*/
}
}