1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys BlogBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\BlogBundle\Controller
; 
  14 use Symfony\Component\HttpFoundation\Request
; 
  15 use Symfony\Component\HttpFoundation\Response
; 
  17 use Rapsys\BlogBundle\Entity\Article
; 
  18 use Rapsys\BlogBundle\Entity\Keyword
; 
  23 class KeywordController 
extends AbstractController 
{ 
  29          * @param Request $request The request instance 
  30          * @return Response The rendered view 
  32         public function index(Request 
$request): Response 
{ 
  33                 //With not enough keywords 
  34                 if (($this->count 
= $this->doctrine
->getRepository(Keyword
::class)->findCountAsInt()) < $this->page 
* $this->limit
) { 
  36                         throw $this->createNotFoundException($this->translator
->trans('Unable to find keywords')); 
  40                 if ($this->context
['keywords'] = $this->doctrine
->getRepository(Keyword
::class)->findAllAsArray($this->page
, $this->limit
)) { 
  42                         $this->modified 
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['keywords'])); 
  46                         $this->modified 
= new \
DateTime('-1 year'); 
  50                 $response = new Response(); 
  53                 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
  55                         $response->setLastModified(new \
DateTime('-1 year')); 
  58                         $response->setPrivate(); 
  62                         //XXX: only for public to force revalidation by last modified 
  63                         $response->setEtag(md5(serialize($this->context
['keywords']))); 
  66                         $response->setLastModified($this->modified
); 
  69                         $response->setPublic(); 
  71                         //Without role and modification 
  72                         if ($response->isNotModified($request)) { 
  79                 $this->context
['head']['keywords'] = implode( 
  81                         //Use closure to extract each unique article keywords sorted 
  89                                         $r[$k['title']] = $k['title']; 
  97                         })($this->context
['keywords']) 
 101                 $this->context
['title']['page'] = $this->translator
->trans('Keywords list'); 
 104                 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary keyword listing'); 
 107                 return $this->render('@RapsysBlog/keyword/index.html.twig', $this->context
, $response); 
 113          * Display keyword articles 
 115          * @param Request $request The request instance 
 116          * @param integer $id The keyword id 
 117          * @param ?string $slug The keyword slug 
 118          * @return Response The rendered view 
 120         public function view(Request 
$request, int $id, ?string $slug): Response 
{ 
 122                 if (empty($this->context
['keyword'] = $this->doctrine
->getRepository(Keyword
::class)->findByIdAsArray($id))) { 
 124                         throw $this->createNotFoundException($this->translator
->trans('Unable to find keyword: %id%', ['%id%' => $id])); 
 128                 if ($slug !== $this->context
['keyword']['slug']) { 
 129                         //Redirect on correctly spelled keyword 
 130                         return $this->redirectToRoute('rapsysblog_keyword_view', ['id' => $this->context
['keyword']['id'], 'slug' => $this->context
['keyword']['slug']], Response
::HTTP_MOVED_PERMANENTLY
); 
 134                 $this->modified 
= $this->context
['keyword']['modified']; 
 137                 $response = new Response(); 
 140                 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 142                         $response->setLastModified(new \
DateTime('-1 year')); 
 145                         $response->setPrivate(); 
 146                 //Without logged user 
 149                         //XXX: only for public to force revalidation by last modified 
 150                         $response->setEtag(md5(serialize($this->context
['keyword']))); 
 153                         $response->setLastModified($this->modified
); 
 156                         $response->setPublic(); 
 158                         //Without role and modification 
 159                         if ($response->isNotModified($request)) { 
 160                                 //Return 304 response 
 166                 $this->context
['head']['keywords'] = implode( 
 168                         //Use closure to extract each unique article keywords sorted 
 173                                 //Iterate on articles 
 176                                         if (!empty($a['keywords'])) { 
 177                                                 //Iterate on keywords 
 178                                                 foreach($a['keywords'] as $k) { 
 180                                                         $r[$k['title']] = $k['title']; 
 190                         })($this->context
['keyword']['articles']) 
 194                 $this->context
['title']['page'] = $this->context
['keyword']['title']; 
 197                 $this->context
['description'] = $this->context
['keyword']['description']; 
 200                 return $this->render('@RapsysBlog/keyword/view.html.twig', $this->context
, $response);