66use Customer ;
77use DateTime ;
88use Doctrine \ORM \EntityManagerInterface ;
9- use Exception ;
109use ModuleFrontController ;
1110use PrestaShop \Module \ProductComment \Entity \ProductComment ;
1211use PrestaShop \PrestaShop \Adapter \Validate ;
1312use Product ;
14- use Symfony \Component \HttpKernel \Exception \HttpException ;
1513
1614class Sync extends ModuleFrontController {
1715
1816 /** @var bool */
1917 public $ ajax ;
2018
21- /**
22- * @throws Exception
23- */
2419 public function postProcess (): void {
2520 $ request_data = trim (file_get_contents ('php://input ' ));
2621 if (!$ request_data ) {
27- throw new HttpException (400 , 'Empty request data ' );
22+ $ this -> returnResponseCode (400 , 'Empty request data. ' );
2823 }
2924 if (!$ request_data = json_decode ($ request_data , true )) {
30- throw new HttpException (400 , 'Invalid JSON data provided ' );
25+ $ this -> returnResponseCode (400 , 'Invalid JSON data provided. ' );
3126 }
3227
3328 if (
3429 !$ this ->hasCredentialFields ($ request_data ['webshop_id ' ], $ request_data ['api_key ' ])
3530 || $ this ->credentialsEmpty ($ request_data ['webshop_id ' ], $ request_data ['api_key ' ])
3631 ) {
37- throw new HttpException (403 , 'Missing credential fields ' );
32+ $ this -> returnResponseCode (403 , 'Missing credential fields. ' );
3833 }
3934
4035 $ this ->isAuthorized ($ request_data ['webshop_id ' ], $ request_data ['api_key ' ]);
4136
4237 $ lang_id = (int ) Configuration::get ('PS_LANG_DEFAULT ' );
4338 $ product = new Product ($ request_data ['product_review ' ]['product_id ' ], false , $ lang_id );
4439 if (!Validate::isLoadedObject ($ product )) {
45- throw new HttpException (404 , sprintf ('Could not find product with ID (%d) ' , $ request_data ['product_review ' ]['product_id ' ]));
40+ $ this -> returnResponseCode (404 , sprintf ('Could not find product with ID (%d) ' , $ request_data ['product_review ' ]['product_id ' ]));
4641 }
4742
4843 if (!Configuration::get ($ this ->module ->getConfigName ('SYNC_PROD_REVIEWS ' ))) {
49- throw new HttpException (403 , 'Product review sync is disabled. ' );
44+ $ this -> returnResponseCode (403 , 'Product review sync is disabled. ' );
5045 }
5146
5247 $ this ->syncProductReview ($ request_data ['product_review ' ]);
5348 }
5449
55- /**
56- * @throws Exception
57- */
5850 private function syncProductReview (array $ product_review ): void {
5951 $ this ->ajax = 1 ;
6052 /** @var EntityManagerInterface $entityManager */
@@ -92,16 +84,13 @@ private function logReviewSync(string $review_id, bool $deleted = false): void {
9284 \PrestaShopLogger::addLog (sprintf ('%s product review with ID (%d) ' , $ deleted ? 'Deleted ' : 'Saved ' , $ review_id ));
9385 }
9486
95- /**
96- * @throws Exception
97- */
9887 private function isAuthorized (string $ webshop_id , string $ api_key ): void {
9988 $ curr_webshop_id = Configuration::get ($ this ->module ->getConfigName ('SHOP_ID ' ));
10089 $ curr_api_key = Configuration::get ($ this ->module ->getConfigName ('API_KEY ' ));
10190 if ($ webshop_id == $ curr_webshop_id && hash_equals ($ api_key , $ curr_api_key )) {
10291 return ;
10392 }
104- throw new HttpException (401 , 'Wrong credentials ' );
93+ $ this -> returnResponseCode (401 , 'Wrong credentials. ' );
10594 }
10695
10796 private function hasCredentialFields (?string $ webshop_id , ?string $ api_key ): bool {
@@ -111,4 +100,9 @@ private function hasCredentialFields(?string $webshop_id, ?string $api_key): boo
111100 private function credentialsEmpty (?string $ webshop_id , ?string $ api_key ): bool {
112101 return !trim ($ webshop_id ) || !trim ($ api_key );
113102 }
103+
104+ private function returnResponseCode (int $ code , string $ message ): void {
105+ http_response_code ($ code );
106+ die ($ message );
107+ }
114108}
0 commit comments