Skip to content

Commit

Permalink
Merge pull request #6 from ThomasDaSilva/main
Browse files Browse the repository at this point in the history
CS Fixer + fix import/export API + Add site and product rating + route
  • Loading branch information
zawaze authored Feb 21, 2024
2 parents 2d96924 + 4fa069a commit 89b85b4
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 117 deletions.
20 changes: 15 additions & 5 deletions Api/GuaranteedOpinionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

namespace GuaranteedOpinion\Api;

use Exception;
use GuaranteedOpinion\GuaranteedOpinion;
use JsonException;
use RuntimeException;

/**
* Class GuaranteedOpinionClient
Expand All @@ -31,11 +34,12 @@ class GuaranteedOpinionClient
*
* @param string $scope 'site' or productId
* @return array
* @throws \JsonException
* @throws JsonException
* @throws RuntimeException
*/
public function getReviewsFromApi(string $scope = 'site'): array
{
$url = self::URL_API . "/" . self::URL_API_REVIEW . "/" . GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW) . "/" . $scope;
$url = self::URL_API . "/" . self::URL_API_REVIEW . "/" . GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_REVIEW_CONFIG_KEY) . "/" . $scope;

$ch = curl_init($url);

Expand All @@ -45,18 +49,24 @@ public function getReviewsFromApi(string $scope = 'site'): array

curl_close($ch);

return json_decode($response, false, 512, JSON_THROW_ON_ERROR)->reviews;
$jsonResponse = json_decode($response, true, 512, JSON_THROW_ON_ERROR);

if (isset($jsonResponse['data']) && $jsonResponse['data'] !== 200) {
throw new RuntimeException($jsonResponse['message']);
}

return $jsonResponse;
}

/**
* @throws \JsonException
* @throws JsonException
*/
public function sendOrder($jsonOrder)
{
$url = self::URL_API . "/" . self::URL_API_ORDER;

$request = [
'api_key' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_ORDER),
'api_key' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::API_ORDER_CONFIG_KEY),
'orders' => $jsonOrder
];

Expand Down
8 changes: 5 additions & 3 deletions Command/GetProductReviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
$output->write("Product Review synchronization start \n");
foreach ($products as $product)
{
$productReviews = $this->client->getReviewsFromApi($product->getId());
$apiResponse = $this->client->getReviewsFromApi($product->getId());

if ($productReviews !== []) {
foreach ($productReviews as $productRow) {
if ($apiResponse['reviews'] !== []) {
$this->productReviewService->addGuaranteedOpinionProductRating($product->getId(), $apiResponse['ratings']);

foreach ($apiResponse['reviews'] as $productRow) {
if ($rowsTreated % 100 === 0) {
$output->write("Rows treated : " . $rowsTreated . "\n");
}
Expand Down
9 changes: 8 additions & 1 deletion Command/GetSiteReviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use GuaranteedOpinion\Api\GuaranteedOpinionClient;
use GuaranteedOpinion\GuaranteedOpinion;
use GuaranteedOpinion\Service\SiteReviewService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -30,7 +31,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
$siteReviewsAdded = 0;

try {
$siteReviews = $this->client->getReviewsFromApi();
$apiResponse = $this->client->getReviewsFromApi();

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_RATING_TOTAL_CONFIG_KEY, $apiResponse['ratings']['total']);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_RATING_AVERAGE_CONFIG_KEY, $apiResponse['ratings']['average']);

$siteReviews = $apiResponse['reviews'];

$output->write("Site Review synchronization start \n");

Expand All @@ -44,6 +50,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
}
} catch (Exception $exception) {
$output->write($exception->getMessage());
return 0;
}

$output->write("End of Site Review synchronization\n");
Expand Down
14 changes: 14 additions & 0 deletions Config/TheliaMain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,19 @@ CREATE TABLE `guaranteed_opinion_site_review`
UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_product_rating
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_product_rating`;

CREATE TABLE `guaranteed_opinion_product_rating`
(
`product_id` INTEGER NOT NULL,
`total` INTEGER,
`average` VARCHAR(255),
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.3</version>
<version>1.0.4</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
6 changes: 6 additions & 0 deletions Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@
</unique>
</table>

<table name="guaranteed_opinion_product_rating" namespace="GuaranteedOpinion\Model">
<column name="product_id" type="INTEGER" primaryKey="true" required="true"/>
<column name="total" type="INTEGER" />
<column name="average" type="VARCHAR" />
</table>

<external-schema filename="local/config/schema.xml" referenceOnly="true" />
</database>
18 changes: 18 additions & 0 deletions Config/update/1.0.4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_product_rating
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_product_rating`;

CREATE TABLE `guaranteed_opinion_product_rating`
(
`product_id` INTEGER NOT NULL,
`total` INTEGER,
`average` VARCHAR(255),
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
79 changes: 79 additions & 0 deletions Controller/FrontController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace GuaranteedOpinion\Controller;

use GuaranteedOpinion\GuaranteedOpinion;
use GuaranteedOpinion\Model\GuaranteedOpinionProductRatingQuery;
use GuaranteedOpinion\Model\GuaranteedOpinionProductReviewQuery;
use GuaranteedOpinion\Model\GuaranteedOpinionSiteReviewQuery;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\JsonResponse;

#[Route(path: "/guaranteed_opinion", name: "guaranteed_opinion")]
class FrontController extends BaseFrontController
{
/**
* @throws PropelException
*/
#[Route(path: "/site_reviews/offset/{offset}/limit/{limit}", name: "site_reviews", methods: "GET")]
public function siteReviews(int $offset, int $limit): JsonResponse
{
$reviews = [];

$siteReviews = GuaranteedOpinionSiteReviewQuery::create()
->setLimit($limit)
->setOffset($limit * ($offset -1))
->find()
;

foreach ($siteReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

return new JsonResponse([
'total' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_TOTAL_CONFIG_KEY),
'average' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_RATING_AVERAGE_CONFIG_KEY),
'reviews' => $reviews
]);
}

/**
* @throws PropelException
*/
#[Route(path: "/product_reviews/{id}/offset/{offset}/limit/{limit}", name: "product_reviews", methods: "GET")]
public function productReviews(int $id, int $offset, int $limit): JsonResponse
{
$reviews = [];

$productRating = GuaranteedOpinionProductRatingQuery::create()
->findOneByProductId($id);

$productReviews = GuaranteedOpinionProductReviewQuery::create()
->filterByProductId($id)
->setLimit($limit)
->setOffset($limit * ($offset -1))
->find();

foreach ($productReviews as $review) {
$reviews[] = [
'rate' => $review->getRate(),
'name' => $review->getName(),
'date' => $review->getReviewDate()?->format('Y-m-d'),
'message' => $review->getReview()
];
}

return new JsonResponse([
'total' => $productRating?->getTotal(),
'average' => $productRating?->getAverage(),
'reviews' => $reviews
]);
}
}
26 changes: 13 additions & 13 deletions Controller/GuaranteedOpinionConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ public function saveConfiguration(ParserContext $parserContext) : RedirectRespon
try {
$data = $this->validateForm($form)->getData();

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW, $data["api_key_review"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::CONFIG_API_ORDER, $data["api_key_order"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SHOW_RATING_URL, $data["show_rating_url"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::API_REVIEW_CONFIG_KEY, $data["api_key_review"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::API_ORDER_CONFIG_KEY, $data["api_key_order"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SHOW_RATING_URL_CONFIG_KEY, $data["show_rating_url"]);

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STORE_URL, $data["store_url"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT, implode(',', $data["status_to_export"]));
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::EMAIL_DELAY, $data["email_delay"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STORE_URL_CONFIG_KEY, $data["store_url"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY, implode(',', $data["status_to_export"]));
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::EMAIL_DELAY_CONFIG_KEY, $data["email_delay"]);

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY, $data["site_review_hook_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY, (bool)$data["site_review_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY_CONFIG_KEY, $data["site_review_hook_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_DISPLAY_CONFIG_KEY, (bool)$data["site_review_display"]);

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY, $data["product_review_hook_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY, (bool)$data["product_review_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY, (bool)$data["product_review_tab_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY_CONFIG_KEY, $data["product_review_hook_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_DISPLAY_CONFIG_KEY, (bool)$data["product_review_display"]);
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_TAB_DISPLAY_CONFIG_KEY, (bool)$data["product_review_tab_display"]);

GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET, htmlspecialchars($data["site_review_widget"]));
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME, htmlspecialchars($data["site_review_widget_iframe"]));
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_CONFIG_KEY, htmlspecialchars($data["site_review_widget"]));
GuaranteedOpinion::setConfigValue(GuaranteedOpinion::SITE_REVIEW_WIDGET_IFRAME_CONFIG_KEY, htmlspecialchars($data["site_review_widget_iframe"]));

return $this->generateSuccessRedirect($form);

Expand Down
6 changes: 3 additions & 3 deletions EventListeners/OrderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GuaranteedOpinion\EventListeners;

use DateTime;
use GuaranteedOpinion\GuaranteedOpinion;
use GuaranteedOpinion\Model\GuaranteedOpinionOrderQueue;
use GuaranteedOpinion\Model\GuaranteedOpinionOrderQueueQuery;
Expand All @@ -11,7 +12,6 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\OrderStatusQuery;

class OrderListener implements EventSubscriberInterface
{
Expand All @@ -32,7 +32,7 @@ public function sendOrderToQueue(OrderEvent $orderEvent): void
return;
}

$orderStatuses = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT));
$orderStatuses = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY));

foreach ($orderStatuses as $orderStatus)
{
Expand All @@ -42,7 +42,7 @@ public function sendOrderToQueue(OrderEvent $orderEvent): void

$guaranteedOpinionOrderQueue
->setOrderId($orderEvent->getOrder()->getId())
->setTreatedAt(new \DateTime(''))
->setTreatedAt(new DateTime(''))
->setStatus($status)
;

Expand Down
Loading

0 comments on commit 89b85b4

Please # to comment.