Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context values compat #1

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Compat/WithGraphQLContextValuesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Magento\ReviewGraphQl\Compat;

use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Before https://github.com/magento/graphql-ce/pull/742/ there
* was no way to add extra parameters to the context.
*
* This trait provides a fallback implementation to get values
* now in the context, but for earlier Magento versions.
*/
trait WithGraphQLContextValuesTrait
{
/**
* The store parameter in GraphQL context appeared in 2.3.3
* see Magento\StoreGraphQl\Model\Context\AddStoreInfoToContext
*
* @param ContextInterface $context
* @param StoreManagerInterface $storeManager
* @return StoreInterface
*/
protected function getStore(
ContextInterface $context,
StoreManagerInterface $storeManager
): StoreInterface {
$extension = $context->getExtensionAttributes();
if (method_exists($extension, 'getStore')) {
return $extension->getStore();
}
return $storeManager->getStore();
}

/**
* The isCustomer parameter in GraphQL context appeared in 2.3.3
* see Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext
*
* @param ContextInterface $context
* @return bool
*/
protected function getIsCustomer(ContextInterface $context): bool
{
$extension = $context->getExtensionAttributes();
if (method_exists($extension, 'getIsCustomer')) {
return $extension->getIsCustomer();
}

$userId = $context->getUserId();
return !empty($userId);
}
}
19 changes: 16 additions & 3 deletions Model/Resolver/CreateProductReview.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\ReviewGraphQl\Model\Resolver;
Expand All @@ -16,15 +18,19 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Review\Helper\Data as ReviewHelper;
use Magento\Review\Model\Review\Config as ReviewsConfig;
use Magento\ReviewGraphQl\Compat\WithGraphQLContextValuesTrait;
use Magento\ReviewGraphQl\Mapper\ReviewDataMapper;
use Magento\ReviewGraphQl\Model\Review\AddReviewToProduct;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Create product review resolver
*/
class CreateProductReview implements ResolverInterface
{
use WithGraphQLContextValuesTrait;

/**
* @var ReviewHelper
*/
Expand All @@ -45,6 +51,11 @@ class CreateProductReview implements ResolverInterface
*/
private $reviewsConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param AddReviewToProduct $addReviewToProduct
* @param ReviewDataMapper $reviewDataMapper
Expand All @@ -55,13 +66,15 @@ public function __construct(
AddReviewToProduct $addReviewToProduct,
ReviewDataMapper $reviewDataMapper,
ReviewHelper $reviewHelper,
ReviewsConfig $reviewsConfig
ReviewsConfig $reviewsConfig,
StoreManagerInterface $storeManager
) {

$this->addReviewToProduct = $addReviewToProduct;
$this->reviewDataMapper = $reviewDataMapper;
$this->reviewHelper = $reviewHelper;
$this->reviewsConfig = $reviewsConfig;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -94,7 +107,7 @@ public function resolve(
$input = $args['input'];
$customerId = null;

if (false !== $context->getExtensionAttributes()->getIsCustomer()) {
if (false !== $this->getIsCustomer($context)) {
$customerId = (int) $context->getUserId();
}

Expand All @@ -110,7 +123,7 @@ public function resolve(
'detail' => $input['text'],
];
/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$store = $this->getStore($context, $this->storeManager);
$review = $this->addReviewToProduct->execute($data, $ratings, $sku, $customerId, (int) $store->getId());

return ['review' => $this->reviewDataMapper->map($review)];
Expand Down
7 changes: 6 additions & 1 deletion Model/Resolver/Customer/Reviews.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\ReviewGraphQl\Model\Resolver\Customer;
Expand All @@ -14,6 +16,7 @@
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\ReviewGraphQl\Compat\WithGraphQLContextValuesTrait;
use Magento\ReviewGraphQl\Model\DataProvider\AggregatedReviewsDataProvider;
use Magento\ReviewGraphQl\Model\DataProvider\CustomerReviewsDataProvider;

Expand All @@ -22,6 +25,8 @@
*/
class Reviews implements ResolverInterface
{
use WithGraphQLContextValuesTrait;

/**
* @var CustomerReviewsDataProvider
*/
Expand Down Expand Up @@ -67,7 +72,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
if (false === $this->getIsCustomer($context)) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
18 changes: 16 additions & 2 deletions Model/Resolver/Product/RatingSummary.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\ReviewGraphQl\Model\Resolver\Product;
Expand All @@ -16,13 +18,17 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Review\Model\Review\Config as ReviewsConfig;
use Magento\Review\Model\Review\SummaryFactory;
use Magento\ReviewGraphQl\Compat\WithGraphQLContextValuesTrait;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Average rating for the product
*/
class RatingSummary implements ResolverInterface
{
use WithGraphQLContextValuesTrait;

/**
* @var SummaryFactory
*/
Expand All @@ -33,16 +39,24 @@ class RatingSummary implements ResolverInterface
*/
private $reviewsConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param SummaryFactory $summaryFactory
* @param ReviewsConfig $reviewsConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(
SummaryFactory $summaryFactory,
ReviewsConfig $reviewsConfig
ReviewsConfig $reviewsConfig,
StoreManagerInterface $storeManager
) {
$this->summaryFactory = $summaryFactory;
$this->reviewsConfig = $reviewsConfig;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -76,7 +90,7 @@ public function resolve(
}

/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$store = $this->getStore($context, $this->storeManager);

/** @var Product $product */
$product = $value['model'];
Expand Down
19 changes: 16 additions & 3 deletions Model/Resolver/ProductReviewRatingsMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
use Magento\Review\Model\ResourceModel\Rating\CollectionFactory;
use Magento\Review\Model\Review;
use Magento\Review\Model\Review\Config as ReviewsConfig;
use Magento\ReviewGraphQl\Compat\WithGraphQLContextValuesTrait;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Resolve data review rating metadata
*/
class ProductReviewRatingsMetadata implements ResolverInterface
{
use WithGraphQLContextValuesTrait;

/**
* @var CollectionFactory
*/
Expand All @@ -35,14 +39,23 @@ class ProductReviewRatingsMetadata implements ResolverInterface
*/
private $reviewsConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param CollectionFactory $ratingCollectionFactory
* @param ReviewsConfig $reviewsConfig
*/
public function __construct(CollectionFactory $ratingCollectionFactory, ReviewsConfig $reviewsConfig)
{
public function __construct(
CollectionFactory $ratingCollectionFactory,
ReviewsConfig $reviewsConfig,
StoreManagerInterface $storeManager
) {
$this->ratingCollectionFactory = $ratingCollectionFactory;
$this->reviewsConfig = $reviewsConfig;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -71,7 +84,7 @@ public function resolve(

$items = [];
/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$store = $this->getStore($context, $this->storeManager);

/** @var RatingCollection $ratingCollection */
$ratingCollection = $this->ratingCollectionFactory->create();
Expand Down