]> Raphaël G. Git Repositories - blogbundle/blob - Controller/KeywordController.php
Cleanup messages
[blogbundle] / Controller / KeywordController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\BlogBundle\Controller;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16
17 use Rapsys\BlogBundle\Entity\Article;
18 use Rapsys\BlogBundle\Entity\Keyword;
19
20 /**
21 * {@inheritdoc}
22 */
23 class KeywordController extends AbstractController {
24 /**
25 * The keyword index
26 *
27 * Display keywords
28 *
29 * @param Request $request The request instance
30 * @return Response The rendered view
31 */
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) {
35 //Throw 404
36 throw $this->createNotFoundException($this->translator->trans('Unable to find keywords'));
37 }
38
39 //Get keywords
40 if ($this->context['keywords'] = $this->doctrine->getRepository(Keyword::class)->findAllAsArray($this->page, $this->limit)) {
41 //Set modified
42 $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['keywords']));
43 //Without keywords
44 } else {
45 //Set empty modified
46 $this->modified = new \DateTime('-1 year');
47 }
48
49 //Create response
50 $response = new Response();
51
52 //With logged user
53 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
54 //Set last modified
55 $response->setLastModified(new \DateTime('-1 year'));
56
57 //Set as private
58 $response->setPrivate();
59 //Without logged user
60 } else {
61 //Set etag
62 //XXX: only for public to force revalidation by last modified
63 $response->setEtag(md5(serialize($this->context['keywords'])));
64
65 //Set last modified
66 $response->setLastModified($this->modified);
67
68 //Set as public
69 $response->setPublic();
70
71 //Without role and modification
72 if ($response->isNotModified($request)) {
73 //Return 304 response
74 return $response;
75 }
76 }
77
78 //Set keywords
79 $this->context['head']['keywords'] = implode(
80 ', ',
81 //Use closure to extract each unique article keywords sorted
82 (function ($t) {
83 //Return array
84 $r = [];
85
86 //Iterate on keywords
87 foreach($t as $k) {
88 //Set keyword
89 $r[$k['title']] = $k['title'];
90 }
91
92 //Sort array
93 sort($r);
94
95 //Return array
96 return $r;
97 })($this->context['keywords'])
98 );
99
100 //Set title
101 $this->context['title'] = $this->translator->trans('Keywords list');
102
103 //Set description
104 $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary keyword listing');
105
106 //Render the view
107 return $this->render('@RapsysBlog/keyword/index.html.twig', $this->context, $response);
108 }
109
110 /**
111 * The keyword view
112 *
113 * Display keyword articles
114 *
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
119 */
120 public function view(Request $request, int $id, ?string $slug): Response {
121 //Without keyword
122 if (empty($this->context['keyword'] = $this->doctrine->getRepository(Keyword::class)->findByIdAsArray($id))) {
123 //Throw 404
124 throw $this->createNotFoundException($this->translator->trans('Unable to find keyword: %id%', ['%id%' => $id]));
125 }
126
127 //With invalid slug
128 if ($slug !== $this->context['keyword']['slug']) {
129 //Redirect on correctly spelled keyword
130 return $this->redirectToRoute('rapsys_blog_keyword_view', ['id' => $this->context['keyword']['id'], 'slug' => $this->context['keyword']['slug']], Response::HTTP_MOVED_PERMANENTLY);
131 }
132
133 //Set modified
134 $this->modified = $this->context['keyword']['modified'];
135
136 //Create response
137 $response = new Response();
138
139 //With logged user
140 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
141 //Set last modified
142 $response->setLastModified(new \DateTime('-1 year'));
143
144 //Set as private
145 $response->setPrivate();
146 //Without logged user
147 } else {
148 //Set etag
149 //XXX: only for public to force revalidation by last modified
150 $response->setEtag(md5(serialize($this->context['keyword'])));
151
152 //Set last modified
153 $response->setLastModified($this->modified);
154
155 //Set as public
156 $response->setPublic();
157
158 //Without role and modification
159 if ($response->isNotModified($request)) {
160 //Return 304 response
161 return $response;
162 }
163 }
164
165 //Set keywords
166 $this->context['head']['keywords'] = implode(
167 ', ',
168 //Use closure to extract each unique article keywords sorted
169 (function ($t) {
170 //Return array
171 $r = [];
172
173 //Iterate on articles
174 foreach($t as $a) {
175 //Non empty keywords
176 if (!empty($a['keywords'])) {
177 //Iterate on keywords
178 foreach($a['keywords'] as $k) {
179 //Set keyword
180 $r[$k['title']] = $k['title'];
181 }
182 }
183 }
184
185 //Sort array
186 sort($r);
187
188 //Return array
189 return $r;
190 })($this->context['keyword']['articles'])
191 );
192
193 //Set title
194 $this->context['title'] = $this->context['keyword']['title'];
195
196 //Set description
197 $this->context['description'] = $this->context['keyword']['description'];
198
199 //Render the view
200 return $this->render('@RapsysBlog/keyword/view.html.twig', $this->context, $response);
201 }
202 }