From f513b8dd30cef984898d525513de2b65fbec997e Mon Sep 17 00:00:00 2001 From: Pierre Martin Date: Wed, 4 Nov 2020 14:47:24 +0100 Subject: [PATCH 1/2] feat(compat): compatibility to get store for pre 2.3.3 versions --- Compat/WithGraphQLContextValuesTrait.php | 32 +++++++++++++++++++ Model/Resolver/CreateProductReview.php | 17 ++++++++-- Model/Resolver/Product/RatingSummary.php | 18 +++++++++-- .../Resolver/ProductReviewRatingsMetadata.php | 19 +++++++++-- 4 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 Compat/WithGraphQLContextValuesTrait.php diff --git a/Compat/WithGraphQLContextValuesTrait.php b/Compat/WithGraphQLContextValuesTrait.php new file mode 100644 index 0000000..b92e845 --- /dev/null +++ b/Compat/WithGraphQLContextValuesTrait.php @@ -0,0 +1,32 @@ +getExtensionAttributes(); + if (method_exists($extension, 'getStore')) { + return $extension->getStore(); + } + return $storeManager->getStore(); + } +} diff --git a/Model/Resolver/CreateProductReview.php b/Model/Resolver/CreateProductReview.php index 9b0171c..9b05835 100644 --- a/Model/Resolver/CreateProductReview.php +++ b/Model/Resolver/CreateProductReview.php @@ -1,8 +1,10 @@ addReviewToProduct = $addReviewToProduct; $this->reviewDataMapper = $reviewDataMapper; $this->reviewHelper = $reviewHelper; $this->reviewsConfig = $reviewsConfig; + $this->storeManager = $storeManager; } /** @@ -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)]; diff --git a/Model/Resolver/Product/RatingSummary.php b/Model/Resolver/Product/RatingSummary.php index eed5034..283527d 100644 --- a/Model/Resolver/Product/RatingSummary.php +++ b/Model/Resolver/Product/RatingSummary.php @@ -1,8 +1,10 @@ summaryFactory = $summaryFactory; $this->reviewsConfig = $reviewsConfig; + $this->storeManager = $storeManager; } /** @@ -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']; diff --git a/Model/Resolver/ProductReviewRatingsMetadata.php b/Model/Resolver/ProductReviewRatingsMetadata.php index 015c15c..fe39db4 100644 --- a/Model/Resolver/ProductReviewRatingsMetadata.php +++ b/Model/Resolver/ProductReviewRatingsMetadata.php @@ -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 */ @@ -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; } /** @@ -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(); From 5a7be1f3a11c60142665835f4417bf146bf1bef5 Mon Sep 17 00:00:00 2001 From: Pierre Martin Date: Wed, 4 Nov 2020 15:14:42 +0100 Subject: [PATCH 2/2] feat(compat): compatibility to get isCustomer for pre 2.3.3 versions --- Compat/WithGraphQLContextValuesTrait.php | 30 ++++++++++++++++++++---- Model/Resolver/CreateProductReview.php | 2 +- Model/Resolver/Customer/Reviews.php | 7 +++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Compat/WithGraphQLContextValuesTrait.php b/Compat/WithGraphQLContextValuesTrait.php index b92e845..505ccd8 100644 --- a/Compat/WithGraphQLContextValuesTrait.php +++ b/Compat/WithGraphQLContextValuesTrait.php @@ -6,14 +6,18 @@ 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 - * and 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 for earlier Magento versions. + * see Magento\StoreGraphQl\Model\Context\AddStoreInfoToContext * * @param ContextInterface $context * @param StoreManagerInterface $storeManager @@ -29,4 +33,22 @@ protected function 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); + } } diff --git a/Model/Resolver/CreateProductReview.php b/Model/Resolver/CreateProductReview.php index 9b05835..1c27e2a 100644 --- a/Model/Resolver/CreateProductReview.php +++ b/Model/Resolver/CreateProductReview.php @@ -107,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(); } diff --git a/Model/Resolver/Customer/Reviews.php b/Model/Resolver/Customer/Reviews.php index 8c0bca6..4ec9519 100644 --- a/Model/Resolver/Customer/Reviews.php +++ b/Model/Resolver/Customer/Reviews.php @@ -1,8 +1,10 @@ getExtensionAttributes()->getIsCustomer()) { + if (false === $this->getIsCustomer($context)) { throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); }