From f5b47f5fae47d876bbb4475ed650be2cadecef1b Mon Sep 17 00:00:00 2001 From: Alfreds Genkins Date: Tue, 7 Jan 2020 12:10:44 +0200 Subject: [PATCH] Performance optimization (#7) * WIP - product load optimization. * Implemented processor in the different space --- composer.json | 3 +- src/Model/Resolver/Product/ReviewSummary.php | 40 +++---------- .../CollectionPostProcessor/ReviewSummary.php | 57 +++++++++++++++++++ src/etc/di.xml | 22 +++++++ 4 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 src/Model/Resolver/Products/CollectionPostProcessor/ReviewSummary.php create mode 100644 src/etc/di.xml diff --git a/composer.json b/composer.json index cafca59..7d9e6e7 100755 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "N/A", "type": "magento2-module", "require": { - "magento/magento2-base": "^2.3.1" + "magento/magento2-base": "^2.3.1", + "scandipwa/performance": "^1.0" }, "support": { "source": "https://github.com/scandipwa/reviews-graphql", diff --git a/src/Model/Resolver/Product/ReviewSummary.php b/src/Model/Resolver/Product/ReviewSummary.php index 4a92e44..4966f84 100755 --- a/src/Model/Resolver/Product/ReviewSummary.php +++ b/src/Model/Resolver/Product/ReviewSummary.php @@ -12,7 +12,6 @@ declare(strict_types=1); - namespace ScandiPWA\ReviewsGraphQl\Model\Resolver\Product; use Magento\Catalog\Model\Product; @@ -20,8 +19,6 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Review\Model\ReviewFactory; -use Magento\Store\Model\StoreManagerInterface; /** * Class GetProductReviews @@ -30,30 +27,6 @@ */ class ReviewSummary implements ResolverInterface { - /** - * @var ReviewFactory - */ - protected $reviewFactory; - - /** - * @var StoreManagerInterface - */ - protected $storeManager; - - /** - * ReviewSummary constructor. - * - * @param ReviewFactory $reviewFactory - * @param StoreManagerInterface $storeManager - */ - public function __construct( - ReviewFactory $reviewFactory, - StoreManagerInterface $storeManager - ) { - $this->reviewFactory = $reviewFactory; - $this->storeManager = $storeManager; - } - /** * @inheritdoc */ @@ -70,14 +43,15 @@ public function resolve( /** @var Product $product */ $product = $value['model']; - $storeId = $this->storeManager->getStore()->getId(); - $this->reviewFactory->create()->getEntitySummary($product, $storeId); - $ratingSummary = $product->getRatingSummary()->getRatingSummary(); - $reviewCount = $product->getRatingSummary()->getReviewsCount(); + $ratingSummary = $product->getRatingSummary(); + + if (!$ratingSummary) { + return []; + } return [ - 'rating_summary' => $ratingSummary, - 'review_count' => $reviewCount + 'rating_summary' => $ratingSummary->getRatingSummary(), + 'review_count' => $ratingSummary->getReviewsCount() ]; } } diff --git a/src/Model/Resolver/Products/CollectionPostProcessor/ReviewSummary.php b/src/Model/Resolver/Products/CollectionPostProcessor/ReviewSummary.php new file mode 100644 index 0000000..c7db573 --- /dev/null +++ b/src/Model/Resolver/Products/CollectionPostProcessor/ReviewSummary.php @@ -0,0 +1,57 @@ +review = $review; + } + + /** + * @inheritDoc + * @throws NoSuchEntityException + */ + public function process( + Collection $collection, + array $attributeNames + ): Collection { + if (in_array(self::REVIEW_SUMMARY, $attributeNames)) { + /** @var $collection ProductCollection */ + $this->review->appendSummary($collection); + } + + return $collection; + } +} diff --git a/src/etc/di.xml b/src/etc/di.xml new file mode 100644 index 0000000..6864773 --- /dev/null +++ b/src/etc/di.xml @@ -0,0 +1,22 @@ + + + + + + + ScandiPWA\ReviewsGraphQl\Model\Resolver\Products\CollectionPostProcessor\ReviewSummary + + + + \ No newline at end of file