src/Controller/IndexController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Announcement;
  4. use App\Entity\User;
  5. use App\Form\Type\LoginType;
  6. use App\Model\AnnouncementService;
  7. use App\Model\ContributorService;
  8. use App\Model\UserService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. class IndexController extends AbstractController
  17. {
  18.     /**
  19.      * @var UserService
  20.      */
  21.     private $userService;
  22.     /**
  23.      * @var AnnouncementService
  24.      */
  25.     private $announcementService;
  26.     public function __construct(UserService $userServiceAnnouncementService $announcementService)
  27.     {
  28.         $this->userService $userService;
  29.         $this->announcementService $announcementService;
  30.     }
  31.     /**
  32.      * @Route("", name="site_index")
  33.      *
  34.      * @param AuthenticationUtils $authenticationUtils
  35.      * @return Response
  36.      */
  37.     public function index(AuthenticationUtils $authenticationUtils): Response
  38.     {
  39.         // Login needs to be part of the Security bundle or at least moved to separate service and action
  40.         $message "";
  41.         $form $this->createForm(LoginType::class, null, [
  42.             'use_placeholder' => true
  43.         ]);
  44.         $authException $authenticationUtils->getLastAuthenticationError();
  45.         if ($authException instanceof AuthenticationException) {
  46.             $this->addFlash('notice'$authException->getMessage());
  47.         }
  48.         return $this->render('index/index.html.twig', [
  49.             'form' => !$this->getUser() ? $form->createView() : null,
  50.             'message' => $message,
  51.             'latestNews' => $this->announcementService->getAnnouncements(['status' => Announcement::STATUS_PUBLISHED], 1),
  52.             'page' => 'home',
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/contributors", name="index_contributors")
  57.      *
  58.      * @param ContributorService $contributorService
  59.      * @return Response
  60.      */
  61.     public function contributors(ContributorService $contributorService): Response
  62.     {
  63.         $contribs $contributorService->getContributors();
  64.         return $this->render('index/contributors.html.twig', [
  65.             'page' => 'home',
  66.             'contributors' => $contribs,
  67.             'highlighted' => !empty($contribs) ? $contribs[array_rand($contribs)] : null
  68.         ]);
  69.     }
  70.     /**
  71.      * @Route("/become-contributor", name="index_become_contributor")
  72.      *
  73.      * @param Request               $request
  74.      * @param ContributorService    $contributorService
  75.      * @param UrlGeneratorInterface $urlGenerator
  76.      *
  77.      * @return Response
  78.      */
  79.     public function contributor(Request $requestContributorService $contributorServiceUrlGeneratorInterface $urlGenerator): Response
  80.     {
  81.         $value = (int) $request->get('value');
  82.         $story $request->get('message''');
  83.         if ($request->isMethod(Request::METHOD_POST)) {
  84.             /** @var User $user */
  85.             $user $this->getUser();
  86.             if (!$user->isLoggedIn()) {
  87.                 $this->addFlash('notice'"You have to be logged in to become a contributor");
  88.             } elseif ($value $user->getPremium()) {
  89.                 $this->addFlash('notice''You need more <a href="' $urlGenerator->generate('account_order_credits') . '">outbreak credits</a> for this amount of days');
  90.             } elseif (empty($story)) {
  91.                 $this->addFlash('notice''Please add a message');
  92.             } elseif ($value 0) {
  93.                 $this->addFlash('notice''Invalid option');
  94.             } else {
  95.                 $this->userService->addCredits($user, -$value);
  96.                 $contributorService->addSupporter($user$value 60strip_tags($story));
  97.                 $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>');
  98.             }
  99.         }
  100.         return $this->render('index/become_contributor.html.twig', [
  101.             'page' => 'home',
  102.             'story' => $story,
  103.             'value' => $value
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/about", name="index_about")
  108.      *
  109.      * @return Response
  110.      */
  111.     public function about(): Response
  112.     {
  113.         return $this->render('index/about.html.twig', [
  114.             'page' => 'home',
  115.         ]);
  116.     }
  117.     public function mainUpdate(Request $requestContributorService $contributorService): Response
  118.     {
  119.         if ($request->get('key') == 'OutBreak!!') {
  120.             $contributorService->tickSupporters();
  121.             $contributorService->removeSupporters();
  122.             return new Response(''Response::HTTP_OK);
  123.         }
  124.         return new Response(''Response::HTTP_FORBIDDEN);
  125.     }
  126. }