src/Controller/NewsController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\NewsArticle;
  4. use App\Entity\User;
  5. use App\Repository\NewsArticleRepository;
  6. use App\Services\UserService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/news",
  15.  *     host="{web_subdomain}.fwthorpe.{web_tld}",
  16.  *     defaults={"web_subdomain"="%web_subdomain%","web_tld"="%web_tld%"},
  17.  *     requirements={"web_subdomain"="%web_subdomain%|","web_tld"="%web_tld%"}
  18.  *     )
  19.  */
  20. class NewsController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="news_route")
  24.      * @param NewsArticleRepository $newsArticleRepository
  25.      * @param UserService           $userService
  26.      * @return Response
  27.      */
  28.     public function indexAction(NewsArticleRepository $newsArticleRepositoryUserService $userService): Response
  29.     {
  30.         $newsArticles $newsArticleRepository->findBy(['archived' => 0'published' => 1], ['datePosted' => 'DESC']);
  31.         $isAdminUser $userService->getIsAdminUser($this->getUser());
  32.         return $this->render('about/latest-news.html.twig', ['news_articles' => $newsArticles'isAdminUser' => $isAdminUser]);
  33.     }
  34.     /**
  35.      * @Route("/article/{id}", name="news_article_view_route")
  36.      */
  37.     public function viewAction(NewsArticle $newsArticle): Response
  38.     {
  39.         return $this->render('about/news-article.html.twig', ['news_article' => $newsArticle]);
  40.     }
  41.     /**
  42.      * @Route("/image/{size}/{filename}", name="news_article_image_route", methods={"GET"})
  43.      */
  44.     public function newsArticleImageAction(Request $requeststring $sizestring $filename): BinaryFileResponse
  45.     {
  46.         $extension pathinfo($filenamePATHINFO_EXTENSION);
  47.         if (!$extension) {
  48.             throw $this->createNotFoundException('No image extension provided');
  49.         }
  50.         $file $_ENV['NEWS_ARTICLE_IMAGE_DIR'] . '/' $size '/' $filename;
  51.         $filesystem = new Filesystem();
  52.         if (!$filesystem->exists($file)) {
  53.             throw $this->createNotFoundException('Image does not exist');
  54.         }
  55.         return new BinaryFileResponse($file);
  56.     }
  57.     /**
  58.      * @Route("/thumbnail/{size}/{filename}", name="news_article_thumbnail_route", methods={"GET"})
  59.      */
  60.     public function newsArticleThumbnailAction(Request $requeststring $sizestring $filename): BinaryFileResponse
  61.     {
  62.         $extension pathinfo($filenamePATHINFO_EXTENSION);
  63.         if (!$extension) {
  64.             throw $this->createNotFoundException('No image extension provided');
  65.         }
  66.         $file $_ENV['NEWS_ARTICLE_THUMBNAIL_DIR'] . '/' $size '/' $filename;
  67.         $filesystem = new Filesystem();
  68.         if (!$filesystem->exists($file)) {
  69.             throw $this->createNotFoundException('Image does not exist');
  70.         }
  71.         return new BinaryFileResponse($file);
  72.     }
  73. }