Completed
Push — 2.2.2 ( 16c427 )
by
unknown
52s
created
src/SWP/Bundle/CoreBundle/Controller/ContentListController.php 2 patches
Indentation   +154 added lines, -154 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,65 +83,65 @@  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
-          file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
120
-      } catch (\Throwable $e) {
121
-          file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
122
-      }
123
-  }
124
-
125
-  /**
126
-   * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists")
127
-   */
128
-  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
+            file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
120
+        } catch (\Throwable $e) {
121
+            file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
122
+        }
123
+    }
124
+
125
+    /**
126
+     * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_list_lists")
127
+     */
128
+    public function listAction(Request $request): ResourcesListResponseInterface {
129 129
     $lists = $this->contentListRepository->getPaginatedByCriteria($this->eventDispatcher, new Criteria(), $request->query->all('sorting'), new PaginationData($request));
130 130
 
131 131
     return new ResourcesListResponse($lists);
132
-  }
132
+    }
133 133
 
134
-  /**
135
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"})
136
-   */
137
-  public function getAction($id): SingleResourceResponseInterface {
134
+    /**
135
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_content_show_lists", requirements={"id"="\d+"})
136
+     */
137
+    public function getAction($id): SingleResourceResponseInterface {
138 138
     return new SingleResourceResponse($this->findOr404($id));
139
-  }
139
+    }
140 140
 
141
-  /**
142
-   * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists")
143
-   */
144
-  public function createAction(Request $request): SingleResourceResponseInterface {
141
+    /**
142
+     * @Route("/api/{version}/content/lists/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_content_create_lists")
143
+     */
144
+    public function createAction(Request $request): SingleResourceResponseInterface {
145 145
     /* @var ContentListInterface $contentList */
146 146
     $contentList = $this->factory->create();
147 147
     $form = $this->formFactory->createNamed('', ContentListType::class, $contentList, ['method' => $request->getMethod()]);
@@ -150,28 +150,28 @@  discard block
 block discarded – undo
150 150
     $this->ensureContentListExists($contentList->getName());
151 151
 
152 152
     if ($form->isSubmitted() && $form->isValid()) {
153
-      $this->contentListRepository->add($contentList);
154
-      self::invalidateCache(
155
-          $this->invalidationCacheUrl,
156
-          $this->invalidationToken,
157
-          [
158
-              'id' => $contentList->getId(),
159
-              'name' => $contentList->getName(),
160
-              'type' => $contentList->getType(),
161
-              'action' => 'CREATE'
162
-          ]
153
+        $this->contentListRepository->add($contentList);
154
+        self::invalidateCache(
155
+            $this->invalidationCacheUrl,
156
+            $this->invalidationToken,
157
+            [
158
+                'id' => $contentList->getId(),
159
+                'name' => $contentList->getName(),
160
+                'type' => $contentList->getType(),
161
+                'action' => 'CREATE'
162
+            ]
163 163
         );
164 164
 
165
-      return new SingleResourceResponse($contentList, new ResponseContext(201));
165
+        return new SingleResourceResponse($contentList, new ResponseContext(201));
166 166
     }
167 167
 
168 168
     return new SingleResourceResponse($form, new ResponseContext(400));
169
-  }
169
+    }
170 170
 
171
-  /**
172
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"})
173
-   */
174
-  public function updateAction(Request $request, int $id): SingleResourceResponseInterface {
171
+    /**
172
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_content_update_lists", requirements={"id"="\d+"})
173
+     */
174
+    public function updateAction(Request $request, int $id): SingleResourceResponseInterface {
175 175
     $objectManager = $this->entityManager;
176 176
     /** @var ContentListInterface $contentList */
177 177
     $contentList = $this->findOr404($id);
@@ -182,12 +182,12 @@  discard block
 block discarded – undo
182 182
     $form->handleRequest($request);
183 183
 
184 184
     if ($form->isSubmitted() && $form->isValid()) {
185
-      $this->eventDispatcher->dispatch(
186
-          new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]),
187
-          ContentListEvents::LIST_CRITERIA_CHANGE
188
-      );
185
+        $this->eventDispatcher->dispatch(
186
+            new GenericEvent($contentList, ['filters' => $filters, 'previousLimit' => $listLimit]),
187
+            ContentListEvents::LIST_CRITERIA_CHANGE
188
+        );
189 189
 
190
-      $objectManager->flush();
190
+        $objectManager->flush();
191 191
         self::invalidateCache(
192 192
             $this->invalidationCacheUrl,
193 193
             $this->invalidationToken,
@@ -199,54 +199,54 @@  discard block
 block discarded – undo
199 199
             ]
200 200
         );
201 201
 
202
-      return new SingleResourceResponse($contentList);
202
+        return new SingleResourceResponse($contentList);
203 203
     }
204 204
 
205 205
     return new SingleResourceResponse($form, new ResponseContext(400));
206
-  }
206
+    }
207 207
 
208
-  /**
209
-   * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"})
210
-   */
211
-  public function deleteAction($id): SingleResourceResponseInterface {
208
+    /**
209
+     * @Route("/api/{version}/content/lists/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_content_delete_lists", requirements={"id"="\d+"})
210
+     */
211
+    public function deleteAction($id): SingleResourceResponseInterface {
212 212
     $repository = $this->contentListRepository;
213 213
     $contentList = $this->findOr404($id);
214 214
 
215 215
     $repository->remove($contentList);
216
-      self::invalidateCache(
217
-          $this->invalidationCacheUrl,
218
-          $this->invalidationToken,
219
-          [
220
-              'id' => $contentList->getId(),
221
-              'name' => $contentList->getName(),
222
-              'type' => $contentList->getType(),
223
-              'action' => 'DELETE'
224
-          ]
225
-      );
216
+        self::invalidateCache(
217
+            $this->invalidationCacheUrl,
218
+            $this->invalidationToken,
219
+            [
220
+                'id' => $contentList->getId(),
221
+                'name' => $contentList->getName(),
222
+                'type' => $contentList->getType(),
223
+                'action' => 'DELETE'
224
+            ]
225
+        );
226 226
 
227 227
     return new SingleResourceResponse(null, new ResponseContext(204));
228
-  }
228
+    }
229 229
 
230
-  /**
231
-   * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink")
232
-   */
233
-  public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface {
230
+    /**
231
+     * @Route("/api/{version}/content/lists/{id}", requirements={"id"="\w+"}, defaults={"version"="v2"}, methods={"LINK","UNLINK"}, name="swp_api_content_list_link_unlink")
232
+     */
233
+    public function linkUnlinkToContentListAction(Request $request, string $id): SingleResourceResponseInterface {
234 234
     $objectManager = $this->entityManager;
235 235
     /** @var ContentListInterface $contentList */
236 236
     $contentList = $this->findOr404($id);
237 237
 
238 238
     $matched = false;
239 239
     foreach ($request->attributes->get('links', []) as $key => $objectArray) {
240
-      if (!is_array($objectArray)) {
240
+        if (!is_array($objectArray)) {
241 241
         continue;
242
-      }
242
+        }
243 243
 
244
-      $object = $objectArray['object'];
245
-      if ($object instanceof Exception) {
244
+        $object = $objectArray['object'];
245
+        if ($object instanceof Exception) {
246 246
         throw $object;
247
-      }
247
+        }
248 248
 
249
-      if ($object instanceof ArticleInterface) {
249
+        if ($object instanceof ArticleInterface) {
250 250
         $contentListItem = $this->contentListItemRepository
251 251
             ->findOneBy([
252 252
                 'contentList' => $contentList,
@@ -254,59 +254,59 @@  discard block
 block discarded – undo
254 254
             ]);
255 255
 
256 256
         if ('LINK' === $request->getMethod()) {
257
-          $position = 0;
258
-          if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) {
257
+            $position = 0;
258
+            if (count($notConvertedLinks = RequestParser::getNotConvertedLinks($request->attributes->get('links'))) > 0) {
259 259
             foreach ($notConvertedLinks as $link) {
260
-              if (isset($link['resourceType']) && 'position' === $link['resourceType']) {
260
+                if (isset($link['resourceType']) && 'position' === $link['resourceType']) {
261 261
                 $position = $link['resource'];
262
-              }
262
+                }
263
+            }
263 264
             }
264
-          }
265 265
 
266
-          if (false === $position && $contentListItem) {
266
+            if (false === $position && $contentListItem) {
267 267
             throw new ConflictHttpException('This content is already linked to Content List');
268
-          }
268
+            }
269 269
 
270
-          if (!$contentListItem) {
270
+            if (!$contentListItem) {
271 271
             $contentListItem = $this->contentListService->addArticleToContentList($contentList, $object, $position);
272 272
             $objectManager->persist($contentListItem);
273
-          } else {
273
+            } else {
274 274
             $contentListItem->setPosition($position);
275
-          }
275
+            }
276 276
 
277
-          $objectManager->flush();
277
+            $objectManager->flush();
278 278
         } elseif ('UNLINK' === $request->getMethod()) {
279
-          if ($contentListItem->getContentList() !== $contentList) {
279
+            if ($contentListItem->getContentList() !== $contentList) {
280 280
             throw new ConflictHttpException('Content is not linked to content list');
281
-          }
282
-          $objectManager->remove($contentListItem);
281
+            }
282
+            $objectManager->remove($contentListItem);
283 283
         }
284 284
 
285 285
         $matched = true;
286 286
 
287 287
         break;
288
-      }
288
+        }
289 289
     }
290 290
     if (false === $matched) {
291
-      throw new NotFoundHttpException('Any supported link object was not found');
291
+        throw new NotFoundHttpException('Any supported link object was not found');
292 292
     }
293 293
 
294 294
     $objectManager->flush();
295 295
 
296 296
     return new SingleResourceResponse($contentList, new ResponseContext(201));
297
-  }
297
+    }
298 298
 
299
-  private function findOr404($id) {
299
+    private function findOr404($id) {
300 300
     if (null === $list = $this->contentListRepository->find($id)) {
301
-      throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id));
301
+        throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $id));
302 302
     }
303 303
 
304 304
     return $list;
305
-  }
305
+    }
306 306
 
307
-  private function ensureContentListExists($name) {
307
+    private function ensureContentListExists($name) {
308 308
     if (null !== $this->contentListRepository->findOneBy(['name' => $name])) {
309
-      throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name));
309
+        throw new ConflictHttpException(sprintf('Content list named "%s" already exists!', $name));
310
+    }
310 311
     }
311
-  }
312 312
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 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
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
               ]
117 117
           ];
118 118
 
119
-          file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
119
+          file_put_contents('/tmp/cache_invalidation.json', json_encode($result).PHP_EOL, FILE_APPEND);
120 120
       } catch (\Throwable $e) {
121
-          file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
121
+          file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage().PHP_EOL, FILE_APPEND);
122 122
       }
123 123
   }
124 124
 
Please login to merge, or discard this patch.