<?php
namespace App\Controller;
use App\Entity\Announcement;
use App\Entity\User;
use App\Form\Type\LoginType;
use App\Model\AnnouncementService;
use App\Model\ContributorService;
use App\Model\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class IndexController extends AbstractController
{
/**
* @var UserService
*/
private $userService;
/**
* @var AnnouncementService
*/
private $announcementService;
public function __construct(UserService $userService, AnnouncementService $announcementService)
{
$this->userService = $userService;
$this->announcementService = $announcementService;
}
/**
* @Route("", name="site_index")
*
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function index(AuthenticationUtils $authenticationUtils): Response
{
// Login needs to be part of the Security bundle or at least moved to separate service and action
$message = "";
$form = $this->createForm(LoginType::class, null, [
'use_placeholder' => true
]);
$authException = $authenticationUtils->getLastAuthenticationError();
if ($authException instanceof AuthenticationException) {
$this->addFlash('notice', $authException->getMessage());
}
return $this->render('index/index.html.twig', [
'form' => !$this->getUser() ? $form->createView() : null,
'message' => $message,
'latestNews' => $this->announcementService->getAnnouncements(['status' => Announcement::STATUS_PUBLISHED], 1),
'page' => 'home',
]);
}
/**
* @Route("/contributors", name="index_contributors")
*
* @param ContributorService $contributorService
* @return Response
*/
public function contributors(ContributorService $contributorService): Response
{
$contribs = $contributorService->getContributors();
return $this->render('index/contributors.html.twig', [
'page' => 'home',
'contributors' => $contribs,
'highlighted' => !empty($contribs) ? $contribs[array_rand($contribs)] : null
]);
}
/**
* @Route("/become-contributor", name="index_become_contributor")
*
* @param Request $request
* @param ContributorService $contributorService
* @param UrlGeneratorInterface $urlGenerator
*
* @return Response
*/
public function contributor(Request $request, ContributorService $contributorService, UrlGeneratorInterface $urlGenerator): Response
{
$value = (int) $request->get('value');
$story = $request->get('message', '');
if ($request->isMethod(Request::METHOD_POST)) {
/** @var User $user */
$user = $this->getUser();
if (!$user->isLoggedIn()) {
$this->addFlash('notice', "You have to be logged in to become a contributor");
} elseif ($value > $user->getPremium()) {
$this->addFlash('notice', 'You need more <a href="' . $urlGenerator->generate('account_order_credits') . '">outbreak credits</a> for this amount of days');
} elseif (empty($story)) {
$this->addFlash('notice', 'Please add a message');
} elseif ($value < 0) {
$this->addFlash('notice', 'Invalid option');
} else {
$this->userService->addCredits($user, -1 * $value);
$contributorService->addSupporter($user, $value * 60, strip_tags($story));
$this->addFlash('notice', 'Thank you for becoming an outbreak supporter, you name is noted on our <a href="' . $urlGenerator->generate('index_contributors') . '">contributors page</a>');
}
}
return $this->render('index/become_contributor.html.twig', [
'page' => 'home',
'story' => $story,
'value' => $value
]);
}
/**
* @Route("/about", name="index_about")
*
* @return Response
*/
public function about(): Response
{
return $this->render('index/about.html.twig', [
'page' => 'home',
]);
}
public function mainUpdate(Request $request, ContributorService $contributorService): Response
{
if ($request->get('key') == 'OutBreak!!') {
$contributorService->tickSupporters();
$contributorService->removeSupporters();
return new Response('', Response::HTTP_OK);
}
return new Response('', Response::HTTP_FORBIDDEN);
}
}