@@ -44,36 +44,36 @@ discard block |
||
44 | 44 | |
45 | 45 | class ContentListController extends AbstractController { |
46 | 46 | |
47 | - private ContentListRepositoryInterface $contentListRepository; |
|
48 | - private ContentListItemRepositoryInterface $contentListItemRepository; |
|
49 | - private ContentListServiceInterface $contentListService; |
|
50 | - private FormFactoryInterface $formFactory; |
|
51 | - private EntityManagerInterface $entityManager; |
|
52 | - private EventDispatcherInterface $eventDispatcher; |
|
53 | - private FactoryInterface $factory; |
|
54 | - private string $invalidationCacheUrl; |
|
55 | - private string $invalidationToken; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param ContentListRepositoryInterface $contentListRepository |
|
59 | - * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
60 | - * @param ContentListServiceInterface $contentListService |
|
61 | - * @param FormFactoryInterface $formFactory |
|
62 | - * @param EntityManagerInterface $entityManager |
|
63 | - * @param EventDispatcherInterface $eventDispatcher |
|
64 | - * @param FactoryInterface $factory |
|
65 | - */ |
|
66 | - public function __construct( |
|
67 | - ContentListRepositoryInterface $contentListRepository, |
|
68 | - ContentListItemRepositoryInterface $contentListItemRepository, |
|
69 | - ContentListServiceInterface $contentListService, |
|
70 | - FormFactoryInterface $formFactory, |
|
71 | - EntityManagerInterface $entityManager, |
|
72 | - EventDispatcherInterface $eventDispatcher, |
|
73 | - FactoryInterface $factory, |
|
74 | - string $invalidationCacheUrl, |
|
75 | - string $invalidationToken |
|
76 | - ) { |
|
47 | + private ContentListRepositoryInterface $contentListRepository; |
|
48 | + private ContentListItemRepositoryInterface $contentListItemRepository; |
|
49 | + private ContentListServiceInterface $contentListService; |
|
50 | + private FormFactoryInterface $formFactory; |
|
51 | + private EntityManagerInterface $entityManager; |
|
52 | + private EventDispatcherInterface $eventDispatcher; |
|
53 | + private FactoryInterface $factory; |
|
54 | + private string $invalidationCacheUrl; |
|
55 | + private string $invalidationToken; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param ContentListRepositoryInterface $contentListRepository |
|
59 | + * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
60 | + * @param ContentListServiceInterface $contentListService |
|
61 | + * @param FormFactoryInterface $formFactory |
|
62 | + * @param EntityManagerInterface $entityManager |
|
63 | + * @param EventDispatcherInterface $eventDispatcher |
|
64 | + * @param FactoryInterface $factory |
|
65 | + */ |
|
66 | + public function __construct( |
|
67 | + ContentListRepositoryInterface $contentListRepository, |
|
68 | + ContentListItemRepositoryInterface $contentListItemRepository, |
|
69 | + ContentListServiceInterface $contentListService, |
|
70 | + FormFactoryInterface $formFactory, |
|
71 | + EntityManagerInterface $entityManager, |
|
72 | + EventDispatcherInterface $eventDispatcher, |
|
73 | + FactoryInterface $factory, |
|
74 | + string $invalidationCacheUrl, |
|
75 | + string $invalidationToken |
|
76 | + ) { |
|
77 | 77 | $this->contentListRepository = $contentListRepository; |
78 | 78 | $this->contentListItemRepository = $contentListItemRepository; |
79 | 79 | $this->contentListService = $contentListService; |
@@ -83,76 +83,76 @@ discard block |
||
83 | 83 | $this->factory = $factory; |
84 | 84 | $this->invalidationCacheUrl = $invalidationCacheUrl; |
85 | 85 | $this->invalidationToken = $invalidationToken; |
86 | - } |
|
87 | - |
|
88 | - public static function invalidateCache(string $url, string $token, array $data = []) |
|
89 | - { |
|
90 | - // Skip cache invalidation if URL or token is empty (test environment) |
|
91 | - if (empty($url) || empty($token)) { |
|
92 | - return; |
|
93 | - } |
|
94 | - |
|
95 | - try { |
|
96 | - $client = new Client(); |
|
97 | - |
|
98 | - $headers = [ |
|
99 | - 'Content-Type' => 'application/json', |
|
100 | - ]; |
|
101 | - $queryParams = [ |
|
102 | - 'secret' => $token, |
|
103 | - ]; |
|
104 | - |
|
105 | - $response = $client->request('POST', $url, [ |
|
106 | - 'headers' => $headers, |
|
107 | - 'json' => $data, |
|
108 | - 'query' => $queryParams |
|
109 | - ]); |
|
110 | - $responseBody = $response->getBody()->getContents(); |
|
111 | - $result = [ |
|
112 | - 'request' => [ |
|
113 | - 'headers' => $headers, |
|
114 | - 'json' => $data, |
|
115 | - 'query' => $queryParams |
|
116 | - ], |
|
117 | - 'response' => [ |
|
118 | - 'status' => $response->getStatusCode(), |
|
119 | - 'body' => $responseBody, |
|
120 | - 'ReasonPhrase' => $response->getReasonPhrase() |
|
121 | - ] |
|
122 | - ]; |
|
123 | - |
|
124 | - if (is_writable('/tmp')) { |
|
125 | - file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND); |
|
126 | - } |
|
127 | - } catch (\Throwable $e) { |
|
128 | - if (is_writable('/tmp')) { |
|
129 | - file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND); |
|
130 | - } |
|
131 | - // Don't throw the exception, just log it |
|
132 | - error_log('Cache invalidation failed: ' . $e->getMessage()); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists") |
|
138 | - */ |
|
139 | - public function listAction(Request $request): ResourcesListResponseInterface { |
|
86 | + } |
|
87 | + |
|
88 | + public static function invalidateCache(string $url, string $token, array $data = []) |
|
89 | + { |
|
90 | + // Skip cache invalidation if URL or token is empty (test environment) |
|
91 | + if (empty($url) || empty($token)) { |
|
92 | + return; |
|
93 | + } |
|
94 | + |
|
95 | + try { |
|
96 | + $client = new Client(); |
|
97 | + |
|
98 | + $headers = [ |
|
99 | + 'Content-Type' => 'application/json', |
|
100 | + ]; |
|
101 | + $queryParams = [ |
|
102 | + 'secret' => $token, |
|
103 | + ]; |
|
104 | + |
|
105 | + $response = $client->request('POST', $url, [ |
|
106 | + 'headers' => $headers, |
|
107 | + 'json' => $data, |
|
108 | + 'query' => $queryParams |
|
109 | + ]); |
|
110 | + $responseBody = $response->getBody()->getContents(); |
|
111 | + $result = [ |
|
112 | + 'request' => [ |
|
113 | + 'headers' => $headers, |
|
114 | + 'json' => $data, |
|
115 | + 'query' => $queryParams |
|
116 | + ], |
|
117 | + 'response' => [ |
|
118 | + 'status' => $response->getStatusCode(), |
|
119 | + 'body' => $responseBody, |
|
120 | + 'ReasonPhrase' => $response->getReasonPhrase() |
|
121 | + ] |
|
122 | + ]; |
|
123 | + |
|
124 | + if (is_writable('/tmp')) { |
|
125 | + file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND); |
|
126 | + } |
|
127 | + } catch (\Throwable $e) { |
|
128 | + if (is_writable('/tmp')) { |
|
129 | + file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND); |
|
130 | + } |
|
131 | + // Don't throw the exception, just log it |
|
132 | + error_log('Cache invalidation failed: ' . $e->getMessage()); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists") |
|
138 | + */ |
|
139 | + public function listAction(Request $request): ResourcesListResponseInterface { |
|
140 | 140 | $lists = $this->contentListRepository->getPaginatedByCriteria($this->eventDispatcher, new Criteria(), $request->query->all('sorting'), new PaginationData($request)); |
141 | 141 | |
142 | 142 | return new ResourcesListResponse($lists); |
143 | - } |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"}) |
|
147 | - */ |
|
148 | - public function getAction($id): SingleResourceResponseInterface { |
|
145 | + /** |
|
146 | + * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"}) |
|
147 | + */ |
|
148 | + public function getAction($id): SingleResourceResponseInterface { |
|
149 | 149 | return new SingleResourceResponse($this->findOr404($id)); |
150 | - } |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists") |
|
154 | - */ |
|
155 | - public function createAction(Request $request): SingleResourceResponseInterface { |
|
152 | + /** |
|
153 | + * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists") |
|
154 | + */ |
|
155 | + public function createAction(Request $request): SingleResourceResponseInterface { |
|
156 | 156 | /* @var ContentListInterface $contentList */ |
157 | 157 | $contentList = $this->factory->create(); |
158 | 158 | $form = $this->formFactory->createNamed('', ContentListType::class, $contentList, ['method' => $request->getMethod()]); |
@@ -161,28 +161,28 @@ discard block |
||
161 | 161 | $this->ensureContentListExists($contentList->getName()); |
162 | 162 | |
163 | 163 | if ($form->isSubmitted() && $form->isValid()) { |
164 | - $this->contentListRepository->add($contentList); |
|
165 | - self::invalidateCache( |
|
166 | - $this->invalidationCacheUrl, |
|
167 | - $this->invalidationToken, |
|
168 | - [ |
|
169 | - 'id' => $contentList->getId(), |
|
170 | - 'name' => $contentList->getName(), |
|
171 | - 'type' => $contentList->getType(), |
|
172 | - 'action' => 'CREATE' |
|
173 | - ] |
|
164 | + $this->contentListRepository->add($contentList); |
|
165 | + self::invalidateCache( |
|
166 | + $this->invalidationCacheUrl, |
|
167 | + $this->invalidationToken, |
|
168 | + [ |
|
169 | + 'id' => $contentList->getId(), |
|
170 | + 'name' => $contentList->getName(), |
|
171 | + 'type' => $contentList->getType(), |
|
172 | + 'action' => 'CREATE' |
|
173 | + ] |
|
174 | 174 | ); |
175 | 175 | |
176 | - return new SingleResourceResponse($contentList, new ResponseContext(201)); |
|
176 | + return new SingleResourceResponse($contentList, new ResponseContext(201)); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | return new SingleResourceResponse($form, new ResponseContext(400)); |
180 | - } |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"}) |
|
184 | - */ |
|
185 | - public function updateAction(Request $request, int $id): SingleResourceResponseInterface { |
|
182 | + /** |
|
183 | + * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"}) |
|
184 | + */ |
|
185 | + public function updateAction(Request $request, int $id): SingleResourceResponseInterface { |
|
186 | 186 | $objectManager = $this->entityManager; |
187 | 187 | /** @var ContentListInterface $contentList */ |
188 | 188 | $contentList = $this->findOr404($id); |
@@ -193,12 +193,12 @@ discard block |
||
193 | 193 | $form->handleRequest($request); |
194 | 194 | |
195 | 195 | if ($form->isSubmitted() && $form->isValid()) { |
196 | - $this->eventDispatcher->dispatch( |
|
197 | - new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]), |
|
198 | - ContentListEvents::LIST_CRITERIA_CHANGE |
|
199 | - ); |
|
196 | + $this->eventDispatcher->dispatch( |
|
197 | + new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]), |
|
198 | + ContentListEvents::LIST_CRITERIA_CHANGE |
|
199 | + ); |
|
200 | 200 | |
201 | - $objectManager->flush(); |
|
201 | + $objectManager->flush(); |
|
202 | 202 | self::invalidateCache( |
203 | 203 | $this->invalidationCacheUrl, |
204 | 204 | $this->invalidationToken, |
@@ -210,54 +210,54 @@ discard block |
||
210 | 210 | ] |
211 | 211 | ); |
212 | 212 | |
213 | - return new SingleResourceResponse($contentList); |
|
213 | + return new SingleResourceResponse($contentList); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | return new SingleResourceResponse($form, new ResponseContext(400)); |
217 | - } |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"}) |
|
221 | - */ |
|
222 | - public function deleteAction($id): SingleResourceResponseInterface { |
|
219 | + /** |
|
220 | + * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"}) |
|
221 | + */ |
|
222 | + public function deleteAction($id): SingleResourceResponseInterface { |
|
223 | 223 | $repository = $this->contentListRepository; |
224 | 224 | $contentList = $this->findOr404($id); |
225 | 225 | |
226 | 226 | $repository->remove($contentList); |
227 | - self::invalidateCache( |
|
228 | - $this->invalidationCacheUrl, |
|
229 | - $this->invalidationToken, |
|
230 | - [ |
|
231 | - 'id' => $contentList->getId(), |
|
232 | - 'name' => $contentList->getName(), |
|
233 | - 'type' => $contentList->getType(), |
|
234 | - 'action' => 'DELETE' |
|
235 | - ] |
|
236 | - ); |
|
227 | + self::invalidateCache( |
|
228 | + $this->invalidationCacheUrl, |
|
229 | + $this->invalidationToken, |
|
230 | + [ |
|
231 | + 'id' => $contentList->getId(), |
|
232 | + 'name' => $contentList->getName(), |
|
233 | + 'type' => $contentList->getType(), |
|
234 | + 'action' => 'DELETE' |
|
235 | + ] |
|
236 | + ); |
|
237 | 237 | |
238 | 238 | return new SingleResourceResponse(null, new ResponseContext(204)); |
239 | - } |
|
239 | + } |
|
240 | 240 | |
241 | - /** |
|
242 | - * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink") |
|
243 | - */ |
|
244 | - public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface { |
|
241 | + /** |
|
242 | + * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink") |
|
243 | + */ |
|
244 | + public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface { |
|
245 | 245 | $objectManager = $this->entityManager; |
246 | 246 | /** @var ContentListInterface $contentList */ |
247 | 247 | $contentList = $this->findOr404($id); |
248 | 248 | |
249 | 249 | $matched = false; |
250 | 250 | foreach ($request->attributes->get('links', []) as $key => $objectArray) { |
251 | - if (!is_array($objectArray)) { |
|
251 | + if (!is_array($objectArray)) { |
|
252 | 252 | continue; |
253 | - } |
|
253 | + } |
|
254 | 254 | |
255 | - $object = $objectArray['object']; |
|
256 | - if ($object instanceof Exception) { |
|
255 | + $object = $objectArray['object']; |
|
256 | + if ($object instanceof Exception) { |
|
257 | 257 | throw $object; |
258 | - } |
|
258 | + } |
|
259 | 259 | |
260 | - if ($object instanceof ArticleInterface) { |
|
260 | + if ($object instanceof ArticleInterface) { |
|
261 | 261 | $contentListItem = $this->contentListItemRepository |
262 | 262 | ->findOneBy([ |
263 | 263 | 'contentList' => $contentList, |
@@ -265,59 +265,59 @@ discard block |
||
265 | 265 | ]); |
266 | 266 | |
267 | 267 | if ('LINK' === $request->getMethod()) { |
268 | - $position = 0; |
|
269 | - if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) { |
|
268 | + $position = 0; |
|
269 | + if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) { |
|
270 | 270 | foreach ($notConvertedLinks as $link) { |
271 | - if (isset($link['resourceType']) && 'position' === $link['resourceType']) { |
|
271 | + if (isset($link['resourceType']) && 'position' === $link['resourceType']) { |
|
272 | 272 | $position = $link['resource']; |
273 | - } |
|
273 | + } |
|
274 | + } |
|
274 | 275 | } |
275 | - } |
|
276 | 276 | |
277 | - if (false === $position && $contentListItem) { |
|
277 | + if (false === $position && $contentListItem) { |
|
278 | 278 | throw new ConflictHttpException('This content is already linked to Content List'); |
279 | - } |
|
279 | + } |
|
280 | 280 | |
281 | - if (!$contentListItem) { |
|
281 | + if (!$contentListItem) { |
|
282 | 282 | $contentListItem = $this->contentListService->addArticleToContentList($contentList, $object, $position); |
283 | 283 | $objectManager->persist($contentListItem); |
284 | - } else { |
|
284 | + } else { |
|
285 | 285 | $contentListItem->setPosition($position); |
286 | - } |
|
286 | + } |
|
287 | 287 | |
288 | - $objectManager->flush(); |
|
288 | + $objectManager->flush(); |
|
289 | 289 | } elseif ('UNLINK' === $request->getMethod()) { |
290 | - if ($contentListItem->getContentList() !== $contentList) { |
|
290 | + if ($contentListItem->getContentList() !== $contentList) { |
|
291 | 291 | throw new ConflictHttpException('Content is not linked to content list'); |
292 | - } |
|
293 | - $objectManager->remove($contentListItem); |
|
292 | + } |
|
293 | + $objectManager->remove($contentListItem); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | $matched = true; |
297 | 297 | |
298 | 298 | break; |
299 | - } |
|
299 | + } |
|
300 | 300 | } |
301 | 301 | if (false === $matched) { |
302 | - throw new NotFoundHttpException('Any supported link object was not found'); |
|
302 | + throw new NotFoundHttpException('Any supported link object was not found'); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | $objectManager->flush(); |
306 | 306 | |
307 | 307 | return new SingleResourceResponse($contentList, new ResponseContext(201)); |
308 | - } |
|
308 | + } |
|
309 | 309 | |
310 | - private function findOr404($id) { |
|
310 | + private function findOr404($id) { |
|
311 | 311 | if (null === $list = $this->contentListRepository->find($id)) { |
312 | - throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id)); |
|
312 | + throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id)); |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | return $list; |
316 | - } |
|
316 | + } |
|
317 | 317 | |
318 | - private function ensureContentListExists($name) { |
|
318 | + private function ensureContentListExists($name) { |
|
319 | 319 | if (null !== $this->contentListRepository->findOneBy(['name' => $name])) { |
320 | - throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name)); |
|
320 | + throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name)); |
|
321 | + } |
|
321 | 322 | } |
322 | - } |
|
323 | 323 | } |