1616
1717use Doctrine \ORM \EntityManagerInterface ;
1818use Exception ;
19+ use GuzzleHttp \Client ;
1920use SWP \Bundle \ContentListBundle \Form \Type \ContentListType ;
2021use SWP \Bundle \ContentListBundle \Services \ContentListServiceInterface ;
2122use 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 }
0 commit comments