Completed
Push — SWP-2348 ( 5d1e02...dd3992 )
by
unknown
46s
created
src/SWP/Bundle/CoreBundle/Controller/ContentListController.php 2 patches
Indentation   +160 added lines, -160 removed lines patch added patch discarded remove patch
@@ -44,36 +44,36 @@  discard block
 block discarded – undo
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,71 +83,71 @@  discard block
 block discarded – undo
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
-      try {
91
-          $client = new Client();
92
-
93
-          $headers = [
94
-              'Content-Type' => 'application/json',
95
-          ];
96
-          $queryParams = [
97
-              'secret' => $token,
98
-          ];
99
-
100
-          $response = $client->request('POST', $url,  [
101
-              'headers' => $headers,
102
-              'json' => $data,
103
-              'query' => $queryParams
104
-          ]);
105
-          $responseBody = $response->getBody()->getContents();
106
-          $result = [
107
-              'request' => [
108
-                  'headers' => $headers,
109
-                  'json' => $data,
110
-                  'query' => $queryParams
111
-              ],
112
-              'response' => [
113
-                  'status' => $response->getStatusCode(),
114
-                  'body' => $responseBody,
115
-                  'ReasonPhrase' => $response->getReasonPhrase()
116
-              ]
117
-          ];
118
-
119
-          if (is_writable('/tmp')) {
120
-              file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
121
-          }
122
-      } catch (\Throwable $e) {
123
-          if (is_writable('/tmp')) {
124
-              file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
125
-          }
126
-          // Don't throw the exception, just log it
127
-          error_log('Cache invalidation failed: ' . $e->getMessage());
128
-      }
129
-  }
130
-
131
-  /**
132
-   * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists")
133
-   */
134
-  public function listAction(Request $request): ResourcesListResponseInterface {
86
+    }
87
+
88
+    public static function invalidateCache(string $url, string $token, array $data = [])
89
+    {
90
+        try {
91
+            $client = new Client();
92
+
93
+            $headers = [
94
+                'Content-Type' => 'application/json',
95
+            ];
96
+            $queryParams = [
97
+                'secret' => $token,
98
+            ];
99
+
100
+            $response = $client->request('POST', $url,  [
101
+                'headers' => $headers,
102
+                'json' => $data,
103
+                'query' => $queryParams
104
+            ]);
105
+            $responseBody = $response->getBody()->getContents();
106
+            $result = [
107
+                'request' => [
108
+                    'headers' => $headers,
109
+                    'json' => $data,
110
+                    'query' => $queryParams
111
+                ],
112
+                'response' => [
113
+                    'status' => $response->getStatusCode(),
114
+                    'body' => $responseBody,
115
+                    'ReasonPhrase' => $response->getReasonPhrase()
116
+                ]
117
+            ];
118
+
119
+            if (is_writable('/tmp')) {
120
+                file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
121
+            }
122
+        } catch (\Throwable $e) {
123
+            if (is_writable('/tmp')) {
124
+                file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
125
+            }
126
+            // Don't throw the exception, just log it
127
+            error_log('Cache invalidation failed: ' . $e->getMessage());
128
+        }
129
+    }
130
+
131
+    /**
132
+     * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists")
133
+     */
134
+    public function listAction(Request $request): ResourcesListResponseInterface {
135 135
     $lists = $this->contentListRepository->getPaginatedByCriteria($this->eventDispatcher, new Criteria(), $request->query->all('sorting'), new PaginationData($request));
136 136
 
137 137
     return new ResourcesListResponse($lists);
138
-  }
138
+    }
139 139
 
140
-  /**
141
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"})
142
-   */
143
-  public function getAction($id): SingleResourceResponseInterface {
140
+    /**
141
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"})
142
+     */
143
+    public function getAction($id): SingleResourceResponseInterface {
144 144
     return new SingleResourceResponse($this->findOr404($id));
145
-  }
145
+    }
146 146
 
147
-  /**
148
-   * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists")
149
-   */
150
-  public function createAction(Request $request): SingleResourceResponseInterface {
147
+    /**
148
+     * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists")
149
+     */
150
+    public function createAction(Request $request): SingleResourceResponseInterface {
151 151
     /* @var ContentListInterface $contentList */
152 152
     $contentList = $this->factory->create();
153 153
     $form = $this->formFactory->createNamed('', ContentListType::class, $contentList, ['method' => $request->getMethod()]);
@@ -156,28 +156,28 @@  discard block
 block discarded – undo
156 156
     $this->ensureContentListExists($contentList->getName());
157 157
 
158 158
     if ($form->isSubmitted() && $form->isValid()) {
159
-      $this->contentListRepository->add($contentList);
160
-      self::invalidateCache(
161
-          $this->invalidationCacheUrl,
162
-          $this->invalidationToken,
163
-          [
164
-              'id' => $contentList->getId(),
165
-              'name' => $contentList->getName(),
166
-              'type' => $contentList->getType(),
167
-              'action' => 'CREATE'
168
-          ]
159
+        $this->contentListRepository->add($contentList);
160
+        self::invalidateCache(
161
+            $this->invalidationCacheUrl,
162
+            $this->invalidationToken,
163
+            [
164
+                'id' => $contentList->getId(),
165
+                'name' => $contentList->getName(),
166
+                'type' => $contentList->getType(),
167
+                'action' => 'CREATE'
168
+            ]
169 169
         );
170 170
 
171
-      return new SingleResourceResponse($contentList, new ResponseContext(201));
171
+        return new SingleResourceResponse($contentList, new ResponseContext(201));
172 172
     }
173 173
 
174 174
     return new SingleResourceResponse($form, new ResponseContext(400));
175
-  }
175
+    }
176 176
 
177
-  /**
178
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"})
179
-   */
180
-  public function updateAction(Request $request, int $id): SingleResourceResponseInterface {
177
+    /**
178
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"})
179
+     */
180
+    public function updateAction(Request $request, int $id): SingleResourceResponseInterface {
181 181
     $objectManager = $this->entityManager;
182 182
     /** @var ContentListInterface $contentList */
183 183
     $contentList = $this->findOr404($id);
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
     $form->handleRequest($request);
189 189
 
190 190
     if ($form->isSubmitted() && $form->isValid()) {
191
-      $this->eventDispatcher->dispatch(
192
-          new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]),
193
-          ContentListEvents::LIST_CRITERIA_CHANGE
194
-      );
191
+        $this->eventDispatcher->dispatch(
192
+            new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]),
193
+            ContentListEvents::LIST_CRITERIA_CHANGE
194
+        );
195 195
 
196
-      $objectManager->flush();
196
+        $objectManager->flush();
197 197
         self::invalidateCache(
198 198
             $this->invalidationCacheUrl,
199 199
             $this->invalidationToken,
@@ -205,54 +205,54 @@  discard block
 block discarded – undo
205 205
             ]
206 206
         );
207 207
 
208
-      return new SingleResourceResponse($contentList);
208
+        return new SingleResourceResponse($contentList);
209 209
     }
210 210
 
211 211
     return new SingleResourceResponse($form, new ResponseContext(400));
212
-  }
212
+    }
213 213
 
214
-  /**
215
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"})
216
-   */
217
-  public function deleteAction($id): SingleResourceResponseInterface {
214
+    /**
215
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"})
216
+     */
217
+    public function deleteAction($id): SingleResourceResponseInterface {
218 218
     $repository = $this->contentListRepository;
219 219
     $contentList = $this->findOr404($id);
220 220
 
221 221
     $repository->remove($contentList);
222
-      self::invalidateCache(
223
-          $this->invalidationCacheUrl,
224
-          $this->invalidationToken,
225
-          [
226
-              'id' => $contentList->getId(),
227
-              'name' => $contentList->getName(),
228
-              'type' => $contentList->getType(),
229
-              'action' => 'DELETE'
230
-          ]
231
-      );
222
+        self::invalidateCache(
223
+            $this->invalidationCacheUrl,
224
+            $this->invalidationToken,
225
+            [
226
+                'id' => $contentList->getId(),
227
+                'name' => $contentList->getName(),
228
+                'type' => $contentList->getType(),
229
+                'action' => 'DELETE'
230
+            ]
231
+        );
232 232
 
233 233
     return new SingleResourceResponse(null, new ResponseContext(204));
234
-  }
234
+    }
235 235
 
236
-  /**
237
-   * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink")
238
-   */
239
-  public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface {
236
+    /**
237
+     * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink")
238
+     */
239
+    public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface {
240 240
     $objectManager = $this->entityManager;
241 241
     /** @var ContentListInterface $contentList */
242 242
     $contentList = $this->findOr404($id);
243 243
 
244 244
     $matched = false;
245 245
     foreach ($request->attributes->get('links', []) as $key => $objectArray) {
246
-      if (!is_array($objectArray)) {
246
+        if (!is_array($objectArray)) {
247 247
         continue;
248
-      }
248
+        }
249 249
 
250
-      $object = $objectArray['object'];
251
-      if ($object instanceof Exception) {
250
+        $object = $objectArray['object'];
251
+        if ($object instanceof Exception) {
252 252
         throw $object;
253
-      }
253
+        }
254 254
 
255
-      if ($object instanceof ArticleInterface) {
255
+        if ($object instanceof ArticleInterface) {
256 256
         $contentListItem = $this->contentListItemRepository
257 257
             ->findOneBy([
258 258
                 'contentList' => $contentList,
@@ -260,59 +260,59 @@  discard block
 block discarded – undo
260 260
             ]);
261 261
 
262 262
         if ('LINK' === $request->getMethod()) {
263
-          $position = 0;
264
-          if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) {
263
+            $position = 0;
264
+            if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) {
265 265
             foreach ($notConvertedLinks as $link) {
266
-              if (isset($link['resourceType']) && 'position' === $link['resourceType']) {
266
+                if (isset($link['resourceType']) && 'position' === $link['resourceType']) {
267 267
                 $position = $link['resource'];
268
-              }
268
+                }
269
+            }
269 270
             }
270
-          }
271 271
 
272
-          if (false === $position && $contentListItem) {
272
+            if (false === $position && $contentListItem) {
273 273
             throw new ConflictHttpException('This content is already linked to Content List');
274
-          }
274
+            }
275 275
 
276
-          if (!$contentListItem) {
276
+            if (!$contentListItem) {
277 277
             $contentListItem = $this->contentListService->addArticleToContentList($contentList, $object, $position);
278 278
             $objectManager->persist($contentListItem);
279
-          } else {
279
+            } else {
280 280
             $contentListItem->setPosition($position);
281
-          }
281
+            }
282 282
 
283
-          $objectManager->flush();
283
+            $objectManager->flush();
284 284
         } elseif ('UNLINK' === $request->getMethod()) {
285
-          if ($contentListItem->getContentList() !== $contentList) {
285
+            if ($contentListItem->getContentList() !== $contentList) {
286 286
             throw new ConflictHttpException('Content is not linked to content list');
287
-          }
288
-          $objectManager->remove($contentListItem);
287
+            }
288
+            $objectManager->remove($contentListItem);
289 289
         }
290 290
 
291 291
         $matched = true;
292 292
 
293 293
         break;
294
-      }
294
+        }
295 295
     }
296 296
     if (false === $matched) {
297
-      throw new NotFoundHttpException('Any supported link object was not found');
297
+        throw new NotFoundHttpException('Any supported link object was not found');
298 298
     }
299 299
 
300 300
     $objectManager->flush();
301 301
 
302 302
     return new SingleResourceResponse($contentList, new ResponseContext(201));
303
-  }
303
+    }
304 304
 
305
-  private function findOr404($id) {
305
+    private function findOr404($id) {
306 306
     if (null === $list = $this->contentListRepository->find($id)) {
307
-      throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id));
307
+        throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id));
308 308
     }
309 309
 
310 310
     return $list;
311
-  }
311
+    }
312 312
 
313
-  private function ensureContentListExists($name) {
313
+    private function ensureContentListExists($name) {
314 314
     if (null !== $this->contentListRepository->findOneBy(['name' => $name])) {
315
-      throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name));
315
+        throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name));
316
+    }
316 317
     }
317
-  }
318 318
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
               'secret' => $token,
98 98
           ];
99 99
 
100
-          $response = $client->request('POST', $url,  [
100
+          $response = $client->request('POST', $url, [
101 101
               'headers' => $headers,
102 102
               'json' => $data,
103 103
               'query' => $queryParams
@@ -117,14 +117,14 @@  discard block
 block discarded – undo
117 117
           ];
118 118
 
119 119
           if (is_writable('/tmp')) {
120
-              file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
120
+              file_put_contents('/tmp/cache_invalidation.json', json_encode($result).PHP_EOL, FILE_APPEND);
121 121
           }
122 122
       } catch (\Throwable $e) {
123 123
           if (is_writable('/tmp')) {
124
-              file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
124
+              file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage().PHP_EOL, FILE_APPEND);
125 125
           }
126 126
           // Don't throw the exception, just log it
127
-          error_log('Cache invalidation failed: ' . $e->getMessage());
127
+          error_log('Cache invalidation failed: '.$e->getMessage());
128 128
       }
129 129
   }
130 130
 
Please login to merge, or discard this patch.