Skip to content

Commit ceed5b1

Browse files
authored
Product reviews fixes (#4)
* Product reviews fixes 1. fix image url 2. add http response codes for debugging * Avoid code repetition * Remove Exception import
1 parent ecd0ee7 commit ceed5b1

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

common/src/Module.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ private function sendSyncUrl(): void {
9898
];
9999
try {
100100
$this->doSendSyncUrl($url, $data);
101-
} catch (Exception $e) {
101+
} catch (\Exception $e) {
102102
PrestaShopLogger::addLog(sprintf('Sending sync URL to Dashboard failed with error %s', $e->getMessage()));
103103
}
104104
}
105105

106106
/**
107-
* @throws Exception
107+
* @throws \Exception
108108
*/
109109
private function doSendSyncUrl(string $url, array $data): void {
110110
$curl = curl_init();
@@ -119,12 +119,12 @@ private function doSendSyncUrl(string $url, array $data): void {
119119
CURLOPT_TIMEOUT => 10,
120120
];
121121
if (!curl_setopt_array($curl, $options)) {
122-
throw new Exception('Could not set cURL options');
122+
throw new \Exception('Could not set cURL options');
123123
}
124124

125125
$response = curl_exec($curl);
126126
if ($response === false) {
127-
throw new Exception(sprintf('(%s) %s', curl_errno($curl), curl_error($curl)));
127+
throw new \Exception(sprintf('(%s) %s', curl_errno($curl), curl_error($curl)));
128128
}
129129

130130
curl_close($curl);
@@ -466,7 +466,7 @@ private function getProductImage($product): string {
466466
$context = Context::getContext();
467467
$img = $product->getCover($product->id);
468468

469-
return str_replace('http://', Tools::getShopProtocol(), $context->link->getImageLink($img['link_rewrite'], $img['id_image'], 'large_default'));
469+
return str_replace('http://', Tools::getShopProtocol(), $context->link->getImageLink($product->link_rewrite, $img['id_image'], 'home_default'));
470470
}
471471

472472
public function hookBackofficeTop() {

common/src/Sync.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,47 @@
66
use Customer;
77
use DateTime;
88
use Doctrine\ORM\EntityManagerInterface;
9-
use Exception;
109
use ModuleFrontController;
1110
use PrestaShop\Module\ProductComment\Entity\ProductComment;
1211
use PrestaShop\PrestaShop\Adapter\Validate;
1312
use Product;
14-
use Symfony\Component\HttpKernel\Exception\HttpException;
1513

1614
class 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

Comments
 (0)