src/Controller/Front/RouletteController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Award;
  4. use App\Entity\FormRoulette;
  5. use App\Entity\Roulette;
  6. use App\Entity\UserClient;
  7. use App\Manager\UserClientManager;
  8. use DateTime;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  16. use App\Manager\RouletteManager;
  17. use App\Manager\AwardManager;
  18. use App\Manager\FormRouletteManager;
  19. use App\Service\Mail\RouletteClientMail;
  20. use App\Service\Mail\RouletteMail;
  21. /**
  22.  * @Route("/ruleta")
  23.  */
  24. class RouletteController extends AbstractController
  25. {
  26.     protected array $locals = [];
  27.     public function __construct()
  28.     {
  29.         $this->locals['menu_active'] = '';
  30.     }
  31.     /**
  32.      * @Route("/", name="roulette")
  33.      * @Template("front/default/ruleta.html.twig")
  34.      */
  35.     public function roulette(Request $requestRouletteManager $rouletteManagerAwardManager $awardManager): array
  36.     {
  37.         $this->locals['home'] = $rouletteManager->find(1);
  38.         $this->locals['awards'] = $awardManager->allValids();
  39.         $this->locals['menu_active'] = 'roulette';
  40.         $date = new DateTime();
  41.         $this->locals['current_date'] = $date->getTimestamp();
  42.         return $this->locals;
  43.     }
  44.     /**
  45.      * @Route("/generar-premio/", name="roulette_award", methods={"POST"})
  46.      * @IsGranted("ROLE_USER")
  47.      */
  48.     public function rouletteAward(Request $requestRouletteManager $rouletteManagerAwardManager $awardManagerFormRouletteManager $formRouletteManagerUserClientManager $userClientManagerRouletteMail $rouletteMailRouletteClientMail $rouletteClientMail): Response
  49.     {
  50.         /** @var UserClient $user */
  51.         $user $this->getUser();
  52.         $response = new JsonResponse([]);
  53.         $response->headers->set('X-Robots-Tag''noindex, nofollow');
  54.         $awards $awardManager->allValids();
  55.         $awardIndex $this->getRandomAward($awards);
  56.         if (is_null($awardIndex)) {
  57.             $response->setData([
  58.                 'success' => false,
  59.                 'message' => 'NingĂșn premio seleccionado',
  60.                 'award' => [],
  61.                 'form_id' => 0
  62.             ]);
  63.         }
  64.         /** @var Award $award */
  65.         $award $awards[$awardIndex];
  66.         $entity null;
  67.         if ($award->getIsFinish()) {
  68.             /** @var Roulette $config */
  69.             $config $rouletteManager->find(1);
  70.             $awardDays $config->getAwardValidityDays() ?: 0;
  71.             $userDays $config->getBlockDays() ?: 0;
  72.             $validityDate = new DateTime("now +{$awardDays} day");
  73.             $nextDate = new DateTime('first day of next month 00:00:00');
  74.             $entity $this->rewardClient($award$user$validityDate$formRouletteManager);
  75.             $user->setNextPlayOn($nextDate);
  76.             $userClientManager->save($user);
  77.             $rouletteMail->load($entity)->send();
  78.             $rouletteClientMail->load($entity)->send();
  79.         }
  80.         $response->setData([
  81.             'success' => true,
  82.             'message' => '',
  83.             'form_id' => $entity $entity->getId() : 0,
  84.             'blocked' => $user->getNextPlayOn() ? $user->getNextPlayOn()->getTimestamp() : 0,
  85.             'award' => [
  86.                 'id' => $award->getId(),
  87.                 'title' => $award->getTitle(),
  88.                 'description' => str_replace(['<p>''</p>'], ''$award->getDescription()),
  89.                 'try_again' => !$award->getIsFinish(),
  90.                 'position' => $awardIndex,
  91.             ]
  92.         ]);
  93.         return $response;
  94.     }
  95.     protected function getRandomAward($awards)
  96.     {
  97.         $totalSize array_reduce($awards, function ($carry$item) {
  98.             $carry += $item->getSize();
  99.             return $carry;
  100.         }, 0);
  101.         $number mt_rand(0$totalSize);
  102.         $selected null;
  103.         $accumulative 0;
  104.         foreach ($awards as $index => $award) {
  105.             $accumulative $accumulative $award->getSize();
  106.             if ($number <= $accumulative) {
  107.                 $selected $index;
  108.                 break;
  109.             }
  110.         }
  111.         return $selected;
  112.     }
  113.     protected function rewardClient(Award $awardUserClient $userDateTime $validityDateFormRouletteManager $formRouletteManager): FormRoulette
  114.     {
  115.         $entity $formRouletteManager->create();
  116.         $entity->setName($user->getName());
  117.         $entity->setDocument($user->getDocument());
  118.         $entity->setEmail($user->getEmail());
  119.         $entity->setPhone($user->getPhone());
  120.         $entity->setAddress($user->getAddress());
  121.         $entity->setAwardTitle($award->getTitle());
  122.         $entity->setAwardDescription($award->getMessageMailing());
  123.         $entity->setAwardImage($award->getImage());
  124.         $entity->setAwardValidity($validityDate);
  125.         $formRouletteManager->save($entity);
  126.         return $entity;
  127.     }
  128. }