Skip to content

Commit 8cc5e94

Browse files
authored
Merge pull request #1268 from superdesk/SWP-2281
Content lists update webhook MVP
2 parents c64e2e8 + 143e3d6 commit 8cc5e94

File tree

3 files changed

+112
-9
lines changed

3 files changed

+112
-9
lines changed

src/SWP/Bundle/CoreBundle/Controller/ContentListController.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Exception;
19+
use GuzzleHttp\Client;
1920
use SWP\Bundle\ContentListBundle\Form\Type\ContentListType;
2021
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface;
2122
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
@@ -50,6 +51,8 @@ class ContentListController extends AbstractController {
5051
private EntityManagerInterface $entityManager;
5152
private EventDispatcherInterface $eventDispatcher;
5253
private FactoryInterface $factory;
54+
private string $invalidationCacheUrl;
55+
private string $invalidationToken;
5356

5457
/**
5558
* @param ContentListRepositoryInterface $contentListRepository
@@ -67,7 +70,9 @@ public function __construct(
6770
FormFactoryInterface $formFactory,
6871
EntityManagerInterface $entityManager,
6972
EventDispatcherInterface $eventDispatcher,
70-
FactoryInterface $factory
73+
FactoryInterface $factory,
74+
string $invalidationCacheUrl,
75+
string $invalidationToken
7176
) {
7277
$this->contentListRepository = $contentListRepository;
7378
$this->contentListItemRepository = $contentListItemRepository;
@@ -76,6 +81,45 @@ public function __construct(
7681
$this->entityManager = $entityManager;
7782
$this->eventDispatcher = $eventDispatcher;
7883
$this->factory = $factory;
84+
$this->invalidationCacheUrl = $invalidationCacheUrl;
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+
}
79123
}
80124

81125
/**
@@ -107,6 +151,16 @@ public function createAction(Request $request): SingleResourceResponseInterface
107151

108152
if ($form->isSubmitted() && $form->isValid()) {
109153
$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+
);
110164

111165
return new SingleResourceResponse($contentList, new ResponseContext(201));
112166
}
@@ -134,6 +188,16 @@ public function updateAction(Request $request, int $id): SingleResourceResponseI
134188
);
135189

136190
$objectManager->flush();
191+
self::invalidateCache(
192+
$this->invalidationCacheUrl,
193+
$this->invalidationToken,
194+
[
195+
'id' => $contentList->getId(),
196+
'name' => $contentList->getName(),
197+
'type' => $contentList->getType(),
198+
'action' => 'UPDATE'
199+
]
200+
);
137201

138202
return new SingleResourceResponse($contentList);
139203
}
@@ -149,6 +213,16 @@ public function deleteAction($id): SingleResourceResponseInterface {
149213
$contentList = $this->findOr404($id);
150214

151215
$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+
);
152226

153227
return new SingleResourceResponse(null, new ResponseContext(204));
154228
}

src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
2222
use SWP\Bundle\ContentListBundle\Form\Type\ContentListItemsType;
2323
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface;
24+
use SWP\Bundle\CoreBundle\Controller\ContentListController;
2425
use SWP\Bundle\CoreBundle\Form\Type\ContentListItemType;
2526
use SWP\Bundle\CoreBundle\Model\ContentListInterface;
2627
use SWP\Bundle\CoreBundle\Model\ContentListItemInterface;
@@ -55,14 +56,20 @@ class ContentListItemController extends AbstractController {
5556
* @param ContentListServiceInterface $contentListService
5657
* @param EventDispatcherInterface $eventDispatcher
5758
*/
58-
public function __construct(ContentListItemRepositoryInterface $contentListItemRepository,
59-
EntityManagerInterface $entityManager,
60-
ContentListServiceInterface $contentListService,
61-
EventDispatcherInterface $eventDispatcher) {
62-
$this->contentListItemRepository = $contentListItemRepository;
63-
$this->entityManager = $entityManager;
64-
$this->contentListService = $contentListService;
65-
$this->eventDispatcher = $eventDispatcher;
59+
public function __construct(
60+
ContentListItemRepositoryInterface $contentListItemRepository,
61+
EntityManagerInterface $entityManager,
62+
ContentListServiceInterface $contentListService,
63+
EventDispatcherInterface $eventDispatcher,
64+
string $invalidationCacheUrl,
65+
string $invalidationToken
66+
) {
67+
$this->contentListItemRepository = $contentListItemRepository;
68+
$this->entityManager = $entityManager;
69+
$this->contentListService = $contentListService;
70+
$this->eventDispatcher = $eventDispatcher;
71+
$this->invalidationCacheUrl = $invalidationCacheUrl;
72+
$this->invalidationToken = $invalidationToken;
6673
}
6774

6875

@@ -137,6 +144,16 @@ public function updateAction(Request $request, FormFactoryInterface $formFactory
137144
}
138145

139146
$this->entityManager->flush();
147+
ContentListController::invalidateCache(
148+
$this->invalidationCacheUrl,
149+
$this->invalidationToken,
150+
[
151+
'id' => $contentListItem->getContentList()->getId(),
152+
'name' => $contentListItem->getContentList()->getName(),
153+
'type' => $contentListItem->getContentList()->getType(),
154+
'action' => 'CREATE'
155+
]
156+
);
140157

141158
return new SingleResourceResponse($contentListItem);
142159
}
@@ -244,6 +261,16 @@ public function batchUpdateAction(
244261
ArticleEvents::POST_UPDATE
245262
), ArticleEvents::POST_UPDATE);
246263
}
264+
ContentListController::invalidateCache(
265+
$this->invalidationCacheUrl,
266+
$this->invalidationToken,
267+
[
268+
'id' => $list->getId(),
269+
'name' => $list->getName(),
270+
'type' => $list->getType(),
271+
'action' => 'BATCH-UPDATE'
272+
]
273+
);
247274

248275
return new SingleResourceResponse($list, new ResponseContext(201));
249276
}

src/SWP/Bundle/CoreBundle/Resources/config/controllers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ services:
1313
$cachedTenantContext: '@swp_multi_tenancy.tenant_context'
1414
$packageRepository: '@swp.repository.package'
1515
$routeRepository: '@swp.repository.route'
16+
$invalidationCacheUrl: '%env(INVALIDATION_CACHE_URL)%'
17+
$invalidationToken: '%env(INVALIDATION_CACHE_TOKEN)%'
1618

1719
SWP\Bundle\SettingsBundle\Controller\SettingsController: ~
1820

0 commit comments

Comments
 (0)