Skip to content

Commit

Permalink
Performance optimization (#7)
Browse files Browse the repository at this point in the history
* WIP - product load optimization.

* Implemented processor in the different space
  • Loading branch information
alfredsgenkins authored Jan 7, 2020
1 parent 625ae54 commit f5b47f5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 34 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 7 additions & 33 deletions src/Model/Resolver/Product/ReviewSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@

declare(strict_types=1);


namespace ScandiPWA\ReviewsGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\LocalizedException;
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
Expand All @@ -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
*/
Expand All @@ -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()
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ('OSL') v. 3.0)
* @package scandipwa/reviews-graphql
* @link https://github.com/scandipwa/reviews-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\ReviewsGraphQl\Model\Resolver\Products\CollectionPostProcessor;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection;
use Magento\Review\Model\Review;
use ScandiPWA\Performance\Api\ProductsCollectionPostProcessorInterface;

class ReviewSummary implements ProductsCollectionPostProcessorInterface
{
const REVIEW_SUMMARY = 'review_summary';

/**
* @var Review
*/
protected $review;

/**
* AttributeProcessor constructor.
* @param Review $review
*/
public function __construct(
Review $review
) {
$this->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;
}
}
22 changes: 22 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!--
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/reviews-graphql
* @link https://github.com/scandipwa/reviews-graphql
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="ScandiPWA\Performance\Model\Resolver\Products\CollectionPostProcessor">
<arguments>
<argument name="processors" xsi:type="array">
<item name="review_summary" xsi:type="object">ScandiPWA\ReviewsGraphQl\Model\Resolver\Products\CollectionPostProcessor\ReviewSummary</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit f5b47f5

Please sign in to comment.