<?php
namespace App\Controller\Front;
use App\Entity\Award;
use App\Entity\FormRoulette;
use App\Entity\Roulette;
use App\Entity\UserClient;
use App\Manager\UserClientManager;
use DateTime;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Manager\RouletteManager;
use App\Manager\AwardManager;
use App\Manager\FormRouletteManager;
use App\Service\Mail\RouletteClientMail;
use App\Service\Mail\RouletteMail;
/**
* @Route("/ruleta")
*/
class RouletteController extends AbstractController
{
protected array $locals = [];
public function __construct()
{
$this->locals['menu_active'] = '';
}
/**
* @Route("/", name="roulette")
* @Template("front/default/ruleta.html.twig")
*/
public function roulette(Request $request, RouletteManager $rouletteManager, AwardManager $awardManager): array
{
$this->locals['home'] = $rouletteManager->find(1);
$this->locals['awards'] = $awardManager->allValids();
$this->locals['menu_active'] = 'roulette';
$date = new DateTime();
$this->locals['current_date'] = $date->getTimestamp();
return $this->locals;
}
/**
* @Route("/generar-premio/", name="roulette_award", methods={"POST"})
* @IsGranted("ROLE_USER")
*/
public function rouletteAward(Request $request, RouletteManager $rouletteManager, AwardManager $awardManager, FormRouletteManager $formRouletteManager, UserClientManager $userClientManager, RouletteMail $rouletteMail, RouletteClientMail $rouletteClientMail): Response
{
/** @var UserClient $user */
$user = $this->getUser();
$response = new JsonResponse([]);
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
$awards = $awardManager->allValids();
$awardIndex = $this->getRandomAward($awards);
if (is_null($awardIndex)) {
$response->setData([
'success' => false,
'message' => 'NingĂșn premio seleccionado',
'award' => [],
'form_id' => 0
]);
}
/** @var Award $award */
$award = $awards[$awardIndex];
$entity = null;
if ($award->getIsFinish()) {
/** @var Roulette $config */
$config = $rouletteManager->find(1);
$awardDays = $config->getAwardValidityDays() ?: 0;
$userDays = $config->getBlockDays() ?: 0;
$validityDate = new DateTime("now +{$awardDays} day");
$nextDate = new DateTime('first day of next month 00:00:00');
$entity = $this->rewardClient($award, $user, $validityDate, $formRouletteManager);
$user->setNextPlayOn($nextDate);
$userClientManager->save($user);
$rouletteMail->load($entity)->send();
$rouletteClientMail->load($entity)->send();
}
$response->setData([
'success' => true,
'message' => '',
'form_id' => $entity ? $entity->getId() : 0,
'blocked' => $user->getNextPlayOn() ? $user->getNextPlayOn()->getTimestamp() : 0,
'award' => [
'id' => $award->getId(),
'title' => $award->getTitle(),
'description' => str_replace(['<p>', '</p>'], '', $award->getDescription()),
'try_again' => !$award->getIsFinish(),
'position' => $awardIndex,
]
]);
return $response;
}
protected function getRandomAward($awards)
{
$totalSize = array_reduce($awards, function ($carry, $item) {
$carry += $item->getSize();
return $carry;
}, 0);
$number = mt_rand(0, $totalSize);
$selected = null;
$accumulative = 0;
foreach ($awards as $index => $award) {
$accumulative = $accumulative + $award->getSize();
if ($number <= $accumulative) {
$selected = $index;
break;
}
}
return $selected;
}
protected function rewardClient(Award $award, UserClient $user, DateTime $validityDate, FormRouletteManager $formRouletteManager): FormRoulette
{
$entity = $formRouletteManager->create();
$entity->setName($user->getName());
$entity->setDocument($user->getDocument());
$entity->setEmail($user->getEmail());
$entity->setPhone($user->getPhone());
$entity->setAddress($user->getAddress());
$entity->setAwardTitle($award->getTitle());
$entity->setAwardDescription($award->getMessageMailing());
$entity->setAwardImage($award->getImage());
$entity->setAwardValidity($validityDate);
$formRouletteManager->save($entity);
return $entity;
}
}