src/Controller/HomeController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Cache\BlogArticleCache;
  4. use App\Entity\User;
  5. use App\Entity\BlogArticle;
  6. use App\EntityManager\BlogArticleManager;
  7. use App\EntityManager\ForumManager;
  8. use App\EntityManager\UserManager;
  9. use App\Service\CounterService;
  10. use App\Service\GitlabConnector;
  11. use Psr\SimpleCache\CacheInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Filesystem\Exception\FileNotFoundException;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  23. class HomeController extends AbstractController
  24. {
  25.     protected $userManager;
  26.     protected $gitlabConnector;
  27.     protected $cache;
  28.     public function __construct(
  29.         UserManager $userManager,
  30.         GitlabConnector $gitlabConnector,
  31.         CacheInterface $cache
  32.     ) {
  33.         $this->userManager     $userManager;
  34.         $this->gitlabConnector $gitlabConnector;
  35.         $this->cache           $cache;
  36.     }
  37.     /**
  38.      * @Route("/", name="_home")
  39.      * @Template()
  40.      */
  41.     public function index(BlogArticleManager $articleManagerCounterService $counterService)
  42.     {
  43.         $articlesData $this->cache->get(BlogArticleCache::HOME_ARTICLES_KEY);
  44.         if ($articlesData == null) {
  45.             $articles     $this->getDoctrine()->getRepository(BlogArticle::class)->getList(110);
  46.             $articlesData array_map(
  47.                 function (BlogArticle $article) use ($articleManager) {
  48.                     return [
  49.                         'article'   => $article,
  50.                         'bannerUri' => $articleManager->getBannerImage($article),
  51.                     ];
  52.                 },
  53.                 $articles
  54.             );
  55.             $this->cache->set(BlogArticleCache::HOME_ARTICLES_KEY$articlesData);
  56.         }
  57.         return [
  58.             'articlesData' => $articlesData,
  59.             'counters' => $counterService->getCounters(),
  60.         ];
  61.     }
  62.     /**
  63.      * @Route("/lang/{locale}", name="_change_locale")
  64.      * @Template()
  65.      */
  66.     public function changeLocale(
  67.         Request $request,
  68.         $locale
  69.     ) {
  70.         $this->userManager->updateLocale($locale);
  71.         return $this->redirect($request->headers->get('referer'$this->generateUrl('_home')));
  72.     }
  73.     /**
  74.      * @Route("/report_bug", name="_report_a_bug")
  75.      * @Method({"POST"})
  76.      */
  77.     public
  78.     function reportBug(
  79.         Request $request
  80.     ): JsonResponse {
  81.         if (!$request->isXmlHttpRequest()) {
  82.             throw new AccessDeniedException();
  83.         }
  84.         $comment    $request->request->get('comment');
  85.         $screenshot $request->request->get('screenshot');
  86.         if (is_null($comment)) {
  87.             throw new \Exception("The bug report has no comment.");
  88.         }
  89.         $extras = [
  90.             'date'     => $request->request->get('timestamp'),
  91.             'uri'      => $request->request->get('uri'),
  92.             '_route'   => $request->request->get('route'),
  93.             'viewport' => $request->request->get('viewport'),
  94.         ];
  95.         if (!is_null($screenshot)) {
  96.             $filename sprintf('%s_%s.png'date('Y-m-d'), md5(uniqid(rand(), true)));
  97.             $filepath sprintf(
  98.                 '%s/data/bug_report/%s',
  99.                 $this->getParameter('kernel.project_dir'),
  100.                 $filename
  101.             );
  102.             $screenshot explode(','$screenshot);
  103.             if (is_array($screenshot) &&
  104.                 isset($screenshot[1]) &&
  105.                 false !== file_put_contents($filepathbase64_decode($screenshot[1]))) {
  106.                 $extras['screenshot'] = $this->generateUrl(
  107.                     '_serve_bug_screenshot',
  108.                     ['filename' => $filename],
  109.                     UrlGeneratorInterface::ABSOLUTE_URL
  110.                 );
  111.             }
  112.         }
  113.         $response $this->gitlabConnector->reportBug($comment$extras);
  114.         if (in_array($response->getStatusCode(), [200201])) {
  115.             return new JsonResponse('success');
  116.         }
  117.         return new JsonResponse('error'400);
  118.     }
  119.     /**
  120.      * @Route("/report_bug/screenshot/{filename}", name="_serve_bug_screenshot")
  121.      * @IsGranted(User::ROLE_SUPER_ADMIN)
  122.      */
  123.     public
  124.     function serveBugScreenshot(
  125.         string $filename
  126.     ) {
  127.         $filepath sprintf(
  128.             '%s/data/bug_report/%s',
  129.             $this->getParameter('kernel.project_dir'),
  130.             $filename
  131.         );
  132.         if (file_exists($filepath)) {
  133.             return $this->file($filepathnullResponseHeaderBag::DISPOSITION_INLINE);
  134.         }
  135.         throw new FileNotFoundException();
  136.     }
  137.     /**
  138.      * @Route("/under_construction", name="_under_construction")
  139.      * @Template()
  140.      */
  141.     public
  142.     function underConstruction()
  143.     {
  144.         return [];
  145.     }
  146. }