From 2806d48437a7504bb0fcf2890717016c8c1b1949 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 8 Jul 2020 14:22:18 +0300 Subject: [PATCH 01/45] Add DB schema --- app/code/Magento/Catalog/etc/db_schema.xml | 25 +++++++++++++++++++ .../Catalog/etc/db_schema_whitelist.json | 23 ++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index a0aa48fb76b13..bde69cbb5d1ff 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -547,6 +547,8 @@ default="0" comment="Product ID"/> + @@ -558,6 +560,8 @@ referenceColumn="entity_id" onDelete="CASCADE"/> + @@ -573,6 +577,27 @@ + + + + + + + + + + + + + + + +
Date: Wed, 8 Jul 2020 18:58:11 +0300 Subject: [PATCH 02/45] add list_id during comparing products --- .../Magento/Catalog/Model/CompareList.php | 24 +++ .../Model/Product/Compare/AddToList.php | 137 ++++++++++++++++++ .../Model/Product/Compare/ListCompare.php | 10 ++ .../Model/ResourceModel/CompareList.php | 23 +++ 4 files changed, 194 insertions(+) create mode 100644 app/code/Magento/Catalog/Model/CompareList.php create mode 100644 app/code/Magento/Catalog/Model/Product/Compare/AddToList.php create mode 100644 app/code/Magento/Catalog/Model/ResourceModel/CompareList.php diff --git a/app/code/Magento/Catalog/Model/CompareList.php b/app/code/Magento/Catalog/Model/CompareList.php new file mode 100644 index 0000000000000..363266d0ce233 --- /dev/null +++ b/app/code/Magento/Catalog/Model/CompareList.php @@ -0,0 +1,24 @@ +_init(\Magento\Catalog\Model\ResourceModel\CompareList::class); + } +} diff --git a/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php b/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php new file mode 100644 index 0000000000000..5331f92b02ee7 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php @@ -0,0 +1,137 @@ +compareListFactory = $compareListFactory; + $this->compareList = $compareList; + $this->compareListResource = $compareListResource; + $this->customerSession = $customerSession; + $this->customerVisitor = $customerVisitor; + } + + /** + * Get list_id + * + * @return int + */ + public function execute() + { + if ($this->customerSession->isLoggedIn()) { + return $this->getListIdByCustomerId(); + } + + return $this->getListIdByVisitorId(); + } + + /** + * Get list_id for visitor + * + * @return int + */ + private function getListIdByVisitorId() + { + + $visitorId = $this->customerVisitor->getId(); + $compareListModel = $this->compareListFactory->create(); + $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); + if ($compareListId = $compareListModel->getId()) { + return (int)$compareListId; + } + + return $this->createCompareList($visitorId, null); + } + + /** + * Get list_id for logged customers + * + * @return int + */ + private function getListIdByCustomerId() + { + $customerId = $this->customerSession->getCustomerId(); + $compareListModel = $this->compareListFactory->create(); + $this->compareListResource->load($compareListModel, $customerId, 'customer_id'); + + if ($compareListId = $compareListModel->getId()) { + return (int)$compareListId; + } + + return $this->createCompareList(0, $customerId); + } + + /** + * Create new compare list + * + * @param $visitorId + * @param $customerId + * + * @return int + */ + private function createCompareList($visitorId, $customerId) + { + /* @var $compareList CompareList */ + $compareList = $this->compareListFactory->create(); + $compareList->setVisitorId($visitorId); + $compareList->setCustomerId($customerId); + $compareList->save(); + + return (int)$compareList->getId(); + } +} diff --git a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php index 9ccb86441812c..71015c87832bd 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php @@ -60,6 +60,11 @@ class ListCompare extends \Magento\Framework\DataObject */ private $productRepository; + /** + * @var AddToList + */ + private $addToCompareList; + /** * Constructor * @@ -68,6 +73,7 @@ class ListCompare extends \Magento\Framework\DataObject * @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Customer\Model\Visitor $customerVisitor + * @param \Magento\Catalog\Model\Product\Compare\AddToList * @param array $data * @param ProductRepository|null $productRepository */ @@ -77,6 +83,7 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor, + \Magento\Catalog\Model\Product\Compare\AddToList $addToList, array $data = [], ProductRepository $productRepository = null ) { @@ -85,6 +92,7 @@ public function __construct( $this->_catalogProductCompareItem = $catalogProductCompareItem; $this->_customerSession = $customerSession; $this->_customerVisitor = $customerVisitor; + $this->addToCompareList = $addToList; $this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class); parent::__construct($data); } @@ -102,9 +110,11 @@ public function addProduct($product) $item = $this->_compareItemFactory->create(); $this->_addVisitorToItem($item); $item->loadByProduct($product); + $listId = $this->addToCompareList->execute(); if (!$item->getId() && $this->productExists($product)) { $item->addProductData($product); + $item->setListId($listId); $item->save(); } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php b/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php new file mode 100644 index 0000000000000..c7ff5e4963214 --- /dev/null +++ b/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php @@ -0,0 +1,23 @@ +_init('catalog_compare_list', 'list_id'); + } +} From 2fe99e93914d4b2c2a548c97ef34dca391f64ddb Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Thu, 16 Jul 2020 15:37:02 +0300 Subject: [PATCH 03/45] set customer for compare list during login --- .../Model/Product/Compare/AddToList.php | 19 ++++++++++++++- .../ResourceModel/Product/Compare/Item.php | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php b/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php index 5331f92b02ee7..686e3d4edd89a 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php @@ -80,6 +80,24 @@ public function execute() return $this->getListIdByVisitorId(); } + /** + * Set customer from visitor + */ + public function setCustomerFromVisitor() + { + $customerId = $this->customerSession->getCustomerId(); + + if (!$customerId) { + return $this; + } + + $visitorId = $this->customerVisitor->getId(); + $compareListModel = $this->compareListFactory->create(); + $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); + $compareListModel->setCustomerId($customerId); + $compareListModel->save(); + } + /** * Get list_id for visitor * @@ -87,7 +105,6 @@ public function execute() */ private function getListIdByVisitorId() { - $visitorId = $this->customerVisitor->getId(); $compareListModel = $this->compareListFactory->create(); $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php index 7eb0552e355fc..60b3f5c71c92f 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php @@ -5,6 +5,9 @@ */ namespace Magento\Catalog\Model\ResourceModel\Product\Compare; +use Magento\Catalog\Model\Product\Compare\AddToList; +use Magento\Framework\Model\ResourceModel\Db\Context; + /** * Catalog compare item resource model * @@ -12,6 +15,11 @@ */ class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { + /** + * @var AddToList + */ + private $compareList; + /** * Initialize connection * @@ -22,6 +30,20 @@ protected function _construct() $this->_init('catalog_compare_item', 'catalog_compare_item_id'); } + /** + * @param AddToList $addToList + * @param Context $context + * @param null $connectionName + */ + public function __construct( + AddToList $addToList, + Context $context, + $connectionName = null + ) { + $this->compareList = $addToList; + parent::__construct($context, $connectionName); + } + /** * Load object by product * @@ -213,6 +235,7 @@ public function updateCustomerFromVisitor($object) $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $itemId) ); } + $this->compareList->setCustomerFromVisitor(); } return $this; From 1fd3c5b6fd372fa9196e4175ac7d04175b93e4eb Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 12 Aug 2020 17:22:50 +0300 Subject: [PATCH 04/45] changes based on review --- .../{AddToList.php => CompareList.php} | 76 ++++--------------- .../Model/Product/Compare/ListCompare.php | 12 +-- .../ResourceModel/Product/Compare/Item.php | 47 ++++++++++-- 3 files changed, 62 insertions(+), 73 deletions(-) rename app/code/Magento/Catalog/Model/Product/Compare/{AddToList.php => CompareList.php} (55%) diff --git a/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php b/app/code/Magento/Catalog/Model/Product/Compare/CompareList.php similarity index 55% rename from app/code/Magento/Catalog/Model/Product/Compare/AddToList.php rename to app/code/Magento/Catalog/Model/Product/Compare/CompareList.php index 686e3d4edd89a..5fab205bbdabe 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/AddToList.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/CompareList.php @@ -8,104 +8,59 @@ namespace Magento\Catalog\Model\Product\Compare; -use Magento\Catalog\Model\CompareList; +use Magento\Catalog\Model\CompareList as CatalogCompareList; use Magento\Catalog\Model\CompareListFactory; use Magento\Catalog\Model\ResourceModel\CompareList as CompareListResource; -use Magento\Customer\Model\Session; -use Magento\Customer\Model\Visitor; -class AddToList +class CompareList { /** * @var CompareListFactory */ private $compareListFactory; - /** - * @var CompareList - */ - private $compareList; - /** * @var CompareListResource */ private $compareListResource; - /** - * Customer session - * - * @var Session - */ - private $customerSession; - - /** - * Customer visitor - * - * @var Visitor - */ - private $customerVisitor; - /** * @param CompareListFactory $compareListFactory - * @param CompareList $compareList * @param CompareListResource $compareListResource - * @param Session $customerSession - * @param Visitor $customerVisitor */ public function __construct( CompareListFactory $compareListFactory, - CompareList $compareList, - CompareListResource $compareListResource, - Session $customerSession, - Visitor $customerVisitor + CompareListResource $compareListResource ) { $this->compareListFactory = $compareListFactory; - $this->compareList = $compareList; $this->compareListResource = $compareListResource; - $this->customerSession = $customerSession; - $this->customerVisitor = $customerVisitor; } /** * Get list_id * + * @param Item $item + * * @return int */ - public function execute() - { - if ($this->customerSession->isLoggedIn()) { - return $this->getListIdByCustomerId(); - } - - return $this->getListIdByVisitorId(); - } - - /** - * Set customer from visitor - */ - public function setCustomerFromVisitor() + public function getListId(Item $item) { - $customerId = $this->customerSession->getCustomerId(); - - if (!$customerId) { - return $this; + if ($customerId = $item->getCustomerId()) { + return $this->getListIdByCustomerId($customerId); } - $visitorId = $this->customerVisitor->getId(); - $compareListModel = $this->compareListFactory->create(); - $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); - $compareListModel->setCustomerId($customerId); - $compareListModel->save(); + return $this->getListIdByVisitorId($item->getVisitorId()); } /** * Get list_id for visitor * + * @param $visitorId + * * @return int */ - private function getListIdByVisitorId() + private function getListIdByVisitorId($visitorId) { - $visitorId = $this->customerVisitor->getId(); $compareListModel = $this->compareListFactory->create(); $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); if ($compareListId = $compareListModel->getId()) { @@ -118,11 +73,12 @@ private function getListIdByVisitorId() /** * Get list_id for logged customers * + * @param $customerId + * * @return int */ - private function getListIdByCustomerId() + private function getListIdByCustomerId($customerId) { - $customerId = $this->customerSession->getCustomerId(); $compareListModel = $this->compareListFactory->create(); $this->compareListResource->load($compareListModel, $customerId, 'customer_id'); @@ -143,7 +99,7 @@ private function getListIdByCustomerId() */ private function createCompareList($visitorId, $customerId) { - /* @var $compareList CompareList */ + /* @var $compareList CatalogCompareList */ $compareList = $this->compareListFactory->create(); $compareList->setVisitorId($visitorId); $compareList->setCustomerId($customerId); diff --git a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php index 71015c87832bd..7c2ad694132b9 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php @@ -61,9 +61,9 @@ class ListCompare extends \Magento\Framework\DataObject private $productRepository; /** - * @var AddToList + * @var CompareList */ - private $addToCompareList; + private $compareList; /** * Constructor @@ -73,7 +73,7 @@ class ListCompare extends \Magento\Framework\DataObject * @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Customer\Model\Visitor $customerVisitor - * @param \Magento\Catalog\Model\Product\Compare\AddToList + * @param \Magento\Catalog\Model\Product\Compare\CompareList * @param array $data * @param ProductRepository|null $productRepository */ @@ -83,7 +83,7 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor, - \Magento\Catalog\Model\Product\Compare\AddToList $addToList, + \Magento\Catalog\Model\Product\Compare\CompareList $addToList, array $data = [], ProductRepository $productRepository = null ) { @@ -92,7 +92,7 @@ public function __construct( $this->_catalogProductCompareItem = $catalogProductCompareItem; $this->_customerSession = $customerSession; $this->_customerVisitor = $customerVisitor; - $this->addToCompareList = $addToList; + $this->compareList = $addToList; $this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class); parent::__construct($data); } @@ -110,7 +110,7 @@ public function addProduct($product) $item = $this->_compareItemFactory->create(); $this->_addVisitorToItem($item); $item->loadByProduct($product); - $listId = $this->addToCompareList->execute(); + $listId = $this->compareList->getListId($item); if (!$item->getId() && $this->productExists($product)) { $item->addProductData($product); diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php index 60b3f5c71c92f..e6ef21e2c8448 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php @@ -5,7 +5,8 @@ */ namespace Magento\Catalog\Model\ResourceModel\Product\Compare; -use Magento\Catalog\Model\Product\Compare\AddToList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\ResourceModel\CompareList as CompareListResource; use Magento\Framework\Model\ResourceModel\Db\Context; /** @@ -16,9 +17,14 @@ class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { /** - * @var AddToList + * @var CompareListFactory */ - private $compareList; + private $compareListFactory; + + /** + * @var CompareListResource + */ + private $compareListResource; /** * Initialize connection @@ -31,16 +37,19 @@ protected function _construct() } /** - * @param AddToList $addToList + * @param CompareListFactory $compareListFactory + * @param CompareListResource $compareListResource * @param Context $context * @param null $connectionName */ public function __construct( - AddToList $addToList, + CompareListFactory $compareListFactory, + CompareListResource $compareListResource, Context $context, $connectionName = null ) { - $this->compareList = $addToList; + $this->compareListFactory = $compareListFactory; + $this->compareListResource = $compareListResource; parent::__construct($context, $connectionName); } @@ -235,7 +244,7 @@ public function updateCustomerFromVisitor($object) $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $itemId) ); } - $this->compareList->setCustomerFromVisitor(); + $this->setCustomerFromVisitor($object); } return $this; @@ -265,4 +274,28 @@ public function clearItems($visitorId = null, $customerId = null) $this->getConnection()->delete($this->getMainTable(), $where); return $this; } + + /** + * Set customer from visitor in catalog_compare_list table + * + * @param \Magento\Catalog\Model\Product\Compare\Item $item + * + * @return $this + */ + private function setCustomerFromVisitor($item) + { + $customerId = $item->getCustomerId(); + + if (!$customerId) { + return $this; + } + + $visitorId = $item->getVisitorId(); + $compareListModel = $this->compareListFactory->create(); + $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); + $compareListModel->setCustomerId($customerId); + $compareListModel->save(); + + return $this; + } } From 330c2001bfcd60422c46508eab7c4218f7fa9f92 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 12 Aug 2020 17:48:37 +0300 Subject: [PATCH 05/45] started working on compared list graphql modules --- .../Model/Resolver/AddItemsToCompareList.php | 40 +++ .../Resolver/AssignCompareListToCustomer.php | 39 +++ .../Model/Resolver/CompareList.php | 41 +++ .../Model/Resolver/CustomerCompareList.php | 252 ++++++++++++++++++ .../Resolver/RemoveItemsFromCompareList.php | 40 +++ .../Magento/CompareListGraphQl/composer.json | 24 ++ .../Magento/CompareListGraphQl/etc/module.xml | 18 ++ .../CompareListGraphQl/etc/schema.graphqls | 37 +++ .../CompareListGraphQl/registration.php | 14 + 9 files changed, 505 insertions(+) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php create mode 100644 app/code/Magento/CompareListGraphQl/composer.json create mode 100644 app/code/Magento/CompareListGraphQl/etc/module.xml create mode 100644 app/code/Magento/CompareListGraphQl/etc/schema.graphqls create mode 100644 app/code/Magento/CompareListGraphQl/registration.php diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php new file mode 100644 index 0000000000000..279119b29a93b --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php @@ -0,0 +1,40 @@ +itemCollectionFactory = $itemCollectionFactory; + $this->catalogProductVisibility = $catalogProductVisibility; + $this->catalogConfig = $catalogConfig; + $this->compareProduct = $compareHelper; + $this->priceProviderPool = $priceProviderPool; + $this->discount = $discount; + } + + /** + * Get customer compare list + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return Value|mixed|void + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + + return [ + 'list_id' => 1, + 'items' => $this->getComparableItems($context, $store), + 'attributes' => $this->getComparableAttributes($context) + ]; + } + + /** + * Get comparable items + * + * @param ContextInterface $context + * @param StoreInterface $store + * + * @return array + */ + private function getComparableItems(ContextInterface $context, StoreInterface $store) + { + $items = []; + foreach ($this->getCollectionComparableItems($context) as $item) { + /** @var Product $item */ + $items[] = [ + 'productId' => $item->getId(), + 'name' => $item->getName(), + 'sku' => $item->getSku(), + 'priceRange' => [ + 'minimum_price' => $this->getMinimumProductPrice($item, $store), + 'maximum_price' => $this->getMinimumProductPrice($item, $store) + ], + 'canonical_url' => $item->getUrlKey(), + 'images' => [ + 'url' => [ + 'model' => $item, + 'image_type' => 'image', + 'label' => $item->getImageLabel() + ], + ], + ]; + } + + return $items; + } + + /** + * Get comparable attributes + * + * @param ContextInterface $context + * + * @return array + */ + private function getComparableAttributes(ContextInterface $context): array + { + $attributes = []; + $itemsCollection = $this->getCollectionComparableItems($context); + foreach ($itemsCollection->getComparableAttributes() as $item) { + $attributes[] = [ + 'code' => $item->getAttributeCode(), + 'title' => $item->getStoreLabel() + ]; + } + + return $attributes; + } + + /** + * Get collection of comparable items + * + * @param ContextInterface $context + * + * @return Collection + */ + private function getCollectionComparableItems(ContextInterface $context): Collection + { + $this->compareProduct->setAllowUsedFlat(false); + /** @var Collection $comparableItems */ + $this->items = $this->itemCollectionFactory->create(); + $this->items->setCustomerId($context->getUserId()); + $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); + + $this->items->addAttributeToSelect( + $this->catalogConfig->getProductAttributes() + )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility( + $this->catalogProductVisibility->getVisibleInSiteIds() + ); + + return $this->items; + } + + /** + * Get formatted minimum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Get formatted maximum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Format price for GraphQl output + * + * @param float $regularPrice + * @param float $finalPrice + * @param StoreInterface $store + * + * @return array + */ + private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array + { + return [ + 'regular_price' => [ + 'value' => $regularPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'final_price' => [ + 'value' => $finalPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), + ]; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php new file mode 100644 index 0000000000000..e82f0fb0c9cdd --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php @@ -0,0 +1,40 @@ + + + + + + + + + + + + diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls new file mode 100644 index 0000000000000..1a44629b482b3 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -0,0 +1,37 @@ +# Copyright © Magento, Inc. All rights reserved. +# See COPYING.txt for license details. + +type ComparableItem { + productId: ID! @doc(description: "Product Id") + name: String! @doc(description: "Product name") + sku: String! @doc(description: "Product SKU") + priceRange: PriceRange! @doc(description: "Product prices") + canonical_url: String @doc(description: "Product URL") + images: [ProductImage]! @doc(description: "Product Images") + # values: [ProductAttribute]! @doc(description: "Product comparable attributes") +} + +type ComparableAttribute { + code: String! @doc(description: "Attribute code") + title: String! @doc(description: "Addibute display title") +} + +type CompareList { + list_id: ID! @doc(description: "Compare list id") + items: [ComparableItem] @doc(description: "Comparable products") + attributes: [ComparableAttribute] @doc(description: "Comparable attributes, provides codes and titles for the attributes") +} + +type Customer { + compare_list: CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CustomerCompareList") @doc(description: "Active customers compare list") +} + +type Query { + compareList(id: ID!): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CompareList") @doc(description: "Compare list") +} + +type Mutation { + addItemsToCompareList(id: ID!, items: [ID!]): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AddItemsToCompareList") @doc(description: "Add items to compare list") + removeItemsFromCompareList(id: ID!, items: [ID!]): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\RemoveItemsFromCompareList") @doc(description: "Remove items from compare list") + assignCompareListToCustomer(customerId: ID!, listId: ID!): Boolean @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign compare list to customer") +} diff --git a/app/code/Magento/CompareListGraphQl/registration.php b/app/code/Magento/CompareListGraphQl/registration.php new file mode 100644 index 0000000000000..bb764b439273d --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/registration.php @@ -0,0 +1,14 @@ + Date: Mon, 17 Aug 2020 16:08:09 +0300 Subject: [PATCH 06/45] added resolver for getting compare list by id --- .../Product/Compare/Item/Collection.php | 35 +++ .../Model/Resolver/CompareList.php | 57 +++- .../Model/Resolver/CustomerCompareList.php | 220 +++----------- .../Model/Service/CompareListService.php | 272 ++++++++++++++++++ .../CompareListGraphQl/etc/schema.graphqls | 7 +- 5 files changed, 401 insertions(+), 190 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 92741cf9ba88e..dca3f76af6a04 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -31,6 +31,13 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection */ protected $_visitorId = 0; + /** + * List Id Filter + * + * @var int + */ + protected $listId = 0; + /** * Comparable attributes cache * @@ -156,6 +163,30 @@ public function setCustomerId($customerId) return $this; } + /** + * Set listId filter to collection + * + * @param $listId + * + * @return $this + */ + public function setListId($listId) + { + $this->listId = (int)$listId; + $this->_addJoinToSelect(); + return $this; + } + + /** + * Retrieve listId filter applied to collection + * + * @return int + */ + public function getListId() + { + return $this->listId; + } + /** * Set visitor filter to collection * @@ -204,6 +235,10 @@ public function getConditionForJoin() return ['visitor_id' => $this->getVisitorId()]; } + if ($this->getListId()) { + return ['list_id' => $this->getListId()]; + } + return ['customer_id' => ['null' => true], 'visitor_id' => '0']; } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index a2e5d06084b14..ffe5cb1fbfc05 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -7,26 +7,57 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\CompareListGraphQl\Model\Service\CompareListService; +use Magento\Catalog\Model\CompareList as ModelCompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Store\Api\Data\StoreInterface; class CompareList implements ResolverInterface { + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var ResourceCompareList + */ + private $resourceCompareList; + + /** + * @var CompareListService + */ + private $compareListService; + + public function __construct( + CompareListFactory $compareListFactory, + ResourceCompareList $resourceCompareList, + CompareListService $compareListService + ) { + $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + $this->compareListService = $compareListService; + } + /** * Get compare list * - * @param Field $field + * @param Field $field * @param ContextInterface $context - * @param ResolveInfo $info - * @param array|null $value - * @param array|null $args + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args * * @return array|Value|mixed * * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * */ public function resolve( Field $field, @@ -35,7 +66,21 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + /** @var $compareListModel ModelCompareList*/ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $args['id']); + $listId = (int)$compareListModel->getId(); + + if (!$listId) { + return null; + } + + return [ + 'list_id' => $listId, + 'items' => $this->compareListService->getComparableItems($listId, $context, $store), + 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) + ]; } } - diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php index ccc1b1ad7b3ad..b8d22e02678de 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php @@ -7,81 +7,47 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Helper\Product\Compare; -use Magento\Catalog\Model\Config as CatalogConfig; -use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\Product\Visibility as CatalogProductVisibility; -use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; -use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; +use Magento\Catalog\Model\CompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\CompareListGraphQl\Model\Service\CompareListService; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Framework\Pricing\SaleableInterface; use Magento\Store\Api\Data\StoreInterface; class CustomerCompareList implements ResolverInterface { /** - * @var Collection + * @var ResourceCompareList */ - private $items; + private $resourceCompareList; /** - * @var CompareItemsCollectionFactory + * @var CompareListFactory */ - private $itemCollectionFactory; + private $compareListFactory; /** - * @var CatalogProductVisibility + * @var CompareListService */ - private $catalogProductVisibility; + private $compareListService; /** - * @var CatalogConfig - */ - private $catalogConfig; - - /** - * @var Compare - */ - private $compareProduct; - - /** - * @var Discount - */ - private $discount; - - /** - * @var PriceProviderPool - */ - private $priceProviderPool; - - /** - * @param CompareItemsCollectionFactory $itemCollectionFactory - * @param CatalogProductVisibility $catalogProductVisibility - * @param CatalogConfig $catalogConfig - * @param Compare $compareHelper - * @param PriceProviderPool $priceProviderPool - * @param Discount $discount + * @param ResourceCompareList $resourceCompareList + * @param CompareListFactory $compareListFactory + * @param CompareListService $compareListService */ public function __construct( - CompareItemsCollectionFactory $itemCollectionFactory, - CatalogProductVisibility $catalogProductVisibility, - CatalogConfig $catalogConfig, - Compare $compareHelper, - PriceProviderPool $priceProviderPool, - Discount $discount + ResourceCompareList $resourceCompareList, + CompareListFactory $compareListFactory, + CompareListService $compareListService ) { - $this->itemCollectionFactory = $itemCollectionFactory; - $this->catalogProductVisibility = $catalogProductVisibility; - $this->catalogConfig = $catalogConfig; - $this->compareProduct = $compareHelper; - $this->priceProviderPool = $priceProviderPool; - $this->discount = $discount; + $this->resourceCompareList = $resourceCompareList; + $this->compareListFactory = $compareListFactory; + $this->compareListService = $compareListService; } /** @@ -106,147 +72,35 @@ public function resolve( ) { /** @var StoreInterface $store */ $store = $context->getExtensionAttributes()->getStore(); + $listId = (int)$this->getListIdByCustomerId($context->getUserId()); - return [ - 'list_id' => 1, - 'items' => $this->getComparableItems($context, $store), - 'attributes' => $this->getComparableAttributes($context) - ]; - } - - /** - * Get comparable items - * - * @param ContextInterface $context - * @param StoreInterface $store - * - * @return array - */ - private function getComparableItems(ContextInterface $context, StoreInterface $store) - { - $items = []; - foreach ($this->getCollectionComparableItems($context) as $item) { - /** @var Product $item */ - $items[] = [ - 'productId' => $item->getId(), - 'name' => $item->getName(), - 'sku' => $item->getSku(), - 'priceRange' => [ - 'minimum_price' => $this->getMinimumProductPrice($item, $store), - 'maximum_price' => $this->getMinimumProductPrice($item, $store) - ], - 'canonical_url' => $item->getUrlKey(), - 'images' => [ - 'url' => [ - 'model' => $item, - 'image_type' => 'image', - 'label' => $item->getImageLabel() - ], - ], - ]; + if (!$listId) { + return null; } - return $items; + return [ + 'list_id' => $listId, + 'items' => $this->compareListService->getComparableItems($listId, $context, $store), + 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) + ]; } /** - * Get comparable attributes + * Get listId by Customer ID * - * @param ContextInterface $context + * @param $customerId * - * @return array + * @return int|null */ - private function getComparableAttributes(ContextInterface $context): array + private function getListIdByCustomerId($customerId) { - $attributes = []; - $itemsCollection = $this->getCollectionComparableItems($context); - foreach ($itemsCollection->getComparableAttributes() as $item) { - $attributes[] = [ - 'code' => $item->getAttributeCode(), - 'title' => $item->getStoreLabel() - ]; + if ($customerId) { + /** @var CompareList $compareListModel */ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $customerId, 'customer_id'); + return (int)$compareListModel->getId(); } - return $attributes; - } - - /** - * Get collection of comparable items - * - * @param ContextInterface $context - * - * @return Collection - */ - private function getCollectionComparableItems(ContextInterface $context): Collection - { - $this->compareProduct->setAllowUsedFlat(false); - /** @var Collection $comparableItems */ - $this->items = $this->itemCollectionFactory->create(); - $this->items->setCustomerId($context->getUserId()); - $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); - - $this->items->addAttributeToSelect( - $this->catalogConfig->getProductAttributes() - )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility( - $this->catalogProductVisibility->getVisibleInSiteIds() - ); - - return $this->items; - } - - /** - * Get formatted minimum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Get formatted maximum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Format price for GraphQl output - * - * @param float $regularPrice - * @param float $finalPrice - * @param StoreInterface $store - * - * @return array - */ - private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array - { - return [ - 'regular_price' => [ - 'value' => $regularPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'final_price' => [ - 'value' => $finalPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), - ]; + return null; } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php new file mode 100644 index 0000000000000..970003a70ff8c --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php @@ -0,0 +1,272 @@ +itemCollectionFactory = $itemCollectionFactory; + $this->catalogProductVisibility = $catalogProductVisibility; + $this->catalogConfig = $catalogConfig; + $this->compareProduct = $compareHelper; + $this->priceProviderPool = $priceProviderPool; + $this->discount = $discount; + $this->resourceCompareList = $resourceCompareList; + $this->modelCompareList = $compareList; + $this->blockListCompare = $listCompare; + } + + /** + * Get comparable items + * + * @param int $listId + * @param ContextInterface $context + * @param StoreInterface $store + * + * @return array + */ + public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) + { + $items = []; + foreach ($this->getCollectionComparableItems($listId, $context) as $item) { + /** @var Product $item */ + $items[] = [ + 'productId' => $item->getId(), + 'name' => $item->getName(), + 'sku' => $item->getSku(), + 'priceRange' => [ + 'minimum_price' => $this->getMinimumProductPrice($item, $store), + 'maximum_price' => $this->getMaximumProductPrice($item, $store) + ], + 'canonical_url' => $item->getUrlKey(), + 'images' => [ + 'url' => [ + 'model' => $item, + 'image_type' => 'image', + 'label' => $item->getImageLabel() + ], + ], + 'values' => $this->getProductComparableAttributes($listId, $item, $context) + ]; + } + + return $items; + } + + /** + * Get comparable attributes + * + * @param int $listId + * @param ContextInterface $context + * + * @return array + */ + public function getComparableAttributes(int $listId, ContextInterface $context): array + { + $attributes = []; + $itemsCollection = $this->getCollectionComparableItems($listId, $context); + foreach ($itemsCollection->getComparableAttributes() as $item) { + $attributes[] = [ + 'code' => $item->getAttributeCode(), + 'title' => $item->getStoreLabel() + ]; + } + + return $attributes; + } + + /** + * Get comparable attributes for product + * + * @param int $listId + * @param Product $product + * @param ContextInterface $context + * + * @return array + */ + private function getProductComparableAttributes(int $listId, Product $product, ContextInterface $context): array + { + $attributes = []; + $itemsCollection = $this->getCollectionComparableItems($listId, $context); + foreach ($itemsCollection->getComparableAttributes() as $item) { + $attributes[] = [ + 'code' => $item->getAttributeCode(), + 'value' => $this->blockListCompare->getProductAttributeValue($product, $item) + ]; + } + + return $attributes; + } + + /** + * Get formatted minimum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Get formatted maximum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Format price for GraphQl output + * + * @param float $regularPrice + * @param float $finalPrice + * @param StoreInterface $store + * + * @return array + */ + private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array + { + return [ + 'regular_price' => [ + 'value' => $regularPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'final_price' => [ + 'value' => $finalPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), + ]; + } + + /** + * Get collection of comparable items + * + * @param int $listId + * @param ContextInterface $context + * + * @return Collection + */ + private function getCollectionComparableItems(int $listId, ContextInterface $context): Collection + { + $this->compareProduct->setAllowUsedFlat(false); + /** @var Collection $comparableItems */ + $this->items = $this->itemCollectionFactory->create(); + $this->items->setListId($listId); + $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); + $this->items->addAttributeToSelect( + $this->catalogConfig->getProductAttributes() + )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility( + $this->catalogProductVisibility->getVisibleInSiteIds() + ); + + return $this->items; + } +} diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 1a44629b482b3..6131aa9ec1135 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -8,7 +8,12 @@ type ComparableItem { priceRange: PriceRange! @doc(description: "Product prices") canonical_url: String @doc(description: "Product URL") images: [ProductImage]! @doc(description: "Product Images") - # values: [ProductAttribute]! @doc(description: "Product comparable attributes") + values: [ProductAttribute]! @doc(description: "Product comparable attributes") +} + +type ProductAttribute { + code: String! @doc(description:"Attribute code") + value: String! @doc(description:"Addibute display value") } type ComparableAttribute { From d24e5446b9b6b4bb00b989a854ec27cd5482bc1e Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 17 Aug 2020 20:17:13 +0300 Subject: [PATCH 07/45] add remove and assign functionality --- .../Model/Product/Compare/ListCompare.php | 6 +- .../Model/Resolver/AddItemsToCompareList.php | 127 ++++++++++++++++- .../Resolver/AssignCompareListToCustomer.php | 133 +++++++++++++++++- .../Model/Resolver/CompareList.php | 5 + .../Resolver/RemoveItemsFromCompareList.php | 86 ++++++++++- 5 files changed, 337 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php index 7c2ad694132b9..356c8e701b21e 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php @@ -73,7 +73,7 @@ class ListCompare extends \Magento\Framework\DataObject * @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Customer\Model\Visitor $customerVisitor - * @param \Magento\Catalog\Model\Product\Compare\CompareList + * @param \Magento\Catalog\Model\Product\Compare\CompareList $compareList * @param array $data * @param ProductRepository|null $productRepository */ @@ -83,7 +83,7 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor, - \Magento\Catalog\Model\Product\Compare\CompareList $addToList, + \Magento\Catalog\Model\Product\Compare\CompareList $compareList, array $data = [], ProductRepository $productRepository = null ) { @@ -92,7 +92,7 @@ public function __construct( $this->_catalogProductCompareItem = $catalogProductCompareItem; $this->_customerSession = $customerSession; $this->_customerVisitor = $customerVisitor; - $this->compareList = $addToList; + $this->compareList = $compareList; $this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class); parent::__construct($data); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php index 279119b29a93b..7b8a00ebf724d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php @@ -7,24 +7,94 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\CompareList as ModelCompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\Product; +use Magento\Catalog\Model\Product\Compare\CompareList; +use Magento\Catalog\Model\Product\Compare\Item; +use Magento\Catalog\Model\Product\Compare\ItemFactory; +use Magento\Catalog\Model\ProductRepository; +use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\CompareListGraphQl\Model\Service\CompareListService; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Store\Api\Data\StoreInterface; class AddItemsToCompareList implements ResolverInterface { + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var ResourceCompareList + */ + private $resourceCompareList; + + /** + * @var CompareListService + */ + private $compareListService; + + /** + * Compare item factory + * + * @var ItemFactory + */ + private $compareItemFactory; + + /** + * @var CompareList + */ + private $compareList; + + /** + * @var ProductRepository + */ + private $productRepository; + + /** + * @param CompareListFactory $compareListFactory + * @param ResourceCompareList $resourceCompareList + * @param CompareListService $compareListService + * @param ItemFactory $compareItemFactory + * @param CompareList $compareList + * @param ProductRepository $productRepository + */ + public function __construct( + CompareListFactory $compareListFactory, + ResourceCompareList $resourceCompareList, + CompareListService $compareListService, + ItemFactory $compareItemFactory, + CompareList $compareList, + ProductRepository $productRepository + ) { + $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + $this->compareListService = $compareListService; + $this->compareItemFactory = $compareItemFactory; + $this->compareList = $compareList; + $this->productRepository = $productRepository; + } + /** * Add items to compare list * - * @param Field $field + * @param Field $field * @param ContextInterface $context - * @param ResolveInfo $info - * @param array|null $value - * @param array|null $args + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args * * @return Value|mixed|void + * + * @throws GraphQlInputException */ public function resolve( Field $field, @@ -33,8 +103,53 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + $listId = (int)$args['id']; + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + /** @var $compareListModel ModelCompareList*/ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $args['id']); + + if (!$compareListModel->getId()) { + throw new GraphQlInputException(__('Can\'t load compare list.')); + } + + foreach ($args['items'] as $key) { + /* @var $item Item */ + $item = $this->compareItemFactory->create(); + $item->loadByProduct($key); + if (!$item->getId() && $this->productExists($key)) { + $item->addProductData($key); + $item->setListId($listId); + $item->save(); + } + } + + return [ + 'list_id' => $listId, + 'items' => $this->compareListService->getComparableItems($listId, $context, $store), + 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) + ]; } -} + /** + * Check product exists. + * + * @param int|Product $product + * + * @return bool + */ + private function productExists($product) + { + if ($product instanceof Product && $product->getId()) { + return true; + } + try { + $product = $this->productRepository->getById((int)$product); + return !empty($product->getId()); + } catch (NoSuchEntityException $e) { + return false; + } + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index ff459d11f1b16..131fd553aa514 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -7,7 +7,19 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\CompareList as ModelCompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\Customer\Api\AccountManagementInterface; +use Magento\Customer\Api\CustomerRepositoryInterface; +use Magento\Customer\Api\Data\CustomerInterface; +use Magento\Customer\Model\AuthenticationInterface; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -15,16 +27,64 @@ class AssignCompareListToCustomer implements ResolverInterface { + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var ResourceCompareList + */ + private $resourceCompareList; + + /** + * @var AuthenticationInterface + */ + private $authentication; + + /** + * @var CustomerRepositoryInterface + */ + private $customerRepository; + + /** + * @var AccountManagementInterface + */ + private $accountManagement; + + /** + * @param CompareListFactory $compareListFactory + * @param ResourceCompareList $resourceCompareList + * @param AuthenticationInterface $authentication + * @param CustomerRepositoryInterface $customerRepository + * @param AccountManagementInterface $accountManagement + */ + public function __construct( + CompareListFactory $compareListFactory, + ResourceCompareList $resourceCompareList, + AuthenticationInterface $authentication, + CustomerRepositoryInterface $customerRepository, + AccountManagementInterface $accountManagement + ) { + $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + $this->authentication = $authentication; + $this->customerRepository = $customerRepository; + $this->accountManagement = $accountManagement; + } + /** * Assign compare list to customer * - * @param Field $field + * @param Field $field * @param ContextInterface $context - * @param ResolveInfo $info - * @param array|null $value - * @param array|null $args + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args * * @return Value|mixed|void + * + * @throws GraphQlInputException */ public function resolve( Field $field, @@ -33,7 +93,68 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + if (!isset($args['customerId'])) { + throw new GraphQlInputException(__('"customerId" value must be specified')); + } + + if (!isset($args['listId'])) { + throw new GraphQlInputException(__('"listId" value must be specified')); + } + + $customer = $this->validateCustomer($args['customerId']); + + /** @var $compareListModel ModelCompareList*/ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $args['listId']); + + if (!$compareListModel->getId()) { + return false; + } + + $compareListModel->setCustomerId($customer->getId()); + $this->resourceCompareList->save($compareListModel); + + return true; } -} + /** + * Customer validate + * + * @param $customerId + * + * @return CustomerInterface + * + * @throws GraphQlAuthenticationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + private function validateCustomer($customerId) + { + try { + $customer = $this->customerRepository->getById($customerId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException( + __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), + $e + ); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if (true === $this->authentication->isLocked($customerId)) { + throw new GraphQlAuthenticationException(__('The account is locked.')); + } + + try { + $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { + throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); + } + + return $customer; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index ffe5cb1fbfc05..502fb305153df 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -35,6 +35,11 @@ class CompareList implements ResolverInterface */ private $compareListService; + /** + * @param CompareListFactory $compareListFactory + * @param ResourceCompareList $resourceCompareList + * @param CompareListService $compareListService + */ public function __construct( CompareListFactory $compareListFactory, ResourceCompareList $resourceCompareList, diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php index e82f0fb0c9cdd..f1a3536dbdb5e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php @@ -7,26 +7,76 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\CompareList as ModelCompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\Product\Compare\Item; +use Magento\Catalog\Model\Product\Compare\ItemFactory; +use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\CompareListGraphQl\Model\Service\CompareListService; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Store\Api\Data\StoreInterface; class RemoveItemsFromCompareList implements ResolverInterface { + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var ResourceCompareList + */ + private $resourceCompareList; + + /** + * @var CompareListService + */ + private $compareListService; + + /** + * Compare item factory + * + * @var ItemFactory + */ + private $compareItemFactory; + + /** + * @param CompareListFactory $compareListFactory + * @param ResourceCompareList $resourceCompareList + * @param CompareListService $compareListService + * @param ItemFactory $compareItemFactory, + */ + public function __construct( + CompareListFactory $compareListFactory, + ResourceCompareList $resourceCompareList, + CompareListService $compareListService, + ItemFactory $compareItemFactory + ) { + $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + $this->compareListService = $compareListService; + $this->compareItemFactory = $compareItemFactory; + } + /** * Remove items from compare list * - * @param Field $field + * @param Field $field * @param ContextInterface $context - * @param ResolveInfo $info - * @param array|null $value - * @param array|null $args + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args * * @return Value|mixed|void * * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * + * @throws GraphQlInputException */ public function resolve( Field $field, @@ -35,6 +85,32 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + $listId = (int)$args['id']; + /** @var StoreInterface $store */ + $store = $context->getExtensionAttributes()->getStore(); + /** @var $compareListModel ModelCompareList*/ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $args['id']); + + if (!$compareListModel->getId()) { + throw new GraphQlInputException(__('Can\'t load compare list.')); + } + + foreach ($args['items'] as $key) { + /* @var $item Item */ + $item = $this->compareItemFactory->create(); + $item->setListId($listId); + $item->loadByProduct($key); + + if ($item->getId()) { + $item->delete(); + } + } + + return [ + 'list_id' => $listId, + 'items' => $this->compareListService->getComparableItems($listId, $context, $store), + 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) + ]; } } From a9ea92a82eabeab252adfb0e3d1e3fbe9fcae6da Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 21 Aug 2020 15:07:06 +0300 Subject: [PATCH 08/45] separated some logic --- .../Model/Resolver/AddItemsToCompareList.php | 61 ++---------- .../Resolver/AssignCompareListToCustomer.php | 81 ++-------------- .../Model/Service/AddToCompareListService.php | 85 +++++++++++++++++ .../Model/Service/CustomerService.php | 95 +++++++++++++++++++ 4 files changed, 200 insertions(+), 122 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareListService.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php index 7b8a00ebf724d..51a48de49c686 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php @@ -9,14 +9,10 @@ use Magento\Catalog\Model\CompareList as ModelCompareList; use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product\Compare\CompareList; -use Magento\Catalog\Model\Product\Compare\Item; -use Magento\Catalog\Model\Product\Compare\ItemFactory; -use Magento\Catalog\Model\ProductRepository; use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\CompareListGraphQl\Model\Service\AddToCompareListService; use Magento\CompareListGraphQl\Model\Service\CompareListService; -use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -25,6 +21,9 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Store\Api\Data\StoreInterface; +/** + * Class add products to compare list + */ class AddItemsToCompareList implements ResolverInterface { /** @@ -42,45 +41,35 @@ class AddItemsToCompareList implements ResolverInterface */ private $compareListService; - /** - * Compare item factory - * - * @var ItemFactory - */ - private $compareItemFactory; - /** * @var CompareList */ private $compareList; /** - * @var ProductRepository + * @var AddToCompareListService */ - private $productRepository; + private $addToCompareListService; /** * @param CompareListFactory $compareListFactory * @param ResourceCompareList $resourceCompareList * @param CompareListService $compareListService - * @param ItemFactory $compareItemFactory * @param CompareList $compareList - * @param ProductRepository $productRepository + * @param AddToCompareListService $addToCompareListService */ public function __construct( CompareListFactory $compareListFactory, ResourceCompareList $resourceCompareList, CompareListService $compareListService, - ItemFactory $compareItemFactory, CompareList $compareList, - ProductRepository $productRepository + AddToCompareListService $addToCompareListService ) { $this->compareListFactory = $compareListFactory; $this->resourceCompareList = $resourceCompareList; $this->compareListService = $compareListService; - $this->compareItemFactory = $compareItemFactory; $this->compareList = $compareList; - $this->productRepository = $productRepository; + $this->addToCompareListService = $addToCompareListService; } /** @@ -114,16 +103,7 @@ public function resolve( throw new GraphQlInputException(__('Can\'t load compare list.')); } - foreach ($args['items'] as $key) { - /* @var $item Item */ - $item = $this->compareItemFactory->create(); - $item->loadByProduct($key); - if (!$item->getId() && $this->productExists($key)) { - $item->addProductData($key); - $item->setListId($listId); - $item->save(); - } - } + $this->addToCompareListService->addToCompareList($listId, $args); return [ 'list_id' => $listId, @@ -131,25 +111,4 @@ public function resolve( 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) ]; } - - /** - * Check product exists. - * - * @param int|Product $product - * - * @return bool - */ - private function productExists($product) - { - if ($product instanceof Product && $product->getId()) { - return true; - } - - try { - $product = $this->productRepository->getById((int)$product); - return !empty($product->getId()); - } catch (NoSuchEntityException $e) { - return false; - } - } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index 131fd553aa514..ed4eb724ea14c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -10,21 +10,17 @@ use Magento\Catalog\Model\CompareList as ModelCompareList; use Magento\Catalog\Model\CompareListFactory; use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\Customer\Api\AccountManagementInterface; -use Magento\Customer\Api\CustomerRepositoryInterface; -use Magento\Customer\Api\Data\CustomerInterface; -use Magento\Customer\Model\AuthenticationInterface; -use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Exception\NoSuchEntityException; +use Magento\CompareListGraphQl\Model\Service\CustomerService; use Magento\Framework\GraphQl\Config\Element\Field; -use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +/** + * Class Assign Customer to CompareList + */ class AssignCompareListToCustomer implements ResolverInterface { /** @@ -38,39 +34,23 @@ class AssignCompareListToCustomer implements ResolverInterface private $resourceCompareList; /** - * @var AuthenticationInterface - */ - private $authentication; - - /** - * @var CustomerRepositoryInterface - */ - private $customerRepository; - - /** - * @var AccountManagementInterface + * @var CustomerService */ - private $accountManagement; + private $customerService; /** * @param CompareListFactory $compareListFactory * @param ResourceCompareList $resourceCompareList - * @param AuthenticationInterface $authentication - * @param CustomerRepositoryInterface $customerRepository - * @param AccountManagementInterface $accountManagement + * @param CustomerService $customerService */ public function __construct( CompareListFactory $compareListFactory, ResourceCompareList $resourceCompareList, - AuthenticationInterface $authentication, - CustomerRepositoryInterface $customerRepository, - AccountManagementInterface $accountManagement + CustomerService $customerService ) { $this->compareListFactory = $compareListFactory; $this->resourceCompareList = $resourceCompareList; - $this->authentication = $authentication; - $this->customerRepository = $customerRepository; - $this->accountManagement = $accountManagement; + $this->customerService = $customerService; } /** @@ -101,7 +81,7 @@ public function resolve( throw new GraphQlInputException(__('"listId" value must be specified')); } - $customer = $this->validateCustomer($args['customerId']); + $customer = $this->customerService->validateCustomer($args['customerId']); /** @var $compareListModel ModelCompareList*/ $compareListModel = $this->compareListFactory->create(); @@ -116,45 +96,4 @@ public function resolve( return true; } - - /** - * Customer validate - * - * @param $customerId - * - * @return CustomerInterface - * - * @throws GraphQlAuthenticationException - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException - */ - private function validateCustomer($customerId) - { - try { - $customer = $this->customerRepository->getById($customerId); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException( - __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), - $e - ); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if (true === $this->authentication->isLocked($customerId)) { - throw new GraphQlAuthenticationException(__('The account is locked.')); - } - - try { - $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } - - if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { - throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); - } - - return $customer; - } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareListService.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareListService.php new file mode 100644 index 0000000000000..06945d8acff7b --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareListService.php @@ -0,0 +1,85 @@ +compareItemFactory = $compareItemFactory; + $this->productRepository = $productRepository; + } + + /** + * Add to compare list + * + * @param int $listId + * @param array $items + */ + public function addToCompareList(int $listId, array $items) + { + foreach ($items['items'] as $key) { + /* @var $item Item */ + $item = $this->compareItemFactory->create(); + $item->loadByProduct($key); + if (!$item->getId() && $this->productExists($key)) { + $item->addProductData($key); + $item->setListId($listId); + $item->save(); + } + } + } + + /** + * Check product exists. + * + * @param int|Product $product + * + * @return bool + */ + private function productExists($product) + { + if ($product instanceof Product && $product->getId()) { + return true; + } + + try { + $product = $this->productRepository->getById((int)$product); + return !empty($product->getId()); + } catch (NoSuchEntityException $e) { + return false; + } + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php new file mode 100644 index 0000000000000..0eb05c3720eec --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php @@ -0,0 +1,95 @@ +authentication = $authentication; + $this->customerRepository = $customerRepository; + $this->accountManagement = $accountManagement; + } + + /** + * Customer validate + * + * @param $customerId + * + * @return CustomerInterface + * + * @throws GraphQlAuthenticationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function validateCustomer($customerId) + { + try { + $customer = $this->customerRepository->getById($customerId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException( + __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), + $e + ); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if (true === $this->authentication->isLocked($customerId)) { + throw new GraphQlAuthenticationException(__('The account is locked.')); + } + + try { + $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { + throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); + } + + return $customer; + } +} From 907877bfe035999cecadd8803268d2950c1d17ac Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 21 Aug 2020 16:32:45 +0300 Subject: [PATCH 09/45] have decoupled some resolvers --- .../Model/Resolver/AddItemsToCompareList.php | 26 +- .../Model/Resolver/CompareList.php | 11 +- .../Model/Resolver/CustomerCompareList.php | 15 +- .../Resolver/RemoveItemsFromCompareList.php | 27 +- .../Service/Collection/ComparableItems.php | 88 ++++++ .../Service/ComparableAttributesService.php | 53 ++++ .../Model/Service/ComparableItemsService.php | 196 ++++++++++++++ .../Model/Service/CompareListService.php | 251 ++---------------- 8 files changed, 391 insertions(+), 276 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php index 51a48de49c686..8df7b9f7a221e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php @@ -12,7 +12,6 @@ use Magento\Catalog\Model\Product\Compare\CompareList; use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; use Magento\CompareListGraphQl\Model\Service\AddToCompareListService; -use Magento\CompareListGraphQl\Model\Service\CompareListService; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -20,6 +19,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Store\Api\Data\StoreInterface; +use Magento\CompareListGraphQl\Model\Service\CompareListService; /** * Class add products to compare list @@ -36,11 +36,6 @@ class AddItemsToCompareList implements ResolverInterface */ private $resourceCompareList; - /** - * @var CompareListService - */ - private $compareListService; - /** * @var CompareList */ @@ -51,25 +46,30 @@ class AddItemsToCompareList implements ResolverInterface */ private $addToCompareListService; + /** + * @var CompareListService + */ + private $compareListService; + /** * @param CompareListFactory $compareListFactory * @param ResourceCompareList $resourceCompareList - * @param CompareListService $compareListService * @param CompareList $compareList * @param AddToCompareListService $addToCompareListService + * @param CompareListService $compareListService */ public function __construct( CompareListFactory $compareListFactory, ResourceCompareList $resourceCompareList, - CompareListService $compareListService, CompareList $compareList, - AddToCompareListService $addToCompareListService + AddToCompareListService $addToCompareListService, + CompareListService $compareListService ) { $this->compareListFactory = $compareListFactory; $this->resourceCompareList = $resourceCompareList; - $this->compareListService = $compareListService; $this->compareList = $compareList; $this->addToCompareListService = $addToCompareListService; + $this->compareListService = $compareListService; } /** @@ -105,10 +105,6 @@ public function resolve( $this->addToCompareListService->addToCompareList($listId, $args); - return [ - 'list_id' => $listId, - 'items' => $this->compareListService->getComparableItems($listId, $context, $store), - 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) - ]; + return $this->compareListService->getCompareList($listId, $context, $store); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index 502fb305153df..4edb20ab3d2b9 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -8,16 +8,19 @@ namespace Magento\CompareListGraphQl\Model\Resolver; use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\CompareListGraphQl\Model\Service\CompareListService; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\CompareListGraphQl\Model\Service\CompareListService; use Magento\Catalog\Model\CompareList as ModelCompareList; use Magento\Catalog\Model\CompareListFactory; use Magento\Store\Api\Data\StoreInterface; +/** + * Get compare list + */ class CompareList implements ResolverInterface { /** @@ -82,10 +85,6 @@ public function resolve( return null; } - return [ - 'list_id' => $listId, - 'items' => $this->compareListService->getComparableItems($listId, $context, $store), - 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) - ]; + return $this->compareListService->getCompareList($listId, $context, $store); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php index b8d22e02678de..cd115ecb53ea4 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php @@ -18,6 +18,9 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Store\Api\Data\StoreInterface; +/** + * Get customer compare list + */ class CustomerCompareList implements ResolverInterface { /** @@ -36,9 +39,9 @@ class CustomerCompareList implements ResolverInterface private $compareListService; /** - * @param ResourceCompareList $resourceCompareList - * @param CompareListFactory $compareListFactory - * @param CompareListService $compareListService + * @param ResourceCompareList $resourceCompareList + * @param CompareListFactory $compareListFactory + * @param CompareListService $compareListService */ public function __construct( ResourceCompareList $resourceCompareList, @@ -78,11 +81,7 @@ public function resolve( return null; } - return [ - 'list_id' => $listId, - 'items' => $this->compareListService->getComparableItems($listId, $context, $store), - 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) - ]; + return $this->compareListService->getCompareList($listId, $context, $store); } /** diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php index f1a3536dbdb5e..d750e142e2dae 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveItemsFromCompareList.php @@ -21,6 +21,9 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Store\Api\Data\StoreInterface; +/** + * Remove items from compare list + */ class RemoveItemsFromCompareList implements ResolverInterface { /** @@ -33,11 +36,6 @@ class RemoveItemsFromCompareList implements ResolverInterface */ private $resourceCompareList; - /** - * @var CompareListService - */ - private $compareListService; - /** * Compare item factory * @@ -45,22 +43,27 @@ class RemoveItemsFromCompareList implements ResolverInterface */ private $compareItemFactory; + /** + * @var CompareListService + */ + private $compareListService; + /** * @param CompareListFactory $compareListFactory * @param ResourceCompareList $resourceCompareList + * @param ItemFactory $compareItemFactory * @param CompareListService $compareListService - * @param ItemFactory $compareItemFactory, */ public function __construct( CompareListFactory $compareListFactory, ResourceCompareList $resourceCompareList, - CompareListService $compareListService, - ItemFactory $compareItemFactory + ItemFactory $compareItemFactory, + CompareListService $compareListService ) { $this->compareListFactory = $compareListFactory; $this->resourceCompareList = $resourceCompareList; - $this->compareListService = $compareListService; $this->compareItemFactory = $compareItemFactory; + $this->compareListService = $compareListService; } /** @@ -107,10 +110,6 @@ public function resolve( } } - return [ - 'list_id' => $listId, - 'items' => $this->compareListService->getComparableItems($listId, $context, $store), - 'attributes' => $this->compareListService->getComparableAttributes($listId, $context) - ]; + return $this->compareListService->getCompareList($listId, $context, $store); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php new file mode 100644 index 0000000000000..0e6c41f1c2aab --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php @@ -0,0 +1,88 @@ +itemCollectionFactory = $itemCollectionFactory; + $this->catalogProductVisibility = $catalogProductVisibility; + $this->catalogConfig = $catalogConfig; + $this->compareProduct = $compareHelper; + } + + /** + * Get collection of comparable items + * + * @param int $listId + * @param ContextInterface $context + * + * @return Collection + */ + public function getCollectionComparableItems(int $listId, ContextInterface $context): Collection + { + $this->compareProduct->setAllowUsedFlat(false); + /** @var Collection $comparableItems */ + $this->items = $this->itemCollectionFactory->create(); + $this->items->setListId($listId); + $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); + $this->items->addAttributeToSelect( + $this->catalogConfig->getProductAttributes() + )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility( + $this->catalogProductVisibility->getVisibleInSiteIds() + ); + + return $this->items; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php new file mode 100644 index 0000000000000..152c09f2bbd1f --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php @@ -0,0 +1,53 @@ +comparableItemsCollection = $comparableItemsCollection; + } + + /** + * Get comparable attributes + * + * @param int $listId + * @param ContextInterface $context + * + * @return array + */ + public function getComparableAttributes(int $listId, ContextInterface $context): array + { + $attributes = []; + $itemsCollection = $this->comparableItemsCollection->getCollectionComparableItems($listId, $context); + foreach ($itemsCollection->getComparableAttributes() as $item) { + $attributes[] = [ + 'code' => $item->getAttributeCode(), + 'title' => $item->getStoreLabel() + ]; + } + + return $attributes; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php new file mode 100644 index 0000000000000..e14678b9ca0e3 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php @@ -0,0 +1,196 @@ +priceProviderPool = $priceProviderPool; + $this->discount = $discount; + $this->resourceCompareList = $resourceCompareList; + $this->modelCompareList = $compareList; + $this->blockListCompare = $listCompare; + $this->comparableItemsCollection = $comparableItemsCollection; + } + + /** + * Get comparable items + * + * @param int $listId + * @param ContextInterface $context + * @param StoreInterface $store + * + * @return array + */ + public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) + { + $items = []; + foreach ($this->comparableItemsCollection->getCollectionComparableItems($listId, $context) as $item) { + /** @var Product $item */ + $items[] = [ + 'productId' => $item->getId(), + 'name' => $item->getName(), + 'sku' => $item->getSku(), + 'priceRange' => [ + 'minimum_price' => $this->getMinimumProductPrice($item, $store), + 'maximum_price' => $this->getMaximumProductPrice($item, $store) + ], + 'canonical_url' => $item->getUrlKey(), + 'images' => [ + 'url' => [ + 'model' => $item, + 'image_type' => 'image', + 'label' => $item->getImageLabel() + ], + ], + 'values' => $this->getProductComparableAttributes($listId, $item, $context) + ]; + } + + return $items; + } + + /** + * Get comparable attributes for product + * + * @param int $listId + * @param Product $product + * @param ContextInterface $context + * + * @return array + */ + private function getProductComparableAttributes(int $listId, Product $product, ContextInterface $context): array + { + $attributes = []; + $itemsCollection = $this->comparableItemsCollection->getCollectionComparableItems($listId, $context); + foreach ($itemsCollection->getComparableAttributes() as $item) { + $attributes[] = [ + 'code' => $item->getAttributeCode(), + 'value' => $this->blockListCompare->getProductAttributeValue($product, $item) + ]; + } + + return $attributes; + } + + /** + * Get formatted minimum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Get formatted maximum product price + * + * @param SaleableInterface $product + * @param StoreInterface $store + * + * @return array + */ + private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array + { + $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); + $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); + $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); + return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); + } + + /** + * Format price for GraphQl output + * + * @param float $regularPrice + * @param float $finalPrice + * @param StoreInterface $store + * + * @return array + */ + private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array + { + return [ + 'regular_price' => [ + 'value' => $regularPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'final_price' => [ + 'value' => $finalPrice, + 'currency' => $store->getCurrentCurrencyCode() + ], + 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), + ]; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php index 970003a70ff8c..a11b93a8fed39 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php @@ -7,109 +7,38 @@ namespace Magento\CompareListGraphQl\Model\Service; -use Magento\Catalog\Block\Product\Compare\ListCompare; -use Magento\Catalog\Helper\Product\Compare; -use Magento\Catalog\Model\CompareList; -use Magento\Catalog\Model\Config as CatalogConfig; -use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\Product\Visibility as CatalogProductVisibility; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; -use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory; -use Magento\Framework\Pricing\SaleableInterface; use Magento\Store\Api\Data\StoreInterface; - +/** + * Get compare list + */ class CompareListService { /** - * @var Collection - */ - private $items; - - /** - * @var CompareItemsCollectionFactory - */ - private $itemCollectionFactory; - - /** - * @var CatalogProductVisibility - */ - private $catalogProductVisibility; - - /** - * @var CatalogConfig - */ - private $catalogConfig; - - /** - * @var Compare - */ - private $compareProduct; - - /** - * @var Discount - */ - private $discount; - - /** - * @var PriceProviderPool - */ - private $priceProviderPool; - - /** - * @var ResourceCompareList - */ - private $resourceCompareList; - - /** - * @var CompareList + * @var ComparableItemsService */ - private $modelCompareList; + private $comparableItemsService; /** - * @var ListCompare + * @var ComparableAttributesService */ - private $blockListCompare; + private $comparableAttributesService; /** - * @param CompareItemsCollectionFactory $itemCollectionFactory - * @param CatalogProductVisibility $catalogProductVisibility - * @param CatalogConfig $catalogConfig - * @param Compare $compareHelper - * @param PriceProviderPool $priceProviderPool - * @param Discount $discount - * @param ResourceCompareList $resourceCompareList - * @param CompareList $compareList - * @param ListCompare $listCompare + * @param ComparableItemsService $comparableItemsService + * @param ComparableAttributesService $comparableAttributesService */ public function __construct( - CompareItemsCollectionFactory $itemCollectionFactory, - CatalogProductVisibility $catalogProductVisibility, - CatalogConfig $catalogConfig, - Compare $compareHelper, - PriceProviderPool $priceProviderPool, - Discount $discount, - ResourceCompareList $resourceCompareList, - CompareList $compareList, - ListCompare $listCompare + ComparableItemsService $comparableItemsService, + ComparableAttributesService $comparableAttributesService ) { - $this->itemCollectionFactory = $itemCollectionFactory; - $this->catalogProductVisibility = $catalogProductVisibility; - $this->catalogConfig = $catalogConfig; - $this->compareProduct = $compareHelper; - $this->priceProviderPool = $priceProviderPool; - $this->discount = $discount; - $this->resourceCompareList = $resourceCompareList; - $this->modelCompareList = $compareList; - $this->blockListCompare = $listCompare; + $this->comparableItemsService = $comparableItemsService; + $this->comparableAttributesService = $comparableAttributesService; } /** - * Get comparable items + * Get compare list * * @param int $listId * @param ContextInterface $context @@ -117,156 +46,12 @@ public function __construct( * * @return array */ - public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) - { - $items = []; - foreach ($this->getCollectionComparableItems($listId, $context) as $item) { - /** @var Product $item */ - $items[] = [ - 'productId' => $item->getId(), - 'name' => $item->getName(), - 'sku' => $item->getSku(), - 'priceRange' => [ - 'minimum_price' => $this->getMinimumProductPrice($item, $store), - 'maximum_price' => $this->getMaximumProductPrice($item, $store) - ], - 'canonical_url' => $item->getUrlKey(), - 'images' => [ - 'url' => [ - 'model' => $item, - 'image_type' => 'image', - 'label' => $item->getImageLabel() - ], - ], - 'values' => $this->getProductComparableAttributes($listId, $item, $context) - ]; - } - - return $items; - } - - /** - * Get comparable attributes - * - * @param int $listId - * @param ContextInterface $context - * - * @return array - */ - public function getComparableAttributes(int $listId, ContextInterface $context): array - { - $attributes = []; - $itemsCollection = $this->getCollectionComparableItems($listId, $context); - foreach ($itemsCollection->getComparableAttributes() as $item) { - $attributes[] = [ - 'code' => $item->getAttributeCode(), - 'title' => $item->getStoreLabel() - ]; - } - - return $attributes; - } - - /** - * Get comparable attributes for product - * - * @param int $listId - * @param Product $product - * @param ContextInterface $context - * - * @return array - */ - private function getProductComparableAttributes(int $listId, Product $product, ContextInterface $context): array - { - $attributes = []; - $itemsCollection = $this->getCollectionComparableItems($listId, $context); - foreach ($itemsCollection->getComparableAttributes() as $item) { - $attributes[] = [ - 'code' => $item->getAttributeCode(), - 'value' => $this->blockListCompare->getProductAttributeValue($product, $item) - ]; - } - - return $attributes; - } - - /** - * Get formatted minimum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Get formatted maximum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Format price for GraphQl output - * - * @param float $regularPrice - * @param float $finalPrice - * @param StoreInterface $store - * - * @return array - */ - private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array + public function getCompareList(int $listId, ContextInterface $context, StoreInterface $store) { return [ - 'regular_price' => [ - 'value' => $regularPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'final_price' => [ - 'value' => $finalPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), + 'list_id' => $listId, + 'items' => $this->comparableItemsService->getComparableItems($listId, $context, $store), + 'attributes' => $this->comparableAttributesService->getComparableAttributes($listId, $context) ]; } - - /** - * Get collection of comparable items - * - * @param int $listId - * @param ContextInterface $context - * - * @return Collection - */ - private function getCollectionComparableItems(int $listId, ContextInterface $context): Collection - { - $this->compareProduct->setAllowUsedFlat(false); - /** @var Collection $comparableItems */ - $this->items = $this->itemCollectionFactory->create(); - $this->items->setListId($listId); - $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); - $this->items->addAttributeToSelect( - $this->catalogConfig->getProductAttributes() - )->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility( - $this->catalogProductVisibility->getVisibleInSiteIds() - ); - - return $this->items; - } } From 3dceba934d0ace9a6b74775e147d859bc3069a54 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Sat, 3 Oct 2020 13:22:22 +0300 Subject: [PATCH 10/45] Added changes to schema and add some class --- ...eList.php => AddProductsToCompareList.php} | 2 +- .../Model/Resolver/CreateCompareList.php | 39 +++++++++++++++++++ .../Model/Resolver/DeleteCompareList.php | 39 +++++++++++++++++++ ....php => RemoveProductsFromCompareList.php} | 2 +- .../CompareListGraphQl/etc/schema.graphqls | 34 ++++++++++++---- 5 files changed, 107 insertions(+), 9 deletions(-) rename app/code/Magento/CompareListGraphQl/Model/Resolver/{AddItemsToCompareList.php => AddProductsToCompareList.php} (98%) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php rename app/code/Magento/CompareListGraphQl/Model/Resolver/{RemoveItemsFromCompareList.php => RemoveProductsFromCompareList.php} (98%) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php similarity index 98% rename from app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php rename to app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 8df7b9f7a221e..177652314c18b 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddItemsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -24,7 +24,7 @@ /** * Class add products to compare list */ -class AddItemsToCompareList implements ResolverInterface +class AddProductsToCompareList implements ResolverInterface { /** * @var CompareListFactory diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php new file mode 100644 index 0000000000000..870b1a21f0c9d --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -0,0 +1,39 @@ + Date: Sat, 3 Oct 2020 13:35:17 +0300 Subject: [PATCH 11/45] set type list_id as varchar in DB --- app/code/Magento/Catalog/etc/db_schema.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index 23b777350d33c..8a4869023d80b 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -547,7 +547,7 @@ default="0" comment="Product ID"/> - @@ -578,7 +578,7 @@
- From a8fe53f54f7471e833ff507f94304f108c402dc5 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 5 Oct 2020 23:41:20 +0300 Subject: [PATCH 12/45] add changes to DB scheme --- .../Magento/Catalog/Model/ResourceModel/CompareList.php | 2 +- app/code/Magento/Catalog/etc/db_schema.xml | 6 ++++-- app/code/Magento/Catalog/etc/db_schema_whitelist.json | 1 + 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php b/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php index c7ff5e4963214..aca0213832c8f 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php @@ -18,6 +18,6 @@ class CompareList extends AbstractDb */ protected function _construct() { - $this->_init('catalog_compare_list', 'list_id'); + $this->_init('catalog_compare_list', 'id'); } } diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index 8a4869023d80b..a3d217127c7eb 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -578,14 +578,16 @@
+ + identity="false" comment="Compare List ID"/> - + Date: Wed, 14 Oct 2020 19:22:28 +0300 Subject: [PATCH 13/45] Extract list id to list_id_mask table --- .../Magento/Catalog/Model/CompareList.php | 2 +- app/code/Magento/Catalog/Model/ListIdMask.php | 59 ++++++++++++++++ .../Model/MaskedListIdToCompareListId.php | 53 +++++++++++++++ .../{ => Product/Compare}/CompareList.php | 8 ++- .../ResourceModel/Product/Compare/Item.php | 56 ---------------- .../Product/Compare/Item/Collection.php | 20 ++++++ .../Product/Compare/ListIdMask.php | 26 +++++++ app/code/Magento/Catalog/etc/db_schema.xml | 35 ++++++---- .../Catalog/etc/db_schema_whitelist.json | 20 ++++-- ...reListService.php => AddToCompareList.php} | 13 ++-- .../Model/Service/ComparableItemsService.php | 2 +- .../Model/Service/CreateCompareList.php | 67 +++++++++++++++++++ ...pareListService.php => GetCompareList.php} | 0 13 files changed, 278 insertions(+), 83 deletions(-) create mode 100644 app/code/Magento/Catalog/Model/ListIdMask.php create mode 100644 app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php rename app/code/Magento/Catalog/Model/ResourceModel/{ => Product/Compare}/CompareList.php (66%) create mode 100644 app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php rename app/code/Magento/CompareListGraphQl/Model/Service/{AddToCompareListService.php => AddToCompareList.php} (87%) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php rename app/code/Magento/CompareListGraphQl/Model/Service/{CompareListService.php => GetCompareList.php} (100%) diff --git a/app/code/Magento/Catalog/Model/CompareList.php b/app/code/Magento/Catalog/Model/CompareList.php index 363266d0ce233..5be30d40aacce 100644 --- a/app/code/Magento/Catalog/Model/CompareList.php +++ b/app/code/Magento/Catalog/Model/CompareList.php @@ -19,6 +19,6 @@ class CompareList extends AbstractModel */ protected function _construct() { - $this->_init(\Magento\Catalog\Model\ResourceModel\CompareList::class); + $this->_init(ResourceModel\Product\Compare\CompareList::class); } } diff --git a/app/code/Magento/Catalog/Model/ListIdMask.php b/app/code/Magento/Catalog/Model/ListIdMask.php new file mode 100644 index 0000000000000..160dc703ca876 --- /dev/null +++ b/app/code/Magento/Catalog/Model/ListIdMask.php @@ -0,0 +1,59 @@ +randomDataGenerator = $randomDataGenerator; + parent::__construct($context, $registry, $resource, $resourceCollection, $data); + } + + /** + * Initialize resource + * + * @return void + */ + protected function _construct() + { + $this->_init(ResourceModel\Product\Compare\ListIdMask::class); + } +} diff --git a/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php new file mode 100644 index 0000000000000..0256b33057b12 --- /dev/null +++ b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php @@ -0,0 +1,53 @@ +listIdMaskFactory = $listIdMaskFactory; + $this->listIdMaskResource = $listIdMaskResource; + } + + /** + * Get maskedId by listId + * + * @param string $maskedListId + * + * @return int + */ + public function execute(string $maskedListId): int + { + $listIdMask = $this->listIdMaskFactory->create(); + $this->listIdMaskResource->load($listIdMask, $maskedListId, 'masked_id'); + + return (int)$listIdMask->getListId(); + } +} diff --git a/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/CompareList.php similarity index 66% rename from app/code/Magento/Catalog/Model/ResourceModel/CompareList.php rename to app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/CompareList.php index aca0213832c8f..4185df079d55d 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/CompareList.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/CompareList.php @@ -6,11 +6,13 @@ declare(strict_types=1); -namespace Magento\Catalog\Model\ResourceModel; +namespace Magento\Catalog\Model\ResourceModel\Product\Compare; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; - +/** + * Compare List resource class + */ class CompareList extends AbstractDb { /** @@ -18,6 +20,6 @@ class CompareList extends AbstractDb */ protected function _construct() { - $this->_init('catalog_compare_list', 'id'); + $this->_init('catalog_compare_list', 'list_id'); } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php index e6ef21e2c8448..7eb0552e355fc 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php @@ -5,10 +5,6 @@ */ namespace Magento\Catalog\Model\ResourceModel\Product\Compare; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\ResourceModel\CompareList as CompareListResource; -use Magento\Framework\Model\ResourceModel\Db\Context; - /** * Catalog compare item resource model * @@ -16,16 +12,6 @@ */ class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { - /** - * @var CompareListFactory - */ - private $compareListFactory; - - /** - * @var CompareListResource - */ - private $compareListResource; - /** * Initialize connection * @@ -36,23 +22,6 @@ protected function _construct() $this->_init('catalog_compare_item', 'catalog_compare_item_id'); } - /** - * @param CompareListFactory $compareListFactory - * @param CompareListResource $compareListResource - * @param Context $context - * @param null $connectionName - */ - public function __construct( - CompareListFactory $compareListFactory, - CompareListResource $compareListResource, - Context $context, - $connectionName = null - ) { - $this->compareListFactory = $compareListFactory; - $this->compareListResource = $compareListResource; - parent::__construct($context, $connectionName); - } - /** * Load object by product * @@ -244,7 +213,6 @@ public function updateCustomerFromVisitor($object) $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $itemId) ); } - $this->setCustomerFromVisitor($object); } return $this; @@ -274,28 +242,4 @@ public function clearItems($visitorId = null, $customerId = null) $this->getConnection()->delete($this->getMainTable(), $where); return $this; } - - /** - * Set customer from visitor in catalog_compare_list table - * - * @param \Magento\Catalog\Model\Product\Compare\Item $item - * - * @return $this - */ - private function setCustomerFromVisitor($item) - { - $customerId = $item->getCustomerId(); - - if (!$customerId) { - return $this; - } - - $visitorId = $item->getVisitorId(); - $compareListModel = $this->compareListFactory->create(); - $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); - $compareListModel->setCustomerId($customerId); - $compareListModel->save(); - - return $this; - } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index dca3f76af6a04..8e50fb466d426 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -267,6 +267,26 @@ public function _addJoinToSelect() return $this; } + /** + * Get products ids by for compare list + * + * @param int $listId + * + * @return array + */ + public function getProductsByListId(int $listId) + { + $select = $this->getConnection()->select()-> + from( + $this->getTable('catalog_compare_item'), + 'product_id' + )->where( + 'list_id = ?', + $listId + ); + return $this->getConnection()->fetchCol($select); + } + /** * Retrieve comapre products attribute set ids * diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php new file mode 100644 index 0000000000000..54719f656403d --- /dev/null +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php @@ -0,0 +1,26 @@ +_init('list_id_mask', 'entity_id'); + } +} diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index a3d217127c7eb..146a84e3045bd 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -547,7 +547,7 @@ default="0" comment="Product ID"/> - @@ -578,28 +578,39 @@
- - - + - + - - - -
+ + + + + + + + + + + + + + + +
compareItemFactory->create(); $item->loadByProduct($key); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php index e14678b9ca0e3..14bbc3586e37d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php @@ -10,7 +10,7 @@ use Magento\Catalog\Block\Product\Compare\ListCompare; use Magento\Catalog\Model\CompareList; use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php new file mode 100644 index 0000000000000..429f012e7c422 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php @@ -0,0 +1,67 @@ +compareListFactory = $compareListFactory; + $this->compareListResource = $compareListResource; + $this->customerVisitor = $customerVisitor; + } + + public function createCompareList(string $listId, int $customerId) + { + /* @var $compareListModel CatalogCompareList */ + $compareListModel = $this->compareListFactory->create(); + $compareListModel->setListId($listId); + $this->addVisitorToItem($compareListModel, $customerId); + $compareListModel->save(); + } + + private function addVisitorToItem($model,int $customerId) + { + $model->setVisitorId($this->customerVisitor->getId()); + if ($customerId) { + $model->setCustomerId($customerId); + } + + return $this; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php similarity index 100% rename from app/code/Magento/CompareListGraphQl/Model/Service/CompareListService.php rename to app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php From 5f4fd9face05aa1c7767628e32889ad0fb90dcff Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Oct 2020 19:30:12 +0300 Subject: [PATCH 14/45] reverted changes --- .../Model/Product/Compare/CompareList.php | 110 ------------------ .../Model/Product/Compare/ListCompare.php | 10 -- 2 files changed, 120 deletions(-) delete mode 100644 app/code/Magento/Catalog/Model/Product/Compare/CompareList.php diff --git a/app/code/Magento/Catalog/Model/Product/Compare/CompareList.php b/app/code/Magento/Catalog/Model/Product/Compare/CompareList.php deleted file mode 100644 index 5fab205bbdabe..0000000000000 --- a/app/code/Magento/Catalog/Model/Product/Compare/CompareList.php +++ /dev/null @@ -1,110 +0,0 @@ -compareListFactory = $compareListFactory; - $this->compareListResource = $compareListResource; - } - - /** - * Get list_id - * - * @param Item $item - * - * @return int - */ - public function getListId(Item $item) - { - if ($customerId = $item->getCustomerId()) { - return $this->getListIdByCustomerId($customerId); - } - - return $this->getListIdByVisitorId($item->getVisitorId()); - } - - /** - * Get list_id for visitor - * - * @param $visitorId - * - * @return int - */ - private function getListIdByVisitorId($visitorId) - { - $compareListModel = $this->compareListFactory->create(); - $this->compareListResource->load($compareListModel, $visitorId, 'visitor_id'); - if ($compareListId = $compareListModel->getId()) { - return (int)$compareListId; - } - - return $this->createCompareList($visitorId, null); - } - - /** - * Get list_id for logged customers - * - * @param $customerId - * - * @return int - */ - private function getListIdByCustomerId($customerId) - { - $compareListModel = $this->compareListFactory->create(); - $this->compareListResource->load($compareListModel, $customerId, 'customer_id'); - - if ($compareListId = $compareListModel->getId()) { - return (int)$compareListId; - } - - return $this->createCompareList(0, $customerId); - } - - /** - * Create new compare list - * - * @param $visitorId - * @param $customerId - * - * @return int - */ - private function createCompareList($visitorId, $customerId) - { - /* @var $compareList CatalogCompareList */ - $compareList = $this->compareListFactory->create(); - $compareList->setVisitorId($visitorId); - $compareList->setCustomerId($customerId); - $compareList->save(); - - return (int)$compareList->getId(); - } -} diff --git a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php index 356c8e701b21e..9ccb86441812c 100644 --- a/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php +++ b/app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php @@ -60,11 +60,6 @@ class ListCompare extends \Magento\Framework\DataObject */ private $productRepository; - /** - * @var CompareList - */ - private $compareList; - /** * Constructor * @@ -73,7 +68,6 @@ class ListCompare extends \Magento\Framework\DataObject * @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Customer\Model\Visitor $customerVisitor - * @param \Magento\Catalog\Model\Product\Compare\CompareList $compareList * @param array $data * @param ProductRepository|null $productRepository */ @@ -83,7 +77,6 @@ public function __construct( \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor, - \Magento\Catalog\Model\Product\Compare\CompareList $compareList, array $data = [], ProductRepository $productRepository = null ) { @@ -92,7 +85,6 @@ public function __construct( $this->_catalogProductCompareItem = $catalogProductCompareItem; $this->_customerSession = $customerSession; $this->_customerVisitor = $customerVisitor; - $this->compareList = $compareList; $this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class); parent::__construct($data); } @@ -110,11 +102,9 @@ public function addProduct($product) $item = $this->_compareItemFactory->create(); $this->_addVisitorToItem($item); $item->loadByProduct($product); - $listId = $this->compareList->getListId($item); if (!$item->getId() && $this->productExists($product)) { $item->addProductData($product); - $item->setListId($listId); $item->save(); } From 8be68b4098b01aea78d9f55b4cb9f3c03ede720a Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Oct 2020 20:03:02 +0300 Subject: [PATCH 15/45] create compare list --- .../Model/CompareListIdToMaskedListId.php | 52 ++++++++++++++++ .../Model/Service/AddToCompareList.php | 57 ++++++++++-------- .../Model/Service/CreateCompareList.php | 59 +++++++++++-------- .../Model/Service/CustomerService.php | 40 ++++++++++++- .../Model/Service/GetCompareList.php | 23 +++++--- 5 files changed, 174 insertions(+), 57 deletions(-) create mode 100644 app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php diff --git a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php new file mode 100644 index 0000000000000..b915ca2f18d0b --- /dev/null +++ b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php @@ -0,0 +1,52 @@ +listIdMaskFactory = $listIdMaskFactory; + $this->listIdMaskResource = $listIdMaskResource; + } + + /** + * Get maskedId by listId + * + * @param int $listId + * + * @return string + */ + public function execute(int $listId): string + { + $listIdMask = $this->listIdMaskFactory->create(); + $this->listIdMaskResource->load($listIdMask, $listId, 'list_id'); + return $listIdMask->getMaskedId() ?? ''; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php index 7aa1bdf36b82d..daba16b59dfd7 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php @@ -7,21 +7,17 @@ namespace Magento\CompareListGraphQl\Model\Service; -use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\Product\Compare\Item; use Magento\Catalog\Model\Product\Compare\ItemFactory; use Magento\Catalog\Model\ProductRepository; +use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; use Magento\Framework\Exception\NoSuchEntityException; -use Magento\Tests\NamingConvention\true\string; /** * Service add product to compare list */ -class AddProductsToCompareList +class AddToCompareList { /** - * Compare item factory - * * @var ItemFactory */ private $compareItemFactory; @@ -31,53 +27,66 @@ class AddProductsToCompareList */ private $productRepository; + /** + * @var Collection + */ + private $itemCollection; + /** * @param ItemFactory $compareItemFactory * @param ProductRepository $productRepository + * @param Collection $collection */ public function __construct( ItemFactory $compareItemFactory, - ProductRepository $productRepository + ProductRepository $productRepository, + Collection $collection ) { $this->compareItemFactory = $compareItemFactory; $this->productRepository = $productRepository; + $this->itemCollection = $collection; } /** * Add products to compare list * - * @param string $listId + * @param int $listId * @param array $products + * @param int $storeId + * + * @return int */ - public function execute(string $listId, array $products) + public function execute(int $listId, array $products, int $storeId): int { - foreach ($products as $key) { - /* @var $item Item */ - $item = $this->compareItemFactory->create(); - $item->loadByProduct($key); - if (!$item->getId() && $this->productExists($key)) { - $item->addProductData($key); - $item->setListId($listId); - $item->save(); + if (count($products)) { + $existedProducts = $this->itemCollection->getProductsByListId($listId); + foreach ($products as $productId) { + if (!array_search($productId, $existedProducts)) { + if ($this->productExists($productId)) { + $item = $this->compareItemFactory->create(); + $item->addProductData($productId); + $item->setStoreId($storeId); + $item->setListId($listId); + $item->save(); + } + } } } + + return (int)$listId; } /** * Check product exists. * - * @param int|Product $product + * @param int $productId * * @return bool */ - private function productExists($product) + private function productExists($productId) { - if ($product instanceof Product && $product->getId()) { - return true; - } - try { - $product = $this->productRepository->getById((int)$product); + $product = $this->productRepository->getById((int)$productId); return !empty($product->getId()); } catch (NoSuchEntityException $e) { return false; diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php index 429f012e7c422..c781651c99d8e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php @@ -7,14 +7,12 @@ namespace Magento\CompareListGraphQl\Model\Service; -use Magento\Catalog\Api\Data\ProductInterface; -use Magento\Catalog\Model\CompareList as CatalogCompareList; use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\Product\Compare\Item; -use Magento\Catalog\Model\ResourceModel\CompareList as CompareListResource; -use Magento\Customer\Model\Visitor; +use Magento\Catalog\Model\ListIdMaskFactory; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; +use Magento\Catalog\Model\ResourceModel\Product\Compare\ListIdMask as ListIdMaskResource; -class CompareList +class CreateCompareList { /** * @var CompareListFactory @@ -27,41 +25,52 @@ class CompareList private $compareListResource; /** - * @var Visitor + * @var ListIdMaskFactory */ - private $customerVisitor; + private $maskedListIdFactory; + + /** + * @var ListIdMaskResource + */ + private $maskedListIdResource; /** * @param CompareListFactory $compareListFactory * @param CompareListResource $compareListResource - * @param Visitor $customerVisitor + * @param ListIdMaskFactory $maskedListIdFactory + * @param ListIdMaskResource $maskedListIdResource */ public function __construct( CompareListFactory $compareListFactory, CompareListResource $compareListResource, - Visitor $customerVisitor + ListIdMaskFactory $maskedListIdFactory, + ListIdMaskResource $maskedListIdResource ) { $this->compareListFactory = $compareListFactory; $this->compareListResource = $compareListResource; - $this->customerVisitor = $customerVisitor; + $this->maskedListIdFactory = $maskedListIdFactory; + $this->maskedListIdResource = $maskedListIdResource; } - public function createCompareList(string $listId, int $customerId) + /** + * Created new compare list + * + * @param string $maskedId + * @param int $customerId + * + * @return int + */ + public function execute(string $maskedId, ?int $customerId = null): int { - /* @var $compareListModel CatalogCompareList */ - $compareListModel = $this->compareListFactory->create(); - $compareListModel->setListId($listId); - $this->addVisitorToItem($compareListModel, $customerId); - $compareListModel->save(); - } + $compareList = $this->compareListFactory->create(); + $compareList->setCustomerId($customerId); + $this->compareListResource->save($compareList); - private function addVisitorToItem($model,int $customerId) - { - $model->setVisitorId($this->customerVisitor->getId()); - if ($customerId) { - $model->setCustomerId($customerId); - } + $maskedListId = $this->maskedListIdFactory->create(); + $maskedListId->setListId($compareList->getListId()); + $maskedListId->setMaskedId($maskedId); + $this->maskedListIdResource->save($maskedListId); - return $this; + return (int)$compareList->getListId(); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php index 0eb05c3720eec..3d96013b96cf9 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php @@ -7,6 +7,9 @@ namespace Magento\CompareListGraphQl\Model\Service; +use Magento\Catalog\Model\CompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Api\Data\CustomerInterface; @@ -37,19 +40,54 @@ class CustomerService */ private $accountManagement; + /** + * @var ResourceCompareList + */ + private $resourceCompareList; + + /** + * @var CompareListFactory + */ + private $compareListFactory; + /** * @param AuthenticationInterface $authentication * @param CustomerRepositoryInterface $customerRepository * @param AccountManagementInterface $accountManagement + * @param ResourceCompareList $resourceCompareList + * @param CompareListFactory $compareListFactory */ public function __construct( AuthenticationInterface $authentication, CustomerRepositoryInterface $customerRepository, - AccountManagementInterface $accountManagement + AccountManagementInterface $accountManagement, + ResourceCompareList $resourceCompareList, + CompareListFactory $compareListFactory ) { $this->authentication = $authentication; $this->customerRepository = $customerRepository; $this->accountManagement = $accountManagement; + $this->resourceCompareList = $resourceCompareList; + $this->compareListFactory = $compareListFactory; + } + + /** + * Get listId by Customer ID + * + * @param $customerId + * + * @return int|null + */ + public function getListIdByCustomerId($customerId) + { + if ($customerId) { + /** @var CompareList $compareListModel */ + $compareListModel = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareListModel, $customerId, 'customer_id'); + return (int)$compareListModel->getListId(); + } + + return null; } /** diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index a11b93a8fed39..05b5c85d8bce4 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -7,13 +7,13 @@ namespace Magento\CompareListGraphQl\Model\Service; +use Magento\Catalog\Model\CompareListIdToMaskedListId; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; -use Magento\Store\Api\Data\StoreInterface; /** * Get compare list */ -class CompareListService +class GetCompareList { /** * @var ComparableItemsService @@ -25,31 +25,40 @@ class CompareListService */ private $comparableAttributesService; + /** + * @var CompareListIdToMaskedListId + */ + private $compareListIdToMaskedListId; + /** * @param ComparableItemsService $comparableItemsService * @param ComparableAttributesService $comparableAttributesService + * @param CompareListIdToMaskedListId $compareListIdToMaskedListId */ public function __construct( ComparableItemsService $comparableItemsService, - ComparableAttributesService $comparableAttributesService + ComparableAttributesService $comparableAttributesService, + CompareListIdToMaskedListId $compareListIdToMaskedListId ) { $this->comparableItemsService = $comparableItemsService; $this->comparableAttributesService = $comparableAttributesService; + $this->compareListIdToMaskedListId = $compareListIdToMaskedListId; } /** - * Get compare list + * Get compare list information * * @param int $listId * @param ContextInterface $context - * @param StoreInterface $store * * @return array */ - public function getCompareList(int $listId, ContextInterface $context, StoreInterface $store) + public function execute(int $listId, ContextInterface $context) { + $store = $context->getExtensionAttributes()->getStore(); + $maskedListId = $this->compareListIdToMaskedListId->execute($listId); return [ - 'list_id' => $listId, + 'uid' => $maskedListId, 'items' => $this->comparableItemsService->getComparableItems($listId, $context, $store), 'attributes' => $this->comparableAttributesService->getComparableAttributes($listId, $context) ]; From c9815644c9633d5e08660d62525dda6f513d3a7e Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Oct 2020 20:04:39 +0300 Subject: [PATCH 16/45] added resolver --- .../Model/Resolver/CreateCompareList.php | 75 ++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index 870b1a21f0c9d..f2453adc4bfd5 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -7,17 +7,68 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\CompareList; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\Catalog\Model\Product\Compare\ItemFactory; +use Magento\CompareListGraphQl\Model\Service\AddToCompareList; +use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService; +use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Framework\Math\Random; /** * Class for creating compare list */ class CreateCompareList implements ResolverInterface { + private $compareItemFactory; + + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var Random + */ + private $mathRandom; + + private $customerService; + + private $addProductToCompareList; + + private $getCompareList; + + private $maskedListIdToCompareListId; + + private $createCompareList; + + public function __construct( + ItemFactory $compareItemFactory, + CompareListFactory $compareListFactory, + Random $mathRandom, + CustomerService $customerService, + AddToCompareList $addProductToCompareList, + GetCompareList $getCompareList, + MaskedListIdToCompareListId $maskedListIdToCompareListId, + CreateCompareListService $createCompareList + ) { + $this->compareItemFactory = $compareItemFactory; + $this->compareListFactory = $compareListFactory; + $this->mathRandom = $mathRandom; + $this->customerService = $customerService; + $this->addProductToCompareList = $addProductToCompareList; + $this->getCompareList = $getCompareList; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + $this->createCompareList = $createCompareList; + } + /** * @param Field $field * @param ContextInterface $context @@ -34,6 +85,26 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + $customerId = $context->getUserId(); + $products = !empty($args['input']['products']) ? $args['input']['products'] : []; + $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); + $generatedListId = $this->mathRandom->getUniqueHash(); + + if ((0 === $customerId || null === $customerId)) { + $listId = $this->createCompareList->execute($generatedListId); + $this->addProductToCompareList->execute($listId, $products, $storeId); + } + + if ($customerId) { + $listId = $this->customerService->getListIdByCustomerId($customerId); + if ($listId) { + $this->addProductToCompareList->execute($listId, $products, $storeId); + } else { + $listId = $this->createCompareList->execute($generatedListId, $customerId); + $this->addProductToCompareList->execute($listId, $products, $storeId); + } + } + + return $this->getCompareList->execute($listId, $context); } -} \ No newline at end of file +} From b03318acd8892f0e51f1561e1ef163112dd461c8 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Oct 2020 20:38:16 +0300 Subject: [PATCH 17/45] rename classes --- .../Model/Resolver/CreateCompareList.php | 38 +++++++++---------- ...s.php => GetComparableItemsCollection.php} | 4 +- ...ervice.php => GetComparableAttributes.php} | 8 ++-- ...temsService.php => GetComparableItems.php} | 8 ++-- .../Model/Service/GetCompareList.php | 14 +++---- 5 files changed, 36 insertions(+), 36 deletions(-) rename app/code/Magento/CompareListGraphQl/Model/Service/Collection/{ComparableItems.php => GetComparableItemsCollection.php} (95%) rename app/code/Magento/CompareListGraphQl/Model/Service/{ComparableAttributesService.php => GetComparableAttributes.php} (76%) rename app/code/Magento/CompareListGraphQl/Model/Service/{ComparableItemsService.php => GetComparableItems.php} (94%) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index f2453adc4bfd5..643098f535e75 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -7,10 +7,6 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\CompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\MaskedListIdToCompareListId; -use Magento\Catalog\Model\Product\Compare\ItemFactory; use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService; use Magento\CompareListGraphQl\Model\Service\CustomerService; @@ -27,45 +23,49 @@ */ class CreateCompareList implements ResolverInterface { - private $compareItemFactory; - - /** - * @var CompareListFactory - */ - private $compareListFactory; - /** * @var Random */ private $mathRandom; + /** + * @var CustomerService + */ private $customerService; + /** + * @var AddToCompareList + */ private $addProductToCompareList; + /** + * @var GetCompareList + */ private $getCompareList; - private $maskedListIdToCompareListId; - + /** + * @var CreateCompareListService + */ private $createCompareList; + /** + * @param Random $mathRandom + * @param CustomerService $customerService + * @param AddToCompareList $addProductToCompareList + * @param GetCompareList $getCompareList + * @param CreateCompareListService $createCompareList + */ public function __construct( - ItemFactory $compareItemFactory, - CompareListFactory $compareListFactory, Random $mathRandom, CustomerService $customerService, AddToCompareList $addProductToCompareList, GetCompareList $getCompareList, - MaskedListIdToCompareListId $maskedListIdToCompareListId, CreateCompareListService $createCompareList ) { - $this->compareItemFactory = $compareItemFactory; - $this->compareListFactory = $compareListFactory; $this->mathRandom = $mathRandom; $this->customerService = $customerService; $this->addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; - $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; $this->createCompareList = $createCompareList; } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php similarity index 95% rename from app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php rename to app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php index 0e6c41f1c2aab..ec58aada4c9cd 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/Collection/ComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php @@ -17,7 +17,7 @@ /** * Get collection with comparable items */ -class ComparableItems +class GetComparableItemsCollection { /** * @var Collection @@ -70,7 +70,7 @@ public function __construct( * * @return Collection */ - public function getCollectionComparableItems(int $listId, ContextInterface $context): Collection + public function execute(int $listId, ContextInterface $context): Collection { $this->compareProduct->setAllowUsedFlat(false); /** @var Collection $comparableItems */ diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php similarity index 76% rename from app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php rename to app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php index 152c09f2bbd1f..9e4dcf5fdad1a 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableAttributesService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php @@ -7,13 +7,13 @@ namespace Magento\CompareListGraphQl\Model\Service; -use Magento\CompareListGraphQl\Model\Service\Collection\ComparableItems as ComparableItemsCollection; +use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** * Get comparable attributes */ -class ComparableAttributesService +class GetComparableAttributes { /** * @var ComparableItemsCollection @@ -37,10 +37,10 @@ public function __construct( * * @return array */ - public function getComparableAttributes(int $listId, ContextInterface $context): array + public function execute(int $listId, ContextInterface $context): array { $attributes = []; - $itemsCollection = $this->comparableItemsCollection->getCollectionComparableItems($listId, $context); + $itemsCollection = $this->comparableItemsCollection->execute($listId, $context); foreach ($itemsCollection->getComparableAttributes() as $item) { $attributes[] = [ 'code' => $item->getAttributeCode(), diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php similarity index 94% rename from app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php rename to app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 14bbc3586e37d..e2c2948cc71c4 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/ComparableItemsService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -16,13 +16,13 @@ use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\Pricing\SaleableInterface; use Magento\Store\Api\Data\StoreInterface; -use Magento\CompareListGraphQl\Model\Service\Collection\ComparableItems as ComparableItemsCollection; +use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; /** * Get comparable items */ -class ComparableItemsService +class GetComparableItems { /** * @var Discount @@ -90,7 +90,7 @@ public function __construct( public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) { $items = []; - foreach ($this->comparableItemsCollection->getCollectionComparableItems($listId, $context) as $item) { + foreach ($this->comparableItemsCollection->execute($listId, $context) as $item) { /** @var Product $item */ $items[] = [ 'productId' => $item->getId(), @@ -127,7 +127,7 @@ public function getComparableItems(int $listId, ContextInterface $context, Store private function getProductComparableAttributes(int $listId, Product $product, ContextInterface $context): array { $attributes = []; - $itemsCollection = $this->comparableItemsCollection->getCollectionComparableItems($listId, $context); + $itemsCollection = $this->comparableItemsCollection->execute($listId, $context); foreach ($itemsCollection->getComparableAttributes() as $item) { $attributes[] = [ 'code' => $item->getAttributeCode(), diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index 05b5c85d8bce4..c90dc1d9faf4d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -16,12 +16,12 @@ class GetCompareList { /** - * @var ComparableItemsService + * @var GetComparableItems */ private $comparableItemsService; /** - * @var ComparableAttributesService + * @var GetComparableAttributes */ private $comparableAttributesService; @@ -31,13 +31,13 @@ class GetCompareList private $compareListIdToMaskedListId; /** - * @param ComparableItemsService $comparableItemsService - * @param ComparableAttributesService $comparableAttributesService + * @param GetComparableItems $comparableItemsService + * @param GetComparableAttributes $comparableAttributesService * @param CompareListIdToMaskedListId $compareListIdToMaskedListId */ public function __construct( - ComparableItemsService $comparableItemsService, - ComparableAttributesService $comparableAttributesService, + GetComparableItems $comparableItemsService, + GetComparableAttributes $comparableAttributesService, CompareListIdToMaskedListId $compareListIdToMaskedListId ) { $this->comparableItemsService = $comparableItemsService; @@ -60,7 +60,7 @@ public function execute(int $listId, ContextInterface $context) return [ 'uid' => $maskedListId, 'items' => $this->comparableItemsService->getComparableItems($listId, $context, $store), - 'attributes' => $this->comparableAttributesService->getComparableAttributes($listId, $context) + 'attributes' => $this->comparableAttributesService->execute($listId, $context) ]; } } From 5e69a5145d77f76948e019371fc99f05dbcfe215 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Thu, 15 Oct 2020 22:46:55 +0300 Subject: [PATCH 18/45] Refactoring resolvers --- .../Resolver/AddProductsToCompareList.php | 83 ++++++++----------- .../Resolver/AssignCompareListToCustomer.php | 52 ++++-------- .../Model/Resolver/CompareList.php | 50 +++++------ .../Model/Resolver/CreateCompareList.php | 4 + .../Model/Resolver/CustomerCompareList.php | 60 ++++---------- .../Model/Resolver/DeleteCompareList.php | 1 + .../Model/Service/CustomerService.php | 47 ++++++++--- 7 files changed, 125 insertions(+), 172 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 177652314c18b..c4f6521c42e10 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -7,19 +7,15 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\CompareList as ModelCompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\Product\Compare\CompareList; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\CompareListGraphQl\Model\Service\AddToCompareListService; +use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\CompareListGraphQl\Model\Service\AddToCompareList; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Store\Api\Data\StoreInterface; -use Magento\CompareListGraphQl\Model\Service\CompareListService; /** * Class add products to compare list @@ -27,53 +23,37 @@ class AddProductsToCompareList implements ResolverInterface { /** - * @var CompareListFactory + * @var AddToCompareList */ - private $compareListFactory; + private $addProductToCompareList; /** - * @var ResourceCompareList + * @var GetCompareList */ - private $resourceCompareList; + private $getCompareList; /** - * @var CompareList + * @var MaskedListIdToCompareListId */ - private $compareList; + private $maskedListIdToCompareListId; /** - * @var AddToCompareListService - */ - private $addToCompareListService; - - /** - * @var CompareListService - */ - private $compareListService; - - /** - * @param CompareListFactory $compareListFactory - * @param ResourceCompareList $resourceCompareList - * @param CompareList $compareList - * @param AddToCompareListService $addToCompareListService - * @param CompareListService $compareListService + * @param AddToCompareList $addProductToCompareList + * @param GetCompareList $getCompareList + * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( - CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList, - CompareList $compareList, - AddToCompareListService $addToCompareListService, - CompareListService $compareListService + AddToCompareList $addProductToCompareList, + GetCompareList $getCompareList, + MaskedListIdToCompareListId $maskedListIdToCompareListId ) { - $this->compareListFactory = $compareListFactory; - $this->resourceCompareList = $resourceCompareList; - $this->compareList = $compareList; - $this->addToCompareListService = $addToCompareListService; - $this->compareListService = $compareListService; + $this->addProductToCompareList = $addProductToCompareList; + $this->getCompareList = $getCompareList; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; } /** - * Add items to compare list + * Add products to compare list * * @param Field $field * @param ContextInterface $context @@ -84,6 +64,7 @@ public function __construct( * @return Value|mixed|void * * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function resolve( Field $field, @@ -92,19 +73,23 @@ public function resolve( array $value = null, array $args = null ) { - $listId = (int)$args['id']; - /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); - /** @var $compareListModel ModelCompareList*/ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $args['id']); + $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); + if (!isset($args['input']['uid'])) { + throw new GraphQlInputException(__('"uid" value must be specified.')); + } + + if (!isset($args['input']['products'])) { + throw new GraphQlInputException(__('"products" value must be specified.')); + } + + $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']); - if (!$compareListModel->getId()) { - throw new GraphQlInputException(__('Can\'t load compare list.')); + if (!$listId) { + throw new GraphQlInputException(__('"uid" value does not exist')); } - $this->addToCompareListService->addToCompareList($listId, $args); + $this->addProductToCompareList->execute($listId, $args['input']['products'], $storeId); - return $this->compareListService->getCompareList($listId, $context, $store); + return $this->getCompareList->execute($listId, $context); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index ed4eb724ea14c..c5325673e69f8 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -7,9 +7,7 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\CompareList as ModelCompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; +use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\CustomerService; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -24,33 +22,25 @@ class AssignCompareListToCustomer implements ResolverInterface { /** - * @var CompareListFactory + * @var CustomerService */ - private $compareListFactory; + private $customerService; /** - * @var ResourceCompareList + * @var MaskedListIdToCompareListId */ - private $resourceCompareList; + private $maskedListIdToCompareListId; /** - * @var CustomerService - */ - private $customerService; - - /** - * @param CompareListFactory $compareListFactory - * @param ResourceCompareList $resourceCompareList * @param CustomerService $customerService + * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( - CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList, - CustomerService $customerService + CustomerService $customerService, + MaskedListIdToCompareListId $maskedListIdToCompareListId ) { - $this->compareListFactory = $compareListFactory; - $this->resourceCompareList = $resourceCompareList; $this->customerService = $customerService; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; } /** @@ -65,6 +55,7 @@ public function __construct( * @return Value|mixed|void * * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function resolve( Field $field, @@ -73,27 +64,16 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['customerId'])) { - throw new GraphQlInputException(__('"customerId" value must be specified')); - } - - if (!isset($args['listId'])) { - throw new GraphQlInputException(__('"listId" value must be specified')); + if (!isset($args['uid'])) { + throw new GraphQlInputException(__('"uid" value must be specified')); } - $customer = $this->customerService->validateCustomer($args['customerId']); - - /** @var $compareListModel ModelCompareList*/ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $args['listId']); - - if (!$compareListModel->getId()) { - return false; + if (!$context->getUserId()) { + throw new GraphQlInputException(__('Customer must be logged')); } - $compareListModel->setCustomerId($customer->getId()); - $this->resourceCompareList->save($compareListModel); + $listId = $this->maskedListIdToCompareListId->execute($args['uid']); - return true; + return $this->customerService->setCustomerToCompareList($listId, $context->getUserId()); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index 4edb20ab3d2b9..3bb800dda5e7d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -7,16 +7,14 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\CompareListGraphQl\Model\Service\CompareListService; +use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Catalog\Model\CompareList as ModelCompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Store\Api\Data\StoreInterface; /** * Get compare list @@ -24,33 +22,25 @@ class CompareList implements ResolverInterface { /** - * @var CompareListFactory + * @var GetCompareList */ - private $compareListFactory; + private $getCompareList; /** - * @var ResourceCompareList + * @var MaskedListIdToCompareListId */ - private $resourceCompareList; + private $maskedListIdToCompareListId; /** - * @var CompareListService - */ - private $compareListService; - - /** - * @param CompareListFactory $compareListFactory - * @param ResourceCompareList $resourceCompareList - * @param CompareListService $compareListService + * @param GetCompareList $getCompareList + * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( - CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList, - CompareListService $compareListService + GetCompareList $getCompareList, + MaskedListIdToCompareListId $maskedListIdToCompareListId ) { - $this->compareListFactory = $compareListFactory; - $this->resourceCompareList = $resourceCompareList; - $this->compareListService = $compareListService; + $this->getCompareList = $getCompareList; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; } /** @@ -66,6 +56,7 @@ public function __construct( * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * + * @throws GraphQlInputException */ public function resolve( Field $field, @@ -74,17 +65,16 @@ public function resolve( array $value = null, array $args = null ) { - /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); - /** @var $compareListModel ModelCompareList*/ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $args['id']); - $listId = (int)$compareListModel->getId(); + if (!isset($args['uid'])) { + throw new GraphQlInputException(__('"uid" value must be specified')); + } + + $listId = $this->maskedListIdToCompareListId->execute($args['uid']); if (!$listId) { return null; } - return $this->compareListService->getCompareList($listId, $context, $store); + return $this->getCompareList->execute($listId, $context); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index 643098f535e75..cb7a800cc69cc 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -70,6 +70,8 @@ public function __construct( } /** + * Create compare list + * * @param Field $field * @param ContextInterface $context * @param ResolveInfo $info @@ -77,6 +79,8 @@ public function __construct( * @param array|null $args * * @return Value|mixed|void + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function resolve( Field $field, diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php index cd115ecb53ea4..c81a1775e82e7 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php @@ -7,16 +7,13 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\CompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\CompareListGraphQl\Model\Service\CompareListService; +use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Store\Api\Data\StoreInterface; /** * Get customer compare list @@ -24,33 +21,25 @@ class CustomerCompareList implements ResolverInterface { /** - * @var ResourceCompareList + * @var GetCompareList */ - private $resourceCompareList; + private $getCompareList; /** - * @var CompareListFactory + * @var CustomerService */ - private $compareListFactory; + private $customerService; /** - * @var CompareListService - */ - private $compareListService; - - /** - * @param ResourceCompareList $resourceCompareList - * @param CompareListFactory $compareListFactory - * @param CompareListService $compareListService + * @param GetCompareList $getCompareList + * @param CustomerService $customerService */ public function __construct( - ResourceCompareList $resourceCompareList, - CompareListFactory $compareListFactory, - CompareListService $compareListService + GetCompareList $getCompareList, + CustomerService $customerService ) { - $this->resourceCompareList = $resourceCompareList; - $this->compareListFactory = $compareListFactory; - $this->compareListService = $compareListService; + $this->getCompareList = $getCompareList; + $this->customerService = $customerService; } /** @@ -73,33 +62,12 @@ public function resolve( array $value = null, array $args = null ) { - /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); - $listId = (int)$this->getListIdByCustomerId($context->getUserId()); + $listId = $this->customerService->getListIdByCustomerId($context->getUserId()); if (!$listId) { return null; } - return $this->compareListService->getCompareList($listId, $context, $store); - } - - /** - * Get listId by Customer ID - * - * @param $customerId - * - * @return int|null - */ - private function getListIdByCustomerId($customerId) - { - if ($customerId) { - /** @var CompareList $compareListModel */ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $customerId, 'customer_id'); - return (int)$compareListModel->getId(); - } - - return null; + return $this->getCompareList->execute($listId, $context); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index a0f313837ceef..9bdc7fca6bebd 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -26,6 +26,7 @@ class DeleteCompareList implements ResolverInterface * @param array|null $args * * @return Value|mixed|void + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function resolve( Field $field, diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php index 3d96013b96cf9..729a86177e651 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php @@ -12,7 +12,6 @@ use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; -use Magento\Customer\Api\Data\CustomerInterface; use Magento\Customer\Model\AuthenticationInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; @@ -74,34 +73,60 @@ public function __construct( /** * Get listId by Customer ID * - * @param $customerId + * @param int $customerId * * @return int|null */ - public function getListIdByCustomerId($customerId) + public function getListIdByCustomerId(int $customerId) { if ($customerId) { - /** @var CompareList $compareListModel */ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $customerId, 'customer_id'); - return (int)$compareListModel->getListId(); + /** @var CompareList $compareList */ + $compareList = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareList, $customerId, 'customer_id'); + return (int)$compareList->getListId(); } return null; } + /** + * Set customer to compare list + * + * @param int $listId + * @param int $customerId + * + * @return bool + * + * @throws GraphQlAuthenticationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function setCustomerToCompareList(int $listId, int $customerId): bool + { + if ($this->validateCustomer($customerId)) { + /** @var CompareList $compareListModel */ + $compareList = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareList, $listId, 'list_id'); + $compareList->setCustomerId($customerId); + $this->resourceCompareList->save($compareList); + return true; + } + + return false; + } + /** * Customer validate * - * @param $customerId + * @param int $customerId * - * @return CustomerInterface + * @return int * * @throws GraphQlAuthenticationException * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException */ - public function validateCustomer($customerId) + public function validateCustomer(int $customerId): int { try { $customer = $this->customerRepository->getById($customerId); @@ -128,6 +153,6 @@ public function validateCustomer($customerId) throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); } - return $customer; + return (int)$customer->getId(); } } From 40d5bf6fe3dfa5dfe60f03a7dd32becd5a964a89 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 16 Oct 2020 20:24:17 +0300 Subject: [PATCH 19/45] removing product from compare list and compare list --- .../ResourceModel/Product/Compare/Item.php | 4 + .../Resolver/AssignCompareListToCustomer.php | 14 ++- .../Model/Resolver/CreateCompareList.php | 32 ++++--- .../Model/Resolver/DeleteCompareList.php | 59 ++++++++++++- .../RemoveProductsFromCompareList.php | 87 ++++++++----------- .../GetComparableItemsCollection.php | 1 - .../Model/Service/CreateCompareList.php | 3 + .../Model/Service/GetComparableItems.php | 1 - .../Model/Service/RemoveFromCompareList.php | 59 +++++++++++++ 9 files changed, 195 insertions(+), 65 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/RemoveFromCompareList.php diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php index 7eb0552e355fc..715abd1884abf 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php @@ -45,6 +45,10 @@ public function loadByProduct(\Magento\Catalog\Model\Product\Compare\Item $objec $select->where('visitor_id = ?', (int)$object->getVisitorId()); } + if ($object->getListId()) { + $select->where('list_id = ?', (int)$object->getListId()); + } + $data = $connection->fetchRow($select); if (!$data) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index c5325673e69f8..cd9f7007f8549 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -9,6 +9,7 @@ use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -73,7 +74,18 @@ public function resolve( } $listId = $this->maskedListIdToCompareListId->execute($args['uid']); + $result = false; - return $this->customerService->setCustomerToCompareList($listId, $context->getUserId()); + if ($listId) { + try { + $result = $this->customerService->setCustomerToCompareList($listId, $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during assigning customer.') + ); + } + } + + return $result; } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index cb7a800cc69cc..a731b22788563 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -11,7 +11,9 @@ use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService; use Magento\CompareListGraphQl\Model\Service\CustomerService; use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -81,6 +83,8 @@ public function __construct( * @return Value|mixed|void * * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws GraphQlInputException + * @throws LocalizedException */ public function resolve( Field $field, @@ -94,19 +98,25 @@ public function resolve( $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); $generatedListId = $this->mathRandom->getUniqueHash(); - if ((0 === $customerId || null === $customerId)) { - $listId = $this->createCompareList->execute($generatedListId); - $this->addProductToCompareList->execute($listId, $products, $storeId); - } - - if ($customerId) { - $listId = $this->customerService->getListIdByCustomerId($customerId); - if ($listId) { - $this->addProductToCompareList->execute($listId, $products, $storeId); - } else { - $listId = $this->createCompareList->execute($generatedListId, $customerId); + try { + if ((0 === $customerId || null === $customerId)) { + $listId = $this->createCompareList->execute($generatedListId); $this->addProductToCompareList->execute($listId, $products, $storeId); } + + if ($customerId) { + $listId = $this->customerService->getListIdByCustomerId($customerId); + if ($listId) { + $this->addProductToCompareList->execute($listId, $products, $storeId); + } else { + $listId = $this->createCompareList->execute($generatedListId, $customerId); + $this->addProductToCompareList->execute($listId, $products, $storeId); + } + } + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during creating compare list') + ); } return $this->getCompareList->execute($listId, $context); diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index 9bdc7fca6bebd..9b3cd0fee9525 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -7,7 +7,12 @@ namespace Magento\CompareListGraphQl\Model\Resolver; +use Magento\Catalog\Model\CompareListFactory; +use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -18,6 +23,36 @@ */ class DeleteCompareList implements ResolverInterface { + /** + * @var CompareListFactory + */ + private $compareListFactory; + + /** + * @var CompareListResource + */ + private $compareListResource; + + /** + * @var MaskedListIdToCompareListId + */ + private $maskedListIdToCompareListId; + + /** + * @param CompareListFactory $compareListFactory + * @param CompareListResource $compareListResource + * @param MaskedListIdToCompareListId $maskedListIdToCompareListId + */ + public function __construct( + CompareListFactory $compareListFactory, + CompareListResource $compareListResource, + MaskedListIdToCompareListId $maskedListIdToCompareListId + ) { + $this->compareListFactory = $compareListFactory; + $this->compareListResource = $compareListResource; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + } + /** * @param Field $field * @param ContextInterface $context @@ -26,7 +61,9 @@ class DeleteCompareList implements ResolverInterface * @param array|null $args * * @return Value|mixed|void + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws GraphQlInputException */ public function resolve( Field $field, @@ -35,6 +72,26 @@ public function resolve( array $value = null, array $args = null ) { - // TODO: Implement resolve() method. + if (!isset($args['uid'])) { + throw new GraphQlInputException(__('"uid" value must be specified')); + } + + $listId = $this->maskedListIdToCompareListId->execute($args['uid']); + $removed = ['result' => false]; + + if ($listId) { + try { + $compareList = $this->compareListFactory->create(); + $compareList->setListId($listId); + $this->compareListResource->delete($compareList); + $removed['result'] = true; + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during removing compare list') + ); + } + } + + return $removed; } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index 35baeb4996048..0ca47c0f4b6e4 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -7,19 +7,16 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\Catalog\Model\CompareList as ModelCompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\Product\Compare\Item; -use Magento\Catalog\Model\Product\Compare\ItemFactory; -use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList; -use Magento\CompareListGraphQl\Model\Service\CompareListService; +use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\CompareListGraphQl\Model\Service\RemoveFromCompareList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\Store\Api\Data\StoreInterface; /** * Remove items from compare list @@ -27,47 +24,37 @@ class RemoveProductsFromCompareList implements ResolverInterface { /** - * @var CompareListFactory + * @var GetCompareList */ - private $compareListFactory; + private $getCompareList; /** - * @var ResourceCompareList + * @var RemoveFromCompareList */ - private $resourceCompareList; + private $removeFromCompareList; /** - * Compare item factory - * - * @var ItemFactory - */ - private $compareItemFactory; - - /** - * @var CompareListService + * @var MaskedListIdToCompareListId */ - private $compareListService; + private $maskedListIdToCompareListId; /** - * @param CompareListFactory $compareListFactory - * @param ResourceCompareList $resourceCompareList - * @param ItemFactory $compareItemFactory - * @param CompareListService $compareListService + * @param GetCompareList $getCompareList + * @param RemoveFromCompareList $removeFromCompareList + * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( - CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList, - ItemFactory $compareItemFactory, - CompareListService $compareListService + GetCompareList $getCompareList, + RemoveFromCompareList $removeFromCompareList, + MaskedListIdToCompareListId $maskedListIdToCompareListId ) { - $this->compareListFactory = $compareListFactory; - $this->resourceCompareList = $resourceCompareList; - $this->compareItemFactory = $compareItemFactory; - $this->compareListService = $compareListService; + $this->getCompareList = $getCompareList; + $this->removeFromCompareList = $removeFromCompareList; + $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; } /** - * Remove items from compare list + * Remove products from compare list * * @param Field $field * @param ContextInterface $context @@ -88,28 +75,28 @@ public function resolve( array $value = null, array $args = null ) { - $listId = (int)$args['id']; - /** @var StoreInterface $store */ - $store = $context->getExtensionAttributes()->getStore(); - /** @var $compareListModel ModelCompareList*/ - $compareListModel = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareListModel, $args['id']); + if (!isset($args['input']['products'])) { + throw new GraphQlInputException(__('"products" value must be specified.')); + } - if (!$compareListModel->getId()) { - throw new GraphQlInputException(__('Can\'t load compare list.')); + if (!isset($args['input']['uid'])) { + throw new GraphQlInputException(__('"uid" value must be specified.')); } - foreach ($args['items'] as $key) { - /* @var $item Item */ - $item = $this->compareItemFactory->create(); - $item->setListId($listId); - $item->loadByProduct($key); + $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']); + + if (!$listId) { + throw new GraphQlInputException(__('"uid" value does not exist')); + } - if ($item->getId()) { - $item->delete(); - } + try { + $this->removeFromCompareList->execute($listId, $args['input']['products']); + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during removing products from compare list') + ); } - return $this->compareListService->getCompareList($listId, $context, $store); + return $this->getCompareList->execute($listId, $context); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php index ec58aada4c9cd..e766ec85248a1 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Collection/GetComparableItemsCollection.php @@ -73,7 +73,6 @@ public function __construct( public function execute(int $listId, ContextInterface $context): Collection { $this->compareProduct->setAllowUsedFlat(false); - /** @var Collection $comparableItems */ $this->items = $this->itemCollectionFactory->create(); $this->items->setListId($listId); $this->items->useProductItem()->setStoreId($context->getExtensionAttributes()->getStore()->getStoreId()); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php index c781651c99d8e..c92eb777c5b05 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CreateCompareList.php @@ -12,6 +12,9 @@ use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; use Magento\Catalog\Model\ResourceModel\Product\Compare\ListIdMask as ListIdMaskResource; +/** + * Create new Compare List + */ class CreateCompareList { /** diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index e2c2948cc71c4..9e5516ba0f557 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -18,7 +18,6 @@ use Magento\Store\Api\Data\StoreInterface; use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; - /** * Get comparable items */ diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/RemoveFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/RemoveFromCompareList.php new file mode 100644 index 0000000000000..dda400559a412 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/RemoveFromCompareList.php @@ -0,0 +1,59 @@ +compareItemFactory = $compareItemFactory; + $this->compareItemResource = $compareItemResource; + } + + /** + * Remove products from compare list + * + * @param int $listId + * @param array $products + */ + public function execute(int $listId, array $products) + { + foreach ($products as $productId) { + /* @var $item Item */ + $item = $this->compareItemFactory->create(); + $item->setListId($listId); + $this->compareItemResource->loadByProduct($item, $productId); + if ($item->getId()) { + $this->compareItemResource->delete($item); + } + } + } +} From eb4de34cfd1c72b68fe202233bc28ffe640958f8 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 16 Oct 2020 20:32:29 +0300 Subject: [PATCH 20/45] fix static issues --- .../Model/Resolver/AddProductsToCompareList.php | 2 +- .../CompareListGraphQl/Model/Service/AddToCompareList.php | 2 +- .../Model/Service/GetComparableAttributes.php | 2 +- .../CompareListGraphQl/Model/Service/GetComparableItems.php | 2 +- .../Magento/CompareListGraphQl/Model/Service/GetCompareList.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index c4f6521c42e10..e0b27157ff3ac 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -18,7 +18,7 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; /** - * Class add products to compare list + * Add products item to compare list */ class AddProductsToCompareList implements ResolverInterface { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php index daba16b59dfd7..88893b23c3d65 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php @@ -61,7 +61,7 @@ public function execute(int $listId, array $products, int $storeId): int if (count($products)) { $existedProducts = $this->itemCollection->getProductsByListId($listId); foreach ($products as $productId) { - if (!array_search($productId, $existedProducts)) { + if (array_search($productId, $existedProducts) === false) { if ($this->productExists($productId)) { $item = $this->compareItemFactory->create(); $item->addProductData($productId); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php index 9e4dcf5fdad1a..a9c93d37c6d74 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php @@ -11,7 +11,7 @@ use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** - * Get comparable attributes + * Get products comparable attributes */ class GetComparableAttributes { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 9e5516ba0f557..6646a2e9e111c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -19,7 +19,7 @@ use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; /** - * Get comparable items + * Get comparable products */ class GetComparableItems { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index c90dc1d9faf4d..bdf24576ef516 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -11,7 +11,7 @@ use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** - * Get compare list + * Get products compare list */ class GetCompareList { From 261e00c408d5ee4b459f413b16c05fcb3f35c16c Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Sat, 17 Oct 2020 10:05:01 +0300 Subject: [PATCH 21/45] static issue fixes --- app/code/Magento/Catalog/Model/ListIdMask.php | 33 ------------------- .../ResourceModel/Product/Compare/Item.php | 1 + .../Product/Compare/Item/Collection.php | 6 ++-- .../Model/Resolver/CreateCompareList.php | 1 + .../Model/Resolver/DeleteCompareList.php | 2 ++ .../Model/Service/CustomerService.php | 16 ++++----- app/code/Magento/CompareListGraphQl/README.md | 4 +++ .../Magento/CompareListGraphQl/composer.json | 3 +- composer.json | 1 + 9 files changed, 22 insertions(+), 45 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/README.md diff --git a/app/code/Magento/Catalog/Model/ListIdMask.php b/app/code/Magento/Catalog/Model/ListIdMask.php index 160dc703ca876..06fc825a29d1b 100644 --- a/app/code/Magento/Catalog/Model/ListIdMask.php +++ b/app/code/Magento/Catalog/Model/ListIdMask.php @@ -7,46 +7,13 @@ namespace Magento\Catalog\Model; -use Magento\Framework\Data\Collection\AbstractDb; -use Magento\Framework\Math\Random; use Magento\Framework\Model\AbstractModel; -use Magento\Framework\Model\Context; -use Magento\Framework\Model\ResourceModel\AbstractResource; -use Magento\Framework\Registry; /** * ListIdMask model - * - * @method string getMaskedId() - * @method ListIdMask setMaskedId() */ class ListIdMask extends AbstractModel { - /** - * @var Random - */ - protected $randomDataGenerator; - - /** - * @param Context $context - * @param Registry $registry - * @param Random $randomDataGenerator - * @param AbstractResource $resource - * @param AbstractDb $resourceCollection - * @param array $data - */ - public function __construct( - Context $context, - Registry $registry, - Random $randomDataGenerator, - AbstractResource $resource = null, - AbstractDb $resourceCollection = null, - array $data = [] - ) { - $this->randomDataGenerator = $randomDataGenerator; - parent::__construct($context, $registry, $resource, $resourceCollection, $data); - } - /** * Initialize resource * diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php index 715abd1884abf..ff29a5afa7eda 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php @@ -144,6 +144,7 @@ public function purgeVisitorByCustomer($object) /** * Update (Merge) customer data from visitor + * * After Login process * * @param \Magento\Catalog\Model\Product\Compare\Item $object diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 8e50fb466d426..59ef50af74b33 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -166,13 +166,13 @@ public function setCustomerId($customerId) /** * Set listId filter to collection * - * @param $listId + * @param int $listId * * @return $this */ - public function setListId($listId) + public function setListId(int $listId) { - $this->listId = (int)$listId; + $this->listId = $listId; $this->_addJoinToSelect(); return $this; } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index a731b22788563..dd90fab117fd1 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -97,6 +97,7 @@ public function resolve( $products = !empty($args['input']['products']) ? $args['input']['products'] : []; $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); $generatedListId = $this->mathRandom->getUniqueHash(); + $listId = 0; try { if ((0 === $customerId || null === $customerId)) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index 9b3cd0fee9525..7278c6c0ef0d1 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -54,6 +54,8 @@ public function __construct( } /** + * Remove compare list + * * @param Field $field * @param ContextInterface $context * @param ResolveInfo $info diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php index 729a86177e651..201faf078d243 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php @@ -51,23 +51,23 @@ class CustomerService /** * @param AuthenticationInterface $authentication - * @param CustomerRepositoryInterface $customerRepository * @param AccountManagementInterface $accountManagement - * @param ResourceCompareList $resourceCompareList - * @param CompareListFactory $compareListFactory + * @param CustomerRepositoryInterface $customerRepository + * @param CompareListFactory $compareListFactory + * @param ResourceCompareList $resourceCompareList */ public function __construct( AuthenticationInterface $authentication, - CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement, - ResourceCompareList $resourceCompareList, - CompareListFactory $compareListFactory + CustomerRepositoryInterface $customerRepository, + CompareListFactory $compareListFactory, + ResourceCompareList $resourceCompareList ) { $this->authentication = $authentication; - $this->customerRepository = $customerRepository; $this->accountManagement = $accountManagement; - $this->resourceCompareList = $resourceCompareList; + $this->customerRepository = $customerRepository; $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; } /** diff --git a/app/code/Magento/CompareListGraphQl/README.md b/app/code/Magento/CompareListGraphQl/README.md new file mode 100644 index 0000000000000..ed1c38ab33a3b --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/README.md @@ -0,0 +1,4 @@ +# CompareListGraphQl module + +The CompareListGraphQl module is designed to implement compare product functionality. + diff --git a/app/code/Magento/CompareListGraphQl/composer.json b/app/code/Magento/CompareListGraphQl/composer.json index fe4b67107c06d..af0863f4d4dbf 100644 --- a/app/code/Magento/CompareListGraphQl/composer.json +++ b/app/code/Magento/CompareListGraphQl/composer.json @@ -7,7 +7,8 @@ "php": "~7.3.0||~7.4.0", "magento/framework": "*", "magento/module-catalog": "*", - "magento/module-customer": "*" + "magento/module-customer": "*", + "magento/module-store": "*" }, "license": [ "OSL-3.0", diff --git a/composer.json b/composer.json index 57fbfaaa35c2b..2ae82b6bcb538 100644 --- a/composer.json +++ b/composer.json @@ -136,6 +136,7 @@ "magento/module-checkout-agreements-graph-ql": "*", "magento/module-cms": "*", "magento/module-cms-url-rewrite": "*", + "magento/compare-list-graph-ql": "*", "magento/module-config": "*", "magento/module-configurable-import-export": "*", "magento/module-configurable-product": "*", From 89d77bb83a8fbb4155c151231fc51ff5f9a6febb Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Sat, 17 Oct 2020 10:11:44 +0300 Subject: [PATCH 22/45] changes in DB scheme --- app/code/Magento/Catalog/etc/db_schema.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index 146a84e3045bd..1bd59e6236d69 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -547,7 +547,7 @@ default="0" comment="Product ID"/> - From 48197d4b2e4fd8e1fe911d5051ecf17a3367df82 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 21 Oct 2020 20:43:52 +0300 Subject: [PATCH 23/45] Minor changes in schema --- .../Model/Service/GetComparableAttributes.php | 2 +- .../CompareListGraphQl/Model/Service/GetComparableItems.php | 2 +- app/code/Magento/CompareListGraphQl/etc/schema.graphqls | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php index a9c93d37c6d74..5b051a3825aac 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableAttributes.php @@ -44,7 +44,7 @@ public function execute(int $listId, ContextInterface $context): array foreach ($itemsCollection->getComparableAttributes() as $item) { $attributes[] = [ 'code' => $item->getAttributeCode(), - 'title' => $item->getStoreLabel() + 'label' => $item->getStoreLabel() ]; } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 6646a2e9e111c..3a797316583f3 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -95,7 +95,7 @@ public function getComparableItems(int $listId, ContextInterface $context, Store 'productId' => $item->getId(), 'name' => $item->getName(), 'sku' => $item->getSku(), - 'priceRange' => [ + 'price_range' => [ 'minimum_price' => $this->getMinimumProductPrice($item, $store), 'maximum_price' => $this->getMaximumProductPrice($item, $store) ], diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 46100d0a61bb7..497cdb35ce01c 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -5,7 +5,7 @@ type ComparableItem { productId: ID! @doc(description: "Product Id") name: String! @doc(description: "Product name") sku: String! @doc(description: "Product SKU") - priceRange: PriceRange! @doc(description: "Product prices") + price_range: PriceRange! @doc(description: "Product prices") canonical_url: String @doc(description: "Product URL") images: [ProductImage]! @doc(description: "Product Images") values: [ProductAttribute]! @doc(description: "Product comparable attributes") @@ -18,7 +18,7 @@ type ProductAttribute { type ComparableAttribute { code: String! @doc(description: "Attribute code") - title: String! @doc(description: "Attribute display title") + label: String! @doc(description: "Attribute label") } type CompareList { From 455a28e150e5b5afcdbf7d5401a71ce910ef9667 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 26 Oct 2020 19:30:33 +0200 Subject: [PATCH 24/45] Add changes after schema updating --- .../Model/Service/GetComparableItems.php | 161 ++++++------------ .../Magento/CompareListGraphQl/composer.json | 5 +- .../CompareListGraphQl/etc/schema.graphqls | 10 +- composer.json | 2 +- .../GraphQl/CompareList/CompareListTest.php | 65 +++++++ 5 files changed, 122 insertions(+), 121 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 3a797316583f3..cfbf921ed50ba 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -8,15 +8,14 @@ namespace Magento\CompareListGraphQl\Model\Service; use Magento\Catalog\Block\Product\Compare\ListCompare; -use Magento\Catalog\Model\CompareList; +use Magento\Catalog\Model\CompareListIdToMaskedListId; use Magento\Catalog\Model\Product; -use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; -use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; +use Magento\Catalog\Model\ProductRepository; +use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; -use Magento\Framework\Pricing\SaleableInterface; use Magento\Store\Api\Data\StoreInterface; -use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; /** * Get comparable products @@ -24,57 +23,41 @@ class GetComparableItems { /** - * @var Discount - */ - private $discount; - - /** - * @var PriceProviderPool + * @var ListCompare */ - private $priceProviderPool; + private $blockListCompare; /** - * @var ResourceCompareList + * @var ComparableItemsCollection */ - private $resourceCompareList; + private $comparableItemsCollection; /** - * @var CompareList + * @var ProductRepository */ - private $modelCompareList; + private $productRepository; /** - * @var ListCompare + * @var CompareListIdToMaskedListId */ - private $blockListCompare; + private $compareListIdToMaskedListId; /** - * @var ComparableItemsCollection - */ - private $comparableItemsCollection; - - /** - * @param PriceProviderPool $priceProviderPool - * @param Discount $discount - * @param ResourceCompareList $resourceCompareList - * @param CompareList $compareList - * @param ListCompare $listCompare - * @param ComparableItemsCollection $comparableItemsCollection + * @param ListCompare $listCompare + * @param ComparableItemsCollection $comparableItemsCollection + * @param ProductRepository $productRepository + * @param CompareListIdToMaskedListId $compareListIdToMaskedListId */ public function __construct( - PriceProviderPool $priceProviderPool, - Discount $discount, - ResourceCompareList $resourceCompareList, - CompareList $compareList, ListCompare $listCompare, - ComparableItemsCollection $comparableItemsCollection + ComparableItemsCollection $comparableItemsCollection, + ProductRepository $productRepository, + CompareListIdToMaskedListId $compareListIdToMaskedListId ) { - $this->priceProviderPool = $priceProviderPool; - $this->discount = $discount; - $this->resourceCompareList = $resourceCompareList; - $this->modelCompareList = $compareList; $this->blockListCompare = $listCompare; $this->comparableItemsCollection = $comparableItemsCollection; + $this->productRepository = $productRepository; + $this->compareListIdToMaskedListId = $compareListIdToMaskedListId; } /** @@ -85,35 +68,47 @@ public function __construct( * @param StoreInterface $store * * @return array + * @throws GraphQlInputException */ public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) { $items = []; + $maskedListId = $this->compareListIdToMaskedListId->execute($listId); foreach ($this->comparableItemsCollection->execute($listId, $context) as $item) { /** @var Product $item */ $items[] = [ - 'productId' => $item->getId(), - 'name' => $item->getName(), - 'sku' => $item->getSku(), - 'price_range' => [ - 'minimum_price' => $this->getMinimumProductPrice($item, $store), - 'maximum_price' => $this->getMaximumProductPrice($item, $store) - ], - 'canonical_url' => $item->getUrlKey(), - 'images' => [ - 'url' => [ - 'model' => $item, - 'image_type' => 'image', - 'label' => $item->getImageLabel() - ], - ], - 'values' => $this->getProductComparableAttributes($listId, $item, $context) + 'uid' => $maskedListId, + 'product' => $this->getProductData((int)$item->getId()), + 'attributes' => $this->getProductComparableAttributes($listId, $item, $context) ]; } return $items; } + /** + * Get product data + * + * @param int $productId + * + * @return array + * + * @throws GraphQlInputException + */ + private function getProductData(int $productId): array + { + $productData = []; + try { + $item = $this->productRepository->getById($productId); + $productData = $item->getData(); + $productData['model'] = $item; + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + return $productData; + } + /** * Get comparable attributes for product * @@ -136,60 +131,4 @@ private function getProductComparableAttributes(int $listId, Product $product, C return $attributes; } - - /** - * Get formatted minimum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMinimumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMinimalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMinimalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Get formatted maximum product price - * - * @param SaleableInterface $product - * @param StoreInterface $store - * - * @return array - */ - private function getMaximumProductPrice(SaleableInterface $product, StoreInterface $store): array - { - $priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId()); - $regularPrice = $priceProvider->getMaximalRegularPrice($product)->getValue(); - $finalPrice = $priceProvider->getMaximalFinalPrice($product)->getValue(); - return $this->formatPrice((float) $regularPrice, (float) $finalPrice, $store); - } - - /** - * Format price for GraphQl output - * - * @param float $regularPrice - * @param float $finalPrice - * @param StoreInterface $store - * - * @return array - */ - private function formatPrice(float $regularPrice, float $finalPrice, StoreInterface $store): array - { - return [ - 'regular_price' => [ - 'value' => $regularPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'final_price' => [ - 'value' => $finalPrice, - 'currency' => $store->getCurrentCurrencyCode() - ], - 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), - ]; - } } diff --git a/app/code/Magento/CompareListGraphQl/composer.json b/app/code/Magento/CompareListGraphQl/composer.json index af0863f4d4dbf..dcefc6acfa8b4 100644 --- a/app/code/Magento/CompareListGraphQl/composer.json +++ b/app/code/Magento/CompareListGraphQl/composer.json @@ -1,5 +1,5 @@ { - "name": "magento/compare-list-graph-ql", + "name": "magento/module-compare-list-graph-ql", "version": "1.0.0", "description": "N/A", "type": "magento2-module", @@ -8,7 +8,8 @@ "magento/framework": "*", "magento/module-catalog": "*", "magento/module-customer": "*", - "magento/module-store": "*" + "magento/module-store": "*", + "magento/module-catalog-graph-ql": "*" }, "license": [ "OSL-3.0", diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 497cdb35ce01c..8634b05aa2bdb 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -2,13 +2,9 @@ # See COPYING.txt for license details. type ComparableItem { - productId: ID! @doc(description: "Product Id") - name: String! @doc(description: "Product name") - sku: String! @doc(description: "Product SKU") - price_range: PriceRange! @doc(description: "Product prices") - canonical_url: String @doc(description: "Product URL") - images: [ProductImage]! @doc(description: "Product Images") - values: [ProductAttribute]! @doc(description: "Product comparable attributes") + uid: ID! + product: ProductInterface! + attributes: [ProductAttribute]! @doc(description: "Product comparable attributes") } type ProductAttribute { diff --git a/composer.json b/composer.json index 2ae82b6bcb538..c6db41663e778 100644 --- a/composer.json +++ b/composer.json @@ -136,7 +136,7 @@ "magento/module-checkout-agreements-graph-ql": "*", "magento/module-cms": "*", "magento/module-cms-url-rewrite": "*", - "magento/compare-list-graph-ql": "*", + "magento/module-compare-list-graph-ql": "*", "magento/module-config": "*", "magento/module-configurable-import-export": "*", "magento/module-configurable-product": "*", diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php new file mode 100644 index 0000000000000..6ac127b69875b --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php @@ -0,0 +1,65 @@ +graphQlMutation($mutation); + $uid = $response['createCompareList']['uid']; + $this->uidAssertion($uid); + } + + /** + * Create compare list with products + */ + public function testCreateCompareListWithProducts() + { + $mutation = <<assertIsString($uid); + $this->assertEquals(32, strlen($uid)); + } +} \ No newline at end of file From d436687801481990b8028b347d6a52187e72b4d0 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 26 Oct 2020 21:00:18 +0200 Subject: [PATCH 25/45] Add test coverage --- .../Magento/CompareListGraphQl/etc/module.xml | 1 - .../GraphQl/CompareList/CompareListTest.php | 188 +++++++++++++++++- 2 files changed, 179 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/etc/module.xml b/app/code/Magento/CompareListGraphQl/etc/module.xml index b3c330fc38df2..050093c2032ec 100644 --- a/app/code/Magento/CompareListGraphQl/etc/module.xml +++ b/app/code/Magento/CompareListGraphQl/etc/module.xml @@ -12,7 +12,6 @@ - diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php index 6ac127b69875b..b2238ffb42423 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php @@ -7,6 +7,8 @@ namespace Magento\GraphQl\CompareList; +use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; /** @@ -14,17 +16,47 @@ */ class CompareListTest extends GraphQlAbstract { + private const PRODUCT_SKU_1 = 'simple1'; + private const PRODUCT_SKU_2 = 'simple2'; + + /** + * @var mixed + */ + private $productRepository; + + protected function setUp(): void + { + $objectManager = Bootstrap::getObjectManager(); + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); + } /** * Create compare list without product */ public function testCreateCompareListWithoutProducts() { + $response = $this->createCompareList(); + $uid = $response['createCompareList']['uid']; + $this->uidAssertion($uid); + } + + /** + * Create compare list with products + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + */ + public function testCreateCompareListWithProducts() + { + $product1 = $this->productRepository->get(self::PRODUCT_SKU_1); + $product2 = $this->productRepository->get(self::PRODUCT_SKU_2); + $mutation = <<getId()}, {$product2->getId()}]}){ uid items { - sku + product { + sku + } } } } @@ -32,25 +64,150 @@ public function testCreateCompareListWithoutProducts() $response = $this->graphQlMutation($mutation); $uid = $response['createCompareList']['uid']; $this->uidAssertion($uid); + $this->itemsAssertion($response['createCompareList']['items']); } /** - * Create compare list with products + * Add products to compare list + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php */ - public function testCreateCompareListWithProducts() + public function testAddProductToCompareList() + { + $compareList = $this->createCompareList(); + $uid = $compareList['createCompareList']['uid']; + $this->uidAssertion($uid); + $response = $this->addProductsToCompareList($uid); + $resultUid = $response['addProductsToCompareList']['uid']; + $this->uidAssertion($resultUid); + $this->itemsAssertion($response['addProductsToCompareList']['items']); + } + + /** + * Remove products from compare list + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + */ + public function testRemoveProductFromCompareList() + { + $compareList = $this->createCompareList(); + $uid = $compareList['createCompareList']['uid']; + $this->uidAssertion($uid); + $addProducts = $this->addProductsToCompareList($uid); + $this->itemsAssertion($addProducts['addProductsToCompareList']['items']); + $this->assertCount(2, $addProducts['addProductsToCompareList']['items']); + $product = $this->productRepository->get(self::PRODUCT_SKU_1); + $removeFromCompareList = <<getId()}]}) { + uid + items { + product { + sku + } + } + } +} +MUTATION; + $response = $this->graphQlMutation($removeFromCompareList); + $this->assertCount(1, $response['removeProductsFromCompareList']['items']); + } + + /** + * Get compare list query + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + */ + public function testGetCompareList() + { + $compareList = $this->createCompareList(); + $uid = $compareList['createCompareList']['uid']; + $this->uidAssertion($uid); + $addProducts = $this->addProductsToCompareList($uid); + $this->itemsAssertion($addProducts['addProductsToCompareList']['items']); + $query = <<graphQlQuery($query); + $this->itemsAssertion($response['compareList']['items']); + } + + /** + * Remove compare list + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + */ + public function testDeleteCompareList() + { + $compareList = $this->createCompareList(); + $uid = $compareList['createCompareList']['uid']; + $this->uidAssertion($uid); + $addProducts = $this->addProductsToCompareList($uid); + $this->itemsAssertion($addProducts['addProductsToCompareList']['items']); + $deleteCompareList = <<graphQlMutation($deleteCompareList); + $this->assertTrue($response['deleteCompareList']['result']); + $response1 = $this->graphQlMutation($deleteCompareList); + $this->assertFalse($response1['deleteCompareList']['result']); + } + + /** + * Create compare list + * + * @return array + */ + private function createCompareList(): array { $mutation = <<graphQlMutation($mutation); } + /** + * Add products to compare list + * + * @param $uid + * + * @return array + */ + private function addProductsToCompareList($uid): array + { + $product1 = $this->productRepository->get(self::PRODUCT_SKU_1); + $product2 = $this->productRepository->get(self::PRODUCT_SKU_2); + $addProductsToCompareList = <<getId()}, {$product2->getId()}]}) { + uid + items { + product { + sku + } + } + } +} +MUTATION; + return $this->graphQlMutation($addProductsToCompareList); + } /** * Assert UID @@ -62,4 +219,17 @@ private function uidAssertion(string $uid) $this->assertIsString($uid); $this->assertEquals(32, strlen($uid)); } -} \ No newline at end of file + + /** + * Assert products + * + * @param array $items + */ + private function itemsAssertion(array $items) + { + $this->assertArrayHasKey(0, $items); + $this->assertArrayHasKey(1, $items); + $this->assertEquals(self::PRODUCT_SKU_1, $items[0]['product']['sku']); + $this->assertEquals(self::PRODUCT_SKU_2, $items[1]['product']['sku']); + } +} From 880e45404581f8fe57abc3a5ffffee871b3117b8 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 28 Oct 2020 11:28:39 +0200 Subject: [PATCH 26/45] add new scenario to test coverage --- .../Model/Service/GetComparableItems.php | 3 +- .../Model/Service/GetCompareList.php | 5 +- .../Magento/CompareListGraphQl/composer.json | 3 +- .../GraphQl/CompareList/CompareListTest.php | 88 ++++++++++++++++++- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index cfbf921ed50ba..2b458db70a3b9 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -65,12 +65,11 @@ public function __construct( * * @param int $listId * @param ContextInterface $context - * @param StoreInterface $store * * @return array * @throws GraphQlInputException */ - public function getComparableItems(int $listId, ContextInterface $context, StoreInterface $store) + public function getComparableItems(int $listId, ContextInterface $context) { $items = []; $maskedListId = $this->compareListIdToMaskedListId->execute($listId); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index bdf24576ef516..5cd785368f8a6 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -8,6 +8,7 @@ namespace Magento\CompareListGraphQl\Model\Service; use Magento\Catalog\Model\CompareListIdToMaskedListId; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** @@ -52,14 +53,14 @@ public function __construct( * @param ContextInterface $context * * @return array + * @throws GraphQlInputException */ public function execute(int $listId, ContextInterface $context) { - $store = $context->getExtensionAttributes()->getStore(); $maskedListId = $this->compareListIdToMaskedListId->execute($listId); return [ 'uid' => $maskedListId, - 'items' => $this->comparableItemsService->getComparableItems($listId, $context, $store), + 'items' => $this->comparableItemsService->getComparableItems($listId, $context), 'attributes' => $this->comparableAttributesService->execute($listId, $context) ]; } diff --git a/app/code/Magento/CompareListGraphQl/composer.json b/app/code/Magento/CompareListGraphQl/composer.json index dcefc6acfa8b4..0c3c76e49f85d 100644 --- a/app/code/Magento/CompareListGraphQl/composer.json +++ b/app/code/Magento/CompareListGraphQl/composer.json @@ -8,8 +8,7 @@ "magento/framework": "*", "magento/module-catalog": "*", "magento/module-customer": "*", - "magento/module-store": "*", - "magento/module-catalog-graph-ql": "*" + "magento/module-store": "*" }, "license": [ "OSL-3.0", diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php index b2238ffb42423..a8543fee28d47 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php @@ -8,6 +8,7 @@ namespace Magento\GraphQl\CompareList; use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; @@ -20,14 +21,20 @@ class CompareListTest extends GraphQlAbstract private const PRODUCT_SKU_2 = 'simple2'; /** - * @var mixed + * @var ProductRepositoryInterface */ private $productRepository; + /** + * @var CustomerTokenServiceInterface + */ + private $customerTokenService; + protected function setUp(): void { $objectManager = Bootstrap::getObjectManager(); $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); } /** * Create compare list without product @@ -166,6 +173,85 @@ public function testDeleteCompareList() $this->assertFalse($response1['deleteCompareList']['result']); } + /** + * Assign compare list to customer + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testAssignCompareListToCustomer() + { + $compareList = $this->createCompareList(); + $uid = $compareList['createCompareList']['uid']; + $this->uidAssertion($uid); + $addProducts = $this->addProductsToCompareList($uid); + $this->itemsAssertion($addProducts['addProductsToCompareList']['items']); + $currentEmail = 'customer@example.com'; + $currentPassword = 'password'; + $customerQuery = <<graphQlQuery( + $customerQuery, + [], + '', + $this->getCustomerAuthHeaders($currentEmail, $currentPassword) + ); + $this->assertArrayHasKey('compare_list', $customerResponse['customer']); + $this->assertNull($customerResponse['customer']['compare_list']); + + $assignCompareListToCustomer = <<graphQlMutation( + $assignCompareListToCustomer, + [], + '', + $this->getCustomerAuthHeaders($currentEmail, $currentPassword) + ); + $this->assertTrue($assignResponse['assignCompareListToCustomer']); + + $customerAssignedResponse = $this->graphQlQuery( + $customerQuery, + [], + '', + $this->getCustomerAuthHeaders($currentEmail, $currentPassword) + ); + + $this->assertArrayHasKey('compare_list', $customerAssignedResponse['customer']); + $this->uidAssertion($customerAssignedResponse['customer']['compare_list']['uid']); + $this->itemsAssertion($customerAssignedResponse['customer']['compare_list']['items']); + } + + /** + * Get customer Header + * + * @param string $email + * @param string $password + * + * @return array + */ + private function getCustomerAuthHeaders(string $email, string $password): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); + return ['Authorization' => 'Bearer ' . $customerToken]; + } + /** * Create compare list * From 193702b275d6f812fb210ca580f03c66292ae891 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 4 Nov 2020 17:35:32 +0200 Subject: [PATCH 27/45] Fixed failed test due to ProductInterface in schema --- .../Magento/Catalog/Model/CompareListIdToMaskedListId.php | 6 +++--- .../Model/ResourceModel/Product/Compare/Item/Collection.php | 6 +++--- .../CompareListGraphQl/Model/Service/GetComparableItems.php | 2 +- .../CompareListGraphQl/Model/Service/GetCompareList.php | 2 +- app/code/Magento/CompareListGraphQl/etc/schema.graphqls | 5 ++++- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php index b915ca2f18d0b..d47be505bd623 100644 --- a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php +++ b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php @@ -41,12 +41,12 @@ public function __construct( * * @param int $listId * - * @return string + * @return null|string */ - public function execute(int $listId): string + public function execute(int $listId): ?string { $listIdMask = $this->listIdMaskFactory->create(); $this->listIdMaskResource->load($listIdMask, $listId, 'list_id'); - return $listIdMask->getMaskedId() ?? ''; + return $listIdMask->getMaskedId() ?? null; } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 59ef50af74b33..7503b5fa17e4d 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -182,9 +182,9 @@ public function setListId(int $listId) * * @return int */ - public function getListId() + public function getListId(): int { - return $this->listId; + return (int)$this->listId; } /** @@ -274,7 +274,7 @@ public function _addJoinToSelect() * * @return array */ - public function getProductsByListId(int $listId) + public function getProductsByListId(int $listId): array { $select = $this->getConnection()->select()-> from( diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 2b458db70a3b9..367d37013253d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -69,7 +69,7 @@ public function __construct( * @return array * @throws GraphQlInputException */ - public function getComparableItems(int $listId, ContextInterface $context) + public function execute(int $listId, ContextInterface $context) { $items = []; $maskedListId = $this->compareListIdToMaskedListId->execute($listId); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index 5cd785368f8a6..dbd7cd9c30da2 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -60,7 +60,7 @@ public function execute(int $listId, ContextInterface $context) $maskedListId = $this->compareListIdToMaskedListId->execute($listId); return [ 'uid' => $maskedListId, - 'items' => $this->comparableItemsService->getComparableItems($listId, $context), + 'items' => $this->comparableItemsService->execute($listId, $context), 'attributes' => $this->comparableAttributesService->execute($listId, $context) ]; } diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 8634b05aa2bdb..5f643e792def3 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -3,10 +3,13 @@ type ComparableItem { uid: ID! - product: ProductInterface! + product: ComparableProduct! attributes: [ProductAttribute]! @doc(description: "Product comparable attributes") } +type ComparableProduct implements ProductInterface { +} + type ProductAttribute { code: String! @doc(description:"Attribute code") value: String! @doc(description:"Attribute display value") From 2857996ff429192286a27206ade4ea10c5ba25c2 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 4 Nov 2020 20:26:43 +0200 Subject: [PATCH 28/45] CR recommendations --- .../Resolver/AddProductsToCompareList.php | 2 +- .../Resolver/AssignCompareListToCustomer.php | 16 ++-- .../Model/Resolver/CompareList.php | 2 +- .../Model/Resolver/CreateCompareList.php | 14 ++-- .../Model/Resolver/CustomerCompareList.php | 16 ++-- .../Model/Resolver/DeleteCompareList.php | 2 +- .../RemoveProductsFromCompareList.php | 2 +- .../Customer/GetListIdByCustomerId.php | 59 ++++++++++++++ .../Customer/SetCustomerToCompareList.php | 77 ++++++++++++++++++ .../ValidateCustomer.php} | 78 ++----------------- 10 files changed, 170 insertions(+), 98 deletions(-) create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/Customer/GetListIdByCustomerId.php create mode 100644 app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php rename app/code/Magento/CompareListGraphQl/Model/Service/{CustomerService.php => Customer/ValidateCustomer.php} (55%) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index e0b27157ff3ac..0bef548d3f82c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -74,7 +74,7 @@ public function resolve( array $args = null ) { $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); - if (!isset($args['input']['uid'])) { + if (empty($args['input']['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified.')); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index cd9f7007f8549..6d847e3f05c4c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -8,7 +8,7 @@ namespace Magento\CompareListGraphQl\Model\Resolver; use Magento\Catalog\Model\MaskedListIdToCompareListId; -use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\CompareListGraphQl\Model\Service\Customer\SetCustomerToCompareList; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -23,9 +23,9 @@ class AssignCompareListToCustomer implements ResolverInterface { /** - * @var CustomerService + * @var SetCustomerToCompareList */ - private $customerService; + private $setCustomerToCompareList; /** * @var MaskedListIdToCompareListId @@ -33,14 +33,14 @@ class AssignCompareListToCustomer implements ResolverInterface private $maskedListIdToCompareListId; /** - * @param CustomerService $customerService + * @param SetCustomerToCompareList $setCustomerToCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( - CustomerService $customerService, + SetCustomerToCompareList $setCustomerToCompareList, MaskedListIdToCompareListId $maskedListIdToCompareListId ) { - $this->customerService = $customerService; + $this->setCustomerToCompareList = $setCustomerToCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; } @@ -65,7 +65,7 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['uid'])) { + if (empty($args['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified')); } @@ -78,7 +78,7 @@ public function resolve( if ($listId) { try { - $result = $this->customerService->setCustomerToCompareList($listId, $context->getUserId()); + $result = $this->setCustomerToCompareList->execute($listId, $context->getUserId()); } catch (LocalizedException $exception) { throw new GraphQlInputException( __('Something was wrong during assigning customer.') diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index 3bb800dda5e7d..ca1314d8ec15e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -65,7 +65,7 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['uid'])) { + if (empty($args['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified')); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index dd90fab117fd1..64f706f84756a 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -9,7 +9,7 @@ use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService; -use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; @@ -31,9 +31,9 @@ class CreateCompareList implements ResolverInterface private $mathRandom; /** - * @var CustomerService + * @var GetListIdByCustomerId */ - private $customerService; + private $getListIdByCustomerId; /** * @var AddToCompareList @@ -52,20 +52,20 @@ class CreateCompareList implements ResolverInterface /** * @param Random $mathRandom - * @param CustomerService $customerService + * @param GetListIdByCustomerId $getListIdByCustomerId * @param AddToCompareList $addProductToCompareList * @param GetCompareList $getCompareList * @param CreateCompareListService $createCompareList */ public function __construct( Random $mathRandom, - CustomerService $customerService, + GetListIdByCustomerId $getListIdByCustomerId, AddToCompareList $addProductToCompareList, GetCompareList $getCompareList, CreateCompareListService $createCompareList ) { $this->mathRandom = $mathRandom; - $this->customerService = $customerService; + $this->getListIdByCustomerId = $getListIdByCustomerId; $this->addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; $this->createCompareList = $createCompareList; @@ -106,7 +106,7 @@ public function resolve( } if ($customerId) { - $listId = $this->customerService->getListIdByCustomerId($customerId); + $listId = $this->getListIdByCustomerId->execute($customerId); if ($listId) { $this->addProductToCompareList->execute($listId, $products, $storeId); } else { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php index c81a1775e82e7..84d0aad0c9a71 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CustomerCompareList.php @@ -7,7 +7,7 @@ namespace Magento\CompareListGraphQl\Model\Resolver; -use Magento\CompareListGraphQl\Model\Service\CustomerService; +use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -26,20 +26,20 @@ class CustomerCompareList implements ResolverInterface private $getCompareList; /** - * @var CustomerService + * @var GetListIdByCustomerId */ - private $customerService; + private $getListIdByCustomerId; /** - * @param GetCompareList $getCompareList - * @param CustomerService $customerService + * @param GetCompareList $getCompareList + * @param GetListIdByCustomerId $getListIdByCustomerId */ public function __construct( GetCompareList $getCompareList, - CustomerService $customerService + GetListIdByCustomerId $getListIdByCustomerId ) { $this->getCompareList = $getCompareList; - $this->customerService = $customerService; + $this->getListIdByCustomerId = $getListIdByCustomerId; } /** @@ -62,7 +62,7 @@ public function resolve( array $value = null, array $args = null ) { - $listId = $this->customerService->getListIdByCustomerId($context->getUserId()); + $listId = $this->getListIdByCustomerId->execute((int)$context->getUserId()); if (!$listId) { return null; diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index 7278c6c0ef0d1..0396cf218b687 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -74,7 +74,7 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['uid'])) { + if (empty($args['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified')); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index 0ca47c0f4b6e4..b3caee87d66d0 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -79,7 +79,7 @@ public function resolve( throw new GraphQlInputException(__('"products" value must be specified.')); } - if (!isset($args['input']['uid'])) { + if (empty($args['input']['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified.')); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/GetListIdByCustomerId.php b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/GetListIdByCustomerId.php new file mode 100644 index 0000000000000..c6437683f6ba7 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/GetListIdByCustomerId.php @@ -0,0 +1,59 @@ +compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + } + + /** + * Get listId by Customer ID + * + * @param int $customerId + * + * @return int|null + */ + public function execute(int $customerId): ?int + { + if ($customerId) { + /** @var CompareList $compareList */ + $compareList = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareList, $customerId, 'customer_id'); + return (int)$compareList->getListId(); + } + + return null; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php new file mode 100644 index 0000000000000..901a04b674594 --- /dev/null +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php @@ -0,0 +1,77 @@ +validateCustomer = $validateCustomer; + $this->compareListFactory = $compareListFactory; + $this->resourceCompareList = $resourceCompareList; + } + + /** + * Set customer to compare list + * + * @param int $listId + * @param int $customerId + * + * @return bool + * + * @throws GraphQlAuthenticationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function execute(int $listId, int $customerId): bool + { + if ($this->validateCustomer->execute($customerId)) { + /** @var CompareList $compareListModel */ + $compareList = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareList, $listId, 'list_id'); + $compareList->setCustomerId($customerId); + $this->resourceCompareList->save($compareList); + return true; + } + + return false; + } +} diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/ValidateCustomer.php similarity index 55% rename from app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php rename to app/code/Magento/CompareListGraphQl/Model/Service/Customer/ValidateCustomer.php index 201faf078d243..ab16b17240b1c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/CustomerService.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/ValidateCustomer.php @@ -5,11 +5,8 @@ */ declare(strict_types=1); -namespace Magento\CompareListGraphQl\Model\Service; +namespace Magento\CompareListGraphQl\Model\Service\Customer; -use Magento\Catalog\Model\CompareList; -use Magento\Catalog\Model\CompareListFactory; -use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Model\AuthenticationInterface; @@ -20,99 +17,38 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; /** - * Service class for customer + * Class provided customer validation */ -class CustomerService +class ValidateCustomer { /** * @var AuthenticationInterface */ private $authentication; - /** - * @var CustomerRepositoryInterface - */ - private $customerRepository; - /** * @var AccountManagementInterface */ private $accountManagement; /** - * @var ResourceCompareList - */ - private $resourceCompareList; - - /** - * @var CompareListFactory + * @var CustomerRepositoryInterface */ - private $compareListFactory; + private $customerRepository; /** * @param AuthenticationInterface $authentication * @param AccountManagementInterface $accountManagement * @param CustomerRepositoryInterface $customerRepository - * @param CompareListFactory $compareListFactory - * @param ResourceCompareList $resourceCompareList */ public function __construct( AuthenticationInterface $authentication, AccountManagementInterface $accountManagement, - CustomerRepositoryInterface $customerRepository, - CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList + CustomerRepositoryInterface $customerRepository ) { $this->authentication = $authentication; $this->accountManagement = $accountManagement; $this->customerRepository = $customerRepository; - $this->compareListFactory = $compareListFactory; - $this->resourceCompareList = $resourceCompareList; - } - - /** - * Get listId by Customer ID - * - * @param int $customerId - * - * @return int|null - */ - public function getListIdByCustomerId(int $customerId) - { - if ($customerId) { - /** @var CompareList $compareList */ - $compareList = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareList, $customerId, 'customer_id'); - return (int)$compareList->getListId(); - } - - return null; - } - - /** - * Set customer to compare list - * - * @param int $listId - * @param int $customerId - * - * @return bool - * - * @throws GraphQlAuthenticationException - * @throws GraphQlInputException - * @throws GraphQlNoSuchEntityException - */ - public function setCustomerToCompareList(int $listId, int $customerId): bool - { - if ($this->validateCustomer($customerId)) { - /** @var CompareList $compareListModel */ - $compareList = $this->compareListFactory->create(); - $this->resourceCompareList->load($compareList, $listId, 'list_id'); - $compareList->setCustomerId($customerId); - $this->resourceCompareList->save($compareList); - return true; - } - - return false; } /** @@ -126,7 +62,7 @@ public function setCustomerToCompareList(int $listId, int $customerId): bool * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException */ - public function validateCustomer(int $customerId): int + public function execute(int $customerId): int { try { $customer = $this->customerRepository->getById($customerId); From 1ec40b0f6dd513ddf50e3b7483c615dfc8cab665 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 6 Nov 2020 19:32:02 +0200 Subject: [PATCH 29/45] Removed list id mask table --- .../Model/CompareListIdToMaskedListId.php | 30 +++++++++---------- app/code/Magento/Catalog/Model/ListIdMask.php | 26 ---------------- .../Model/MaskedListIdToCompareListId.php | 28 ++++++++--------- .../Product/Compare/ListIdMask.php | 26 ---------------- app/code/Magento/Catalog/etc/db_schema.xml | 26 ++++------------ .../Catalog/etc/db_schema_whitelist.json | 17 ++--------- .../Model/Service/CreateCompareList.php | 26 ++-------------- 7 files changed, 39 insertions(+), 140 deletions(-) delete mode 100644 app/code/Magento/Catalog/Model/ListIdMask.php delete mode 100644 app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php diff --git a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php index d47be505bd623..f17244a2ed836 100644 --- a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php +++ b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php @@ -7,7 +7,7 @@ namespace Magento\Catalog\Model; -use Magento\Catalog\Model\ResourceModel\Product\Compare\ListIdMask as ListIdMaskMaskResource; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; /** * CompareListId to MaskedListId resolver @@ -15,29 +15,29 @@ class CompareListIdToMaskedListId { /** - * @var ListIdMaskFactory + * @var CompareListFactory */ - private $listIdMaskFactory; + private $compareListFactory; /** - * @var ListIdMaskMaskResource + * @var CompareListResource */ - private $listIdMaskResource; + private $compareListResource; /** - * @param ListIdMaskFactory $listIdMaskFactory - * @param ListIdMaskMaskResource $listIdMaskResource + * @param CompareListFactory $compareListFactory + * @param CompareListResource $compareListResource */ public function __construct( - ListIdMaskFactory $listIdMaskFactory, - ListIdMaskMaskResource $listIdMaskResource + CompareListFactory $compareListFactory, + CompareListResource $compareListResource ) { - $this->listIdMaskFactory = $listIdMaskFactory; - $this->listIdMaskResource = $listIdMaskResource; + $this->compareListFactory = $compareListFactory; + $this->compareListResource = $compareListResource; } /** - * Get maskedId by listId + * Get listIdMask by listId * * @param int $listId * @@ -45,8 +45,8 @@ public function __construct( */ public function execute(int $listId): ?string { - $listIdMask = $this->listIdMaskFactory->create(); - $this->listIdMaskResource->load($listIdMask, $listId, 'list_id'); - return $listIdMask->getMaskedId() ?? null; + $compareList = $this->compareListFactory->create(); + $this->compareListResource->load($compareList, $listId, 'list_id'); + return $compareList->getListIdMask() ?? null; } } diff --git a/app/code/Magento/Catalog/Model/ListIdMask.php b/app/code/Magento/Catalog/Model/ListIdMask.php deleted file mode 100644 index 06fc825a29d1b..0000000000000 --- a/app/code/Magento/Catalog/Model/ListIdMask.php +++ /dev/null @@ -1,26 +0,0 @@ -_init(ResourceModel\Product\Compare\ListIdMask::class); - } -} diff --git a/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php index 0256b33057b12..47cea9dc09ce7 100644 --- a/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php +++ b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php @@ -7,7 +7,7 @@ namespace Magento\Catalog\Model; -use Magento\Catalog\Model\ResourceModel\Product\Compare\ListIdMask as ListIdMaskMaskResource; +use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; /** * MaskedListId to ListId resolver @@ -15,25 +15,25 @@ class MaskedListIdToCompareListId { /** - * @var ListIdMaskFactory + * @var CompareListFactory */ - private $listIdMaskFactory; + private $compareListFactory; /** - * @var ListIdMaskMaskResource + * @var CompareListResource */ - private $listIdMaskResource; + private $compareListResource; /** - * @param ListIdMaskFactory $listIdMaskFactory - * @param ListIdMaskMaskResource $listIdMaskResource + * @param CompareListFactory $compareListFactory + * @param CompareListResource $compareListResource */ public function __construct( - ListIdMaskFactory $listIdMaskFactory, - ListIdMaskMaskResource $listIdMaskResource + CompareListFactory $compareListFactory, + CompareListResource $compareListResource ) { - $this->listIdMaskFactory = $listIdMaskFactory; - $this->listIdMaskResource = $listIdMaskResource; + $this->compareListFactory = $compareListFactory; + $this->compareListResource = $compareListResource; } /** @@ -45,9 +45,9 @@ public function __construct( */ public function execute(string $maskedListId): int { - $listIdMask = $this->listIdMaskFactory->create(); - $this->listIdMaskResource->load($listIdMask, $maskedListId, 'masked_id'); + $compareList = $this->compareListFactory->create(); + $this->compareListResource->load($compareList, $maskedListId, 'list_id_mask'); - return (int)$listIdMask->getListId(); + return (int)$compareList->getListId(); } } diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php deleted file mode 100644 index 54719f656403d..0000000000000 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/ListIdMask.php +++ /dev/null @@ -1,26 +0,0 @@ -_init('list_id_mask', 'entity_id'); - } -} diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index 1bd59e6236d69..d601a882cb25e 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -580,35 +580,21 @@
+ + - - + + -
- - - - - - - - - - - - - - + +
compareListFactory = $compareListFactory; $this->compareListResource = $compareListResource; - $this->maskedListIdFactory = $maskedListIdFactory; - $this->maskedListIdResource = $maskedListIdResource; } /** @@ -66,14 +48,10 @@ public function __construct( public function execute(string $maskedId, ?int $customerId = null): int { $compareList = $this->compareListFactory->create(); + $compareList->setListIdMask($maskedId); $compareList->setCustomerId($customerId); $this->compareListResource->save($compareList); - $maskedListId = $this->maskedListIdFactory->create(); - $maskedListId->setListId($compareList->getListId()); - $maskedListId->setMaskedId($maskedId); - $this->maskedListIdResource->save($maskedListId); - return (int)$compareList->getListId(); } } From 8780f6673ec4d89db6474f5eee50d083de3bd7d0 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 6 Nov 2020 20:05:19 +0200 Subject: [PATCH 30/45] add additional check --- .../Resolver/AddProductsToCompareList.php | 20 ++++++++- .../Model/Resolver/DeleteCompareList.php | 44 ++++++++++++++++--- .../RemoveProductsFromCompareList.php | 36 ++++++++++++++- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 0bef548d3f82c..689676aa8a81b 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -9,6 +9,7 @@ use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\AddToCompareList; +use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -37,19 +38,27 @@ class AddProductsToCompareList implements ResolverInterface */ private $maskedListIdToCompareListId; + /** + * @var GetListIdByCustomerId + */ + private $getListIdByCustomerId; + /** * @param AddToCompareList $addProductToCompareList * @param GetCompareList $getCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId + * @param GetListIdByCustomerId $getListIdByCustomerId */ public function __construct( AddToCompareList $addProductToCompareList, GetCompareList $getCompareList, - MaskedListIdToCompareListId $maskedListIdToCompareListId + MaskedListIdToCompareListId $maskedListIdToCompareListId, + GetListIdByCustomerId $getListIdByCustomerId ) { $this->addProductToCompareList = $addProductToCompareList; $this->getCompareList = $getCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + $this->getListIdByCustomerId = $getListIdByCustomerId; } /** @@ -88,6 +97,15 @@ public function resolve( throw new GraphQlInputException(__('"uid" value does not exist')); } + if ($userId = $context->getUserId()) { + $customerListId = $this->getListIdByCustomerId->execute($userId); + if ($listId === $customerListId) { + $this->addProductToCompareList->execute($customerListId, $args['input']['products'], $storeId); + + return $this->getCompareList->execute($customerListId, $context); + } + } + $this->addProductToCompareList->execute($listId, $args['input']['products'], $storeId); return $this->getCompareList->execute($listId, $context); diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index 0396cf218b687..80ed98e6c776c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -10,6 +10,7 @@ use Magento\Catalog\Model\CompareListFactory; use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; +use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -38,19 +39,27 @@ class DeleteCompareList implements ResolverInterface */ private $maskedListIdToCompareListId; + /** + * @var GetListIdByCustomerId + */ + private $getListIdByCustomerId; + /** * @param CompareListFactory $compareListFactory * @param CompareListResource $compareListResource * @param MaskedListIdToCompareListId $maskedListIdToCompareListId + * @param GetListIdByCustomerId $getListIdByCustomerId */ public function __construct( CompareListFactory $compareListFactory, CompareListResource $compareListResource, - MaskedListIdToCompareListId $maskedListIdToCompareListId + MaskedListIdToCompareListId $maskedListIdToCompareListId, + GetListIdByCustomerId $getListIdByCustomerId ) { $this->compareListFactory = $compareListFactory; $this->compareListResource = $compareListResource; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + $this->getListIdByCustomerId = $getListIdByCustomerId; } /** @@ -81,12 +90,22 @@ public function resolve( $listId = $this->maskedListIdToCompareListId->execute($args['uid']); $removed = ['result' => false]; + if ($userId = $context->getUserId()) { + $customerListId = $this->getListIdByCustomerId->execute($userId); + if ($listId === $customerListId) { + try { + $removed['result'] = $this->deleteCompareList($customerListId); + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during removing compare list') + ); + } + } + } + if ($listId) { try { - $compareList = $this->compareListFactory->create(); - $compareList->setListId($listId); - $this->compareListResource->delete($compareList); - $removed['result'] = true; + $removed['result'] = $this->deleteCompareList($listId); } catch (LocalizedException $exception) { throw new GraphQlInputException( __('Something was wrong during removing compare list') @@ -96,4 +115,19 @@ public function resolve( return $removed; } + + /** + * Delete compare list + * + * @param int|null $listId + * @return bool + */ + private function deleteCompareList(?int $listId): bool + { + $compareList = $this->compareListFactory->create(); + $compareList->setListId($listId); + $this->compareListResource->delete($compareList); + + return true; + } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index b3caee87d66d0..2e78e351ae64c 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -8,6 +8,7 @@ namespace Magento\CompareListGraphQl\Model\Resolver; use Magento\Catalog\Model\MaskedListIdToCompareListId; +use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\CompareListGraphQl\Model\Service\RemoveFromCompareList; use Magento\Framework\Exception\LocalizedException; @@ -38,19 +39,27 @@ class RemoveProductsFromCompareList implements ResolverInterface */ private $maskedListIdToCompareListId; + /** + * @var GetListIdByCustomerId + */ + private $getListIdByCustomerId; + /** * @param GetCompareList $getCompareList * @param RemoveFromCompareList $removeFromCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId + * @param GetListIdByCustomerId $getListIdByCustomerId */ public function __construct( GetCompareList $getCompareList, RemoveFromCompareList $removeFromCompareList, - MaskedListIdToCompareListId $maskedListIdToCompareListId + MaskedListIdToCompareListId $maskedListIdToCompareListId, + GetListIdByCustomerId $getListIdByCustomerId ) { $this->getCompareList = $getCompareList; $this->removeFromCompareList = $removeFromCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + $this->getListIdByCustomerId = $getListIdByCustomerId; } /** @@ -89,6 +98,13 @@ public function resolve( throw new GraphQlInputException(__('"uid" value does not exist')); } + if ($userId = $context->getUserId()) { + $customerListId = $this->getListIdByCustomerId->execute($userId); + if ($listId === $customerListId) { + $this->removeFromCompareList($customerListId, $args); + } + } + try { $this->removeFromCompareList->execute($listId, $args['input']['products']); } catch (LocalizedException $exception) { @@ -99,4 +115,22 @@ public function resolve( return $this->getCompareList->execute($listId, $context); } + + /** + * Remove products from compare list + * + * @param int $listId + * @param array $args + * @throws GraphQlInputException + */ + private function removeFromCompareList(int $listId, array $args): void + { + try { + $this->removeFromCompareList->execute($listId, $args['input']['products']); + } catch (LocalizedException $exception) { + throw new GraphQlInputException( + __('Something was wrong during removing products from compare list') + ); + } + } } From dd5b44326784229d75136da44785c4f03e298261 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 6 Nov 2020 21:35:53 +0200 Subject: [PATCH 31/45] luma compatibility add list id for customer compare list --- .../Product/Compare/Item/Collection.php | 26 +++++++++++++++++++ .../Resolver/AddProductsToCompareList.php | 5 ++-- .../Model/Resolver/CreateCompareList.php | 7 +++-- .../Model/Service/AddToCompareList.php | 14 ++++++++-- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 7503b5fa17e4d..3f1a1d88211ff 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -287,6 +287,32 @@ public function getProductsByListId(int $listId): array return $this->getConnection()->fetchCol($select); } + /** + * Set list_id for customer compare item + * + * @param int $listId + * @param int $customerId + */ + public function setListIdToCustomerCompareItems(int $listId, int $customerId) + { + $table = $this->getTable('catalog_compare_item'); + $select = $this->getConnection()->select()-> + from( + $table + )->where( + 'customer_id = ?', + $customerId + ); + $customerCompareItems = $this->getConnection()->fetchCol($select); + foreach ($customerCompareItems as $itemId) { + $this->getConnection()->update( + $table, + ['list_id' => $listId], + ['catalog_compare_item_id = ?' => (int)$itemId] + ); + } + } + /** * Retrieve comapre products attribute set ids * diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 689676aa8a81b..04d4ed8e1424e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -82,7 +82,6 @@ public function resolve( array $value = null, array $args = null ) { - $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); if (empty($args['input']['uid'])) { throw new GraphQlInputException(__('"uid" value must be specified.')); } @@ -100,13 +99,13 @@ public function resolve( if ($userId = $context->getUserId()) { $customerListId = $this->getListIdByCustomerId->execute($userId); if ($listId === $customerListId) { - $this->addProductToCompareList->execute($customerListId, $args['input']['products'], $storeId); + $this->addProductToCompareList->execute($customerListId, $args['input']['products'], $context); return $this->getCompareList->execute($customerListId, $context); } } - $this->addProductToCompareList->execute($listId, $args['input']['products'], $storeId); + $this->addProductToCompareList->execute($listId, $args['input']['products'], $context); return $this->getCompareList->execute($listId, $context); } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php index 64f706f84756a..9b0e8fd18298f 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php @@ -95,23 +95,22 @@ public function resolve( ) { $customerId = $context->getUserId(); $products = !empty($args['input']['products']) ? $args['input']['products'] : []; - $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); $generatedListId = $this->mathRandom->getUniqueHash(); $listId = 0; try { if ((0 === $customerId || null === $customerId)) { $listId = $this->createCompareList->execute($generatedListId); - $this->addProductToCompareList->execute($listId, $products, $storeId); + $this->addProductToCompareList->execute($listId, $products, $context); } if ($customerId) { $listId = $this->getListIdByCustomerId->execute($customerId); if ($listId) { - $this->addProductToCompareList->execute($listId, $products, $storeId); + $this->addProductToCompareList->execute($listId, $products, $context); } else { $listId = $this->createCompareList->execute($generatedListId, $customerId); - $this->addProductToCompareList->execute($listId, $products, $storeId); + $this->addProductToCompareList->execute($listId, $products, $context); } } } catch (LocalizedException $exception) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php index 88893b23c3d65..ef1f0ab46e51f 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php @@ -11,6 +11,7 @@ use Magento\Catalog\Model\ProductRepository; use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** * Service add product to compare list @@ -52,18 +53,23 @@ public function __construct( * * @param int $listId * @param array $products - * @param int $storeId + * @param ContextInterface $context * * @return int */ - public function execute(int $listId, array $products, int $storeId): int + public function execute(int $listId, array $products, ContextInterface $context): int { + $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); + $customerId = $context->getUserId(); if (count($products)) { $existedProducts = $this->itemCollection->getProductsByListId($listId); foreach ($products as $productId) { if (array_search($productId, $existedProducts) === false) { if ($this->productExists($productId)) { $item = $this->compareItemFactory->create(); + if ($customerId) { + $item->setCustomerId($customerId); + } $item->addProductData($productId); $item->setStoreId($storeId); $item->setListId($listId); @@ -73,6 +79,10 @@ public function execute(int $listId, array $products, int $storeId): int } } + if ($customerId) { + $this->itemCollection->setListIdToCustomerCompareItems($listId, $customerId); + } + return (int)$listId; } From 2e48ca1ea7c63372cc3e43a27c467638b6183350 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 6 Nov 2020 23:57:18 +0200 Subject: [PATCH 32/45] luma compatibility remove compare list --- .../Controller/Product/Compare/Clear.php | 1 + .../Controller/Product/Compare/Remove.php | 4 ++ .../Product/Compare/Item/Collection.php | 52 +++++++++++++++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php b/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php index 2703e9869bd47..81f63f3e75c54 100644 --- a/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php +++ b/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php @@ -31,6 +31,7 @@ public function execute() try { $items->clear(); + $items->removeCompareList($this->_customerSession->getCustomerId()); $this->messageManager->addSuccessMessage(__('You cleared the comparison list.')); $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class)->calculate(); } catch (\Magento\Framework\Exception\LocalizedException $e) { diff --git a/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php b/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php index f5d56dc9e6b0e..23bb7e0e3d459 100644 --- a/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php +++ b/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php @@ -7,6 +7,7 @@ namespace Magento\Catalog\Controller\Product\Compare; use Magento\Catalog\Model\Product\Attribute\Source\Status; +use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; use Magento\Framework\Exception\NoSuchEntityException; @@ -49,6 +50,9 @@ public function execute() $helper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class); if ($item->getId()) { $item->delete(); + /** @var Collection $items */ + $items = $this->_itemCollectionFactory->create(); + $items->removeCompareList($this->_customerSession->getCustomerId()); $productName = $this->_objectManager->get(\Magento\Framework\Escaper::class) ->escapeHtml($product->getName()); $this->messageManager->addSuccessMessage( diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 3f1a1d88211ff..99ce8feb451e9 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -287,6 +287,7 @@ public function getProductsByListId(int $listId): array return $this->getConnection()->fetchCol($select); } + /** * Set list_id for customer compare item * @@ -295,24 +296,53 @@ public function getProductsByListId(int $listId): array */ public function setListIdToCustomerCompareItems(int $listId, int $customerId) { - $table = $this->getTable('catalog_compare_item'); - $select = $this->getConnection()->select()-> - from( - $table - )->where( - 'customer_id = ?', - $customerId - ); - $customerCompareItems = $this->getConnection()->fetchCol($select); - foreach ($customerCompareItems as $itemId) { + foreach ($this->getCustomerCompareItems($customerId) as $itemId) { $this->getConnection()->update( - $table, + $this->getTable('catalog_compare_item'), ['list_id' => $listId], ['catalog_compare_item_id = ?' => (int)$itemId] ); } } + /** + * Remove compare list if customer compare list empty + * + * @param int $customerId + */ + public function removeCompareList(int $customerId) + { + if (empty($this->getCustomerCompareItems($customerId))) { + $this->getConnection()->delete( + $this->getTable('catalog_compare_list'), + ['customer_id = ?' => $customerId] + ); + } + } + + /** + * Get customer compare items + * + * @param int|null $customerId + * @return array + */ + private function getCustomerCompareItems(?int $customerId): array + { + if ($customerId) { + $select = $this->getConnection()->select()-> + from( + $this->getTable('catalog_compare_item') + )->where( + 'customer_id = ?', + $customerId + ); + + return $this->getConnection()->fetchCol($select); + } + + return []; + } + /** * Retrieve comapre products attribute set ids * From 098a40792e4d11d6bf3daa6bc08fc3c1d03e845f Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Sat, 7 Nov 2020 11:21:30 +0200 Subject: [PATCH 33/45] minor changes --- .../Model/ResourceModel/Product/Compare/Item/Collection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php index 99ce8feb451e9..76f566a364769 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php @@ -308,9 +308,9 @@ public function setListIdToCustomerCompareItems(int $listId, int $customerId) /** * Remove compare list if customer compare list empty * - * @param int $customerId + * @param int|null $customerId */ - public function removeCompareList(int $customerId) + public function removeCompareList(?int $customerId) { if (empty($this->getCustomerCompareItems($customerId))) { $this->getConnection()->delete( From b21be308c2a369bcbcde2db5921faaf8b2b237af Mon Sep 17 00:00:00 2001 From: Prabhu Ram Date: Wed, 11 Nov 2020 17:50:28 -0600 Subject: [PATCH 34/45] - Modified assigncomparelust output to match schema in architecture - Added validations for list_id of one customer cannot be accessed by other customer - Added item_count - tests pending for above --- .../Model/CompareListIdToMaskedListId.php | 8 +++++- .../Model/MaskedListIdToCompareListId.php | 10 ++++--- .../Resolver/AddProductsToCompareList.php | 21 ++++++++------- .../Resolver/AssignCompareListToCustomer.php | 27 ++++++++++++++++--- .../Model/Resolver/CompareList.php | 7 ++++- .../Model/Resolver/DeleteCompareList.php | 6 ++++- .../RemoveProductsFromCompareList.php | 6 ++++- .../Model/Service/AddToCompareList.php | 1 + .../Customer/SetCustomerToCompareList.php | 8 +++--- .../Model/Service/GetComparableItems.php | 15 ++--------- .../Model/Service/GetCompareList.php | 14 +++++++--- .../CompareListGraphQl/etc/schema.graphqls | 13 +++++---- .../GraphQl/CompareList/CompareListTest.php | 22 ++++++++++----- 13 files changed, 105 insertions(+), 53 deletions(-) diff --git a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php index f17244a2ed836..a911980b98894 100644 --- a/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php +++ b/app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php @@ -8,6 +8,7 @@ namespace Magento\Catalog\Model; use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; +use Magento\Framework\Exception\LocalizedException; /** * CompareListId to MaskedListId resolver @@ -41,12 +42,17 @@ public function __construct( * * @param int $listId * + * @param int|null $customerId * @return null|string + * @throws LocalizedException */ - public function execute(int $listId): ?string + public function execute(int $listId, int $customerId = null): ?string { $compareList = $this->compareListFactory->create(); $this->compareListResource->load($compareList, $listId, 'list_id'); + if ((int)$compareList->getCustomerId() !== (int)$customerId) { + throw new LocalizedException(__('This customer is not authorized to access this list')); + } return $compareList->getListIdMask() ?? null; } } diff --git a/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php index 47cea9dc09ce7..cd1506c970763 100644 --- a/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php +++ b/app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php @@ -8,6 +8,7 @@ namespace Magento\Catalog\Model; use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource; +use Magento\Framework\Exception\LocalizedException; /** * MaskedListId to ListId resolver @@ -40,14 +41,17 @@ public function __construct( * Get maskedId by listId * * @param string $maskedListId - * + * @param int $customerId * @return int + * @throws LocalizedException */ - public function execute(string $maskedListId): int + public function execute(string $maskedListId, int $customerId = null): int { $compareList = $this->compareListFactory->create(); $this->compareListResource->load($compareList, $maskedListId, 'list_id_mask'); - + if ((int)$compareList->getCustomerId() !== (int)$customerId) { + throw new LocalizedException(__('This customer is not authorized to access this list')); + } return (int)$compareList->getListId(); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php index 04d4ed8e1424e..2aab3a0cb266e 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php @@ -11,6 +11,7 @@ use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -90,23 +91,23 @@ public function resolve( throw new GraphQlInputException(__('"products" value must be specified.')); } - $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']); + try { + $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid'], $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } + if (!$listId) { throw new GraphQlInputException(__('"uid" value does not exist')); } - if ($userId = $context->getUserId()) { - $customerListId = $this->getListIdByCustomerId->execute($userId); - if ($listId === $customerListId) { - $this->addProductToCompareList->execute($customerListId, $args['input']['products'], $context); - - return $this->getCompareList->execute($customerListId, $context); - } + try { + $this->addProductToCompareList->execute($listId, $args['input']['products'], $context); + } catch (\Exception $exception) { + throw new GraphQlInputException(__($exception->getMessage())); } - $this->addProductToCompareList->execute($listId, $args['input']['products'], $context); - return $this->getCompareList->execute($listId, $context); } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index 6d847e3f05c4c..d3597ebdb1021 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -9,6 +9,7 @@ use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\Customer\SetCustomerToCompareList; +use Magento\CompareListGraphQl\Model\Service\GetCompareList; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -32,16 +33,23 @@ class AssignCompareListToCustomer implements ResolverInterface */ private $maskedListIdToCompareListId; + /** + * @var GetCompareList + */ + private $getCompareList; + /** * @param SetCustomerToCompareList $setCustomerToCompareList * @param MaskedListIdToCompareListId $maskedListIdToCompareListId */ public function __construct( SetCustomerToCompareList $setCustomerToCompareList, - MaskedListIdToCompareListId $maskedListIdToCompareListId + MaskedListIdToCompareListId $maskedListIdToCompareListId, + GetCompareList $getCompareList ) { $this->setCustomerToCompareList = $setCustomerToCompareList; $this->maskedListIdToCompareListId = $maskedListIdToCompareListId; + $this->getCompareList = $getCompareList; } /** @@ -73,12 +81,21 @@ public function resolve( throw new GraphQlInputException(__('Customer must be logged')); } - $listId = $this->maskedListIdToCompareListId->execute($args['uid']); - $result = false; + try { + $listId = $this->maskedListIdToCompareListId->execute($args['uid']); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } if ($listId) { try { $result = $this->setCustomerToCompareList->execute($listId, $context->getUserId()); + if ($result) { + return [ + 'result' => true, + 'compare_list' => $this->getCompareList->execute($listId, $context) + ]; + } } catch (LocalizedException $exception) { throw new GraphQlInputException( __('Something was wrong during assigning customer.') @@ -86,6 +103,8 @@ public function resolve( } } - return $result; + return [ + 'result' => false + ]; } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php index ca1314d8ec15e..861f3ce36d555 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/CompareList.php @@ -9,6 +9,7 @@ use Magento\Catalog\Model\MaskedListIdToCompareListId; use Magento\CompareListGraphQl\Model\Service\GetCompareList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -69,7 +70,11 @@ public function resolve( throw new GraphQlInputException(__('"uid" value must be specified')); } - $listId = $this->maskedListIdToCompareListId->execute($args['uid']); + try { + $listId = $this->maskedListIdToCompareListId->execute($args['uid'], $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } if (!$listId) { return null; diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php index 80ed98e6c776c..a811478718985 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/DeleteCompareList.php @@ -87,7 +87,11 @@ public function resolve( throw new GraphQlInputException(__('"uid" value must be specified')); } - $listId = $this->maskedListIdToCompareListId->execute($args['uid']); + try { + $listId = $this->maskedListIdToCompareListId->execute($args['uid'], $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } $removed = ['result' => false]; if ($userId = $context->getUserId()) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php index 2e78e351ae64c..6e4e4d8951cb9 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php @@ -92,7 +92,11 @@ public function resolve( throw new GraphQlInputException(__('"uid" value must be specified.')); } - $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid']); + try { + $listId = $this->maskedListIdToCompareListId->execute($args['input']['uid'], $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } if (!$listId) { throw new GraphQlInputException(__('"uid" value does not exist')); diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php index ef1f0ab46e51f..1a0aace67b6c5 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php @@ -56,6 +56,7 @@ public function __construct( * @param ContextInterface $context * * @return int + * @throws \Exception */ public function execute(int $listId, array $products, ContextInterface $context): int { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php index 901a04b674594..c496a0ced5933 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php @@ -55,13 +55,13 @@ public function __construct( * @param int $listId * @param int $customerId * - * @return bool + * @return CompareList * * @throws GraphQlAuthenticationException * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException */ - public function execute(int $listId, int $customerId): bool + public function execute(int $listId, int $customerId): ?CompareList { if ($this->validateCustomer->execute($customerId)) { /** @var CompareList $compareListModel */ @@ -69,9 +69,9 @@ public function execute(int $listId, int $customerId): bool $this->resourceCompareList->load($compareList, $listId, 'list_id'); $compareList->setCustomerId($customerId); $this->resourceCompareList->save($compareList); - return true; + return $compareList; } - return false; + return null; } } diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php index 367d37013253d..1cf42553718fd 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetComparableItems.php @@ -8,14 +8,12 @@ namespace Magento\CompareListGraphQl\Model\Service; use Magento\Catalog\Block\Product\Compare\ListCompare; -use Magento\Catalog\Model\CompareListIdToMaskedListId; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductRepository; use Magento\CompareListGraphQl\Model\Service\Collection\GetComparableItemsCollection as ComparableItemsCollection; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; -use Magento\Store\Api\Data\StoreInterface; /** * Get comparable products @@ -37,27 +35,19 @@ class GetComparableItems */ private $productRepository; - /** - * @var CompareListIdToMaskedListId - */ - private $compareListIdToMaskedListId; - /** * @param ListCompare $listCompare * @param ComparableItemsCollection $comparableItemsCollection * @param ProductRepository $productRepository - * @param CompareListIdToMaskedListId $compareListIdToMaskedListId */ public function __construct( ListCompare $listCompare, ComparableItemsCollection $comparableItemsCollection, - ProductRepository $productRepository, - CompareListIdToMaskedListId $compareListIdToMaskedListId + ProductRepository $productRepository ) { $this->blockListCompare = $listCompare; $this->comparableItemsCollection = $comparableItemsCollection; $this->productRepository = $productRepository; - $this->compareListIdToMaskedListId = $compareListIdToMaskedListId; } /** @@ -72,11 +62,10 @@ public function __construct( public function execute(int $listId, ContextInterface $context) { $items = []; - $maskedListId = $this->compareListIdToMaskedListId->execute($listId); foreach ($this->comparableItemsCollection->execute($listId, $context) as $item) { /** @var Product $item */ $items[] = [ - 'uid' => $maskedListId, + 'uid' => $item->getId(), 'product' => $this->getProductData((int)$item->getId()), 'attributes' => $this->getProductComparableAttributes($listId, $item, $context) ]; diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php index dbd7cd9c30da2..5cef555479838 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/GetCompareList.php @@ -8,6 +8,7 @@ namespace Magento\CompareListGraphQl\Model\Service; use Magento\Catalog\Model\CompareListIdToMaskedListId; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -57,11 +58,18 @@ public function __construct( */ public function execute(int $listId, ContextInterface $context) { - $maskedListId = $this->compareListIdToMaskedListId->execute($listId); + try { + $maskedListId = $this->compareListIdToMaskedListId->execute($listId, $context->getUserId()); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } + $comparableItems = $this->comparableItemsService->execute($listId, $context); + return [ 'uid' => $maskedListId, - 'items' => $this->comparableItemsService->execute($listId, $context), - 'attributes' => $this->comparableAttributesService->execute($listId, $context) + 'items' => $comparableItems, + 'attributes' => $this->comparableAttributesService->execute($listId, $context), + 'item_count' => count($comparableItems) ]; } } diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 5f643e792def3..31b4743a30b51 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -3,13 +3,10 @@ type ComparableItem { uid: ID! - product: ComparableProduct! + product: ProductInterface! attributes: [ProductAttribute]! @doc(description: "Product comparable attributes") } -type ComparableProduct implements ProductInterface { -} - type ProductAttribute { code: String! @doc(description:"Attribute code") value: String! @doc(description:"Attribute display value") @@ -24,6 +21,7 @@ type CompareList { uid: ID! @doc(description: "Compare list id") items: [ComparableItem] @doc(description: "Comparable products") attributes: [ComparableAttribute] @doc(description: "Comparable attributes, provides codes and titles for the attributes") + item_count: Int! } type Customer { @@ -38,7 +36,7 @@ type Mutation { createCompareList(input: CreateCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CreateCompareList") @doc(description: "Creates a new compare list. For a logged in user, the created list is assigned to the user") addProductsToCompareList(input: AddProductsToCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AddProductsToCompareList") @doc(description: "Add products to compare list") removeProductsFromCompareList(input: RemoveProductsFromCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\RemoveProductsFromCompareList") @doc(description: "Remove products from compare list") - assignCompareListToCustomer(uid: ID!): Boolean @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign compare list to customer") + assignCompareListToCustomer(uid: ID!): AssignCompareListToCustomerOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign compare list to customer") deleteCompareList(uid: ID!): DeleteCompareListOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\DeleteCompareList") @doc(description: "Delete compare list") } @@ -59,3 +57,8 @@ input RemoveProductsFromCompareListInput { type DeleteCompareListOutput { result: Boolean! } + +type AssignCompareListToCustomerOutput { + result: Boolean! + compare_list: CompareList +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php index a8543fee28d47..3f522199974cd 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php @@ -62,7 +62,7 @@ public function testCreateCompareListWithProducts() uid items { product { - sku + sku } } } @@ -110,7 +110,7 @@ public function testRemoveProductFromCompareList() uid items { product { - sku + sku } } } @@ -138,7 +138,7 @@ public function testGetCompareList() uid items { product { - sku + sku } } } @@ -197,7 +197,7 @@ public function testAssignCompareListToCustomer() uid items { product { - sku + sku } } } @@ -215,7 +215,15 @@ public function testAssignCompareListToCustomer() $assignCompareListToCustomer = <<graphQlMutation( @@ -224,7 +232,7 @@ public function testAssignCompareListToCustomer() '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword) ); - $this->assertTrue($assignResponse['assignCompareListToCustomer']); + $this->assertTrue($assignResponse['assignCompareListToCustomer']['result']); $customerAssignedResponse = $this->graphQlQuery( $customerQuery, @@ -286,7 +294,7 @@ private function addProductsToCompareList($uid): array uid items { product { - sku + sku } } } From 27a592cf38b501bcf05d9ef2e5401a9cff7e986b Mon Sep 17 00:00:00 2001 From: Prabhu Ram Date: Thu, 12 Nov 2020 17:39:59 -0600 Subject: [PATCH 35/45] - Updated assigncomparelist impl - On assigning a compare list "A" to customer, if there already exist a list "B" for the customer, then all the items in "A" should be added to "B" and list "A" should be deleted --- .../Resolver/AssignCompareListToCustomer.php | 4 +- .../Customer/SetCustomerToCompareList.php | 44 ++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php index d3597ebdb1021..8c43fcf5e9299 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php +++ b/app/code/Magento/CompareListGraphQl/Model/Resolver/AssignCompareListToCustomer.php @@ -89,11 +89,11 @@ public function resolve( if ($listId) { try { - $result = $this->setCustomerToCompareList->execute($listId, $context->getUserId()); + $result = $this->setCustomerToCompareList->execute($listId, $context->getUserId(), $context); if ($result) { return [ 'result' => true, - 'compare_list' => $this->getCompareList->execute($listId, $context) + 'compare_list' => $this->getCompareList->execute((int)$result->getListId(), $context) ]; } } catch (LocalizedException $exception) { diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php index c496a0ced5933..72216c6c70a16 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php @@ -10,9 +10,13 @@ use Magento\Catalog\Model\CompareList; use Magento\Catalog\Model\CompareListFactory; use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; +use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; +use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory; +use Magento\CompareListGraphQl\Model\Service\AddToCompareList; use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; /** * Assign customer to compare list @@ -34,6 +38,26 @@ class SetCustomerToCompareList */ private $resourceCompareList; + /** + * @var GetListIdByCustomerId + */ + private $getListIdByCustomerId; + + /** + * @var Collection + */ + private $items; + + /** + * @var CompareItemsCollectionFactory + */ + private $itemCollectionFactory; + + /** + * @var AddToCompareList + */ + private $addProductToCompareList; + /** * @param ValidateCustomer $validateCustomer * @param CompareListFactory $compareListFactory @@ -42,11 +66,17 @@ class SetCustomerToCompareList public function __construct( ValidateCustomer $validateCustomer, CompareListFactory $compareListFactory, - ResourceCompareList $resourceCompareList + ResourceCompareList $resourceCompareList, + GetListIdByCustomerId $getListIdByCustomerId, + CompareItemsCollectionFactory $itemCollectionFactory, + AddToCompareList $addProductToCompareList ) { $this->validateCustomer = $validateCustomer; $this->compareListFactory = $compareListFactory; $this->resourceCompareList = $resourceCompareList; + $this->getListIdByCustomerId = $getListIdByCustomerId; + $this->itemCollectionFactory = $itemCollectionFactory; + $this->addProductToCompareList = $addProductToCompareList; } /** @@ -61,12 +91,22 @@ public function __construct( * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException */ - public function execute(int $listId, int $customerId): ?CompareList + public function execute(int $listId, int $customerId, ContextInterface $context): ?CompareList { if ($this->validateCustomer->execute($customerId)) { /** @var CompareList $compareListModel */ $compareList = $this->compareListFactory->create(); + $customerListId = $this->getListIdByCustomerId->execute($customerId); $this->resourceCompareList->load($compareList, $listId, 'list_id'); + if ($customerListId) { + $this->items = $this->itemCollectionFactory->create(); + $products = $this->items->getProductsByListId($listId); + $this->addProductToCompareList->execute($customerListId, $products, $context); + $this->resourceCompareList->delete($compareList); + $compareList = $this->compareListFactory->create(); + $this->resourceCompareList->load($compareList, $customerListId, 'list_id'); + return $compareList; + } $compareList->setCustomerId($customerId); $this->resourceCompareList->save($compareList); return $compareList; From 923c723e840e9c79f5e757b4517e4172e04faaa2 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 13 Nov 2020 10:24:53 +0200 Subject: [PATCH 36/45] Update schema description --- .../CompareListGraphQl/etc/schema.graphqls | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 31b4743a30b51..510a9250bb7d3 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -2,60 +2,60 @@ # See COPYING.txt for license details. type ComparableItem { - uid: ID! + uid: ID! @doc(description: "The unique ID of an item in a compare list") product: ProductInterface! - attributes: [ProductAttribute]! @doc(description: "Product comparable attributes") + attributes: [ProductAttribute]! @doc(description: "An array of product attributes that can be used to compare products") } type ProductAttribute { - code: String! @doc(description:"Attribute code") - value: String! @doc(description:"Attribute display value") + code: String! @doc(description: "The unique identifier for a product attribute code.") + value: String! @doc(description:"The display value of the attribute") } type ComparableAttribute { - code: String! @doc(description: "Attribute code") - label: String! @doc(description: "Attribute label") + code: String! @doc(description: "An attribute code that is enabled for product comparisons") + label: String! @doc(description: "The label of the attribute code") } type CompareList { - uid: ID! @doc(description: "Compare list id") - items: [ComparableItem] @doc(description: "Comparable products") - attributes: [ComparableAttribute] @doc(description: "Comparable attributes, provides codes and titles for the attributes") + uid: ID! @doc(description: "The unique ID assigned to the compare list") + items: [ComparableItem] @doc(description: "An array of products to compare") + attributes: [ComparableAttribute] @doc(description: "An array of attributes that can be used for comparing products") item_count: Int! } type Customer { - compare_list: CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CustomerCompareList") @doc(description: "Active customers compare list") + compare_list: CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CustomerCompareList") @doc(description: "The contents of the customer's compare list") } type Query { - compareList(uid: ID!): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CompareList") @doc(description: "Compare list") + compareList(uid: ID!): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CompareList") @doc(description: "Return products that have been added to the specified compare list") } type Mutation { - createCompareList(input: CreateCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CreateCompareList") @doc(description: "Creates a new compare list. For a logged in user, the created list is assigned to the user") - addProductsToCompareList(input: AddProductsToCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AddProductsToCompareList") @doc(description: "Add products to compare list") - removeProductsFromCompareList(input: RemoveProductsFromCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\RemoveProductsFromCompareList") @doc(description: "Remove products from compare list") + createCompareList(input: CreateCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CreateCompareList") @doc(description: "Creates a new compare list. The compare list is saved for logged in customers") + addProductsToCompareList(input: AddProductsToCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AddProductsToCompareList") @doc(description: "Add products to the specified compare list") + removeProductsFromCompareList(input: RemoveProductsFromCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\RemoveProductsFromCompareList") @doc(description: "Remove products from the specified compare list") assignCompareListToCustomer(uid: ID!): AssignCompareListToCustomerOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign compare list to customer") - deleteCompareList(uid: ID!): DeleteCompareListOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\DeleteCompareList") @doc(description: "Delete compare list") + deleteCompareList(uid: ID!): DeleteCompareListOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\DeleteCompareList") @doc(description: "Delete the specified compare list") } input CreateCompareListInput { - products: [ID!] + products: [ID!] @doc(description: "An array of product IDs to add to the compare list") } input AddProductsToCompareListInput { - uid: ID!, - products: [ID!]! + uid: ID!, @doc(description: "The unique identifier of the compare list to modify") + products: [ID!]! @doc(description: "An array of product IDs to add to the compare list") } input RemoveProductsFromCompareListInput { - uid: ID!, - products: [ID!]! + uid: ID!, @doc(description: "The unique identifier of the compare list to modify") + products: [ID!]! @doc(description: "An array of product IDs to remove from the compare list") } type DeleteCompareListOutput { - result: Boolean! + result: Boolean! @doc(description: "Indicates whether the compare list was successfully deleted") } type AssignCompareListToCustomerOutput { From 34790b045fdf2caffbf19f7469fd8b5c56ca3a0b Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 13 Nov 2020 12:05:32 +0200 Subject: [PATCH 37/45] minor changes --- app/code/Magento/CompareListGraphQl/etc/schema.graphqls | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 510a9250bb7d3..ebae3ca0ff940 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -3,7 +3,7 @@ type ComparableItem { uid: ID! @doc(description: "The unique ID of an item in a compare list") - product: ProductInterface! + product: ProductInterface! @doc(description: "Contains details about a product in a compare list") attributes: [ProductAttribute]! @doc(description: "An array of product attributes that can be used to compare products") } @@ -36,7 +36,7 @@ type Mutation { createCompareList(input: CreateCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\CreateCompareList") @doc(description: "Creates a new compare list. The compare list is saved for logged in customers") addProductsToCompareList(input: AddProductsToCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AddProductsToCompareList") @doc(description: "Add products to the specified compare list") removeProductsFromCompareList(input: RemoveProductsFromCompareListInput): CompareList @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\RemoveProductsFromCompareList") @doc(description: "Remove products from the specified compare list") - assignCompareListToCustomer(uid: ID!): AssignCompareListToCustomerOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign compare list to customer") + assignCompareListToCustomer(uid: ID!): AssignCompareListToCustomerOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\AssignCompareListToCustomer") @doc(description: "Assign the specified compare list to the logged in customer") deleteCompareList(uid: ID!): DeleteCompareListOutput @resolver(class: "\\Magento\\CompareListGraphQl\\Model\\Resolver\\DeleteCompareList") @doc(description: "Delete the specified compare list") } From a9cffd6382e75dae59a8d7f69d1a45ef01e2646f Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 13 Nov 2020 20:32:44 +0200 Subject: [PATCH 38/45] changes in composer.json --- app/code/Magento/CompareListGraphQl/composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/composer.json b/app/code/Magento/CompareListGraphQl/composer.json index 0c3c76e49f85d..dd9c998857258 100644 --- a/app/code/Magento/CompareListGraphQl/composer.json +++ b/app/code/Magento/CompareListGraphQl/composer.json @@ -1,14 +1,12 @@ { "name": "magento/module-compare-list-graph-ql", - "version": "1.0.0", "description": "N/A", "type": "magento2-module", "require": { "php": "~7.3.0||~7.4.0", "magento/framework": "*", "magento/module-catalog": "*", - "magento/module-customer": "*", - "magento/module-store": "*" + "magento/module-customer": "*" }, "license": [ "OSL-3.0", From ce4fc73b634d94dc2e21ae5a4a2dcac2efde271d Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 16 Nov 2020 20:24:05 +0200 Subject: [PATCH 39/45] Update app/code/Magento/CompareListGraphQl/etc/module.xml Co-authored-by: Andrii Beziazychnyi --- app/code/Magento/CompareListGraphQl/etc/module.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/CompareListGraphQl/etc/module.xml b/app/code/Magento/CompareListGraphQl/etc/module.xml index 050093c2032ec..b3c330fc38df2 100644 --- a/app/code/Magento/CompareListGraphQl/etc/module.xml +++ b/app/code/Magento/CompareListGraphQl/etc/module.xml @@ -12,6 +12,7 @@ + From e90810a0012a4a1ad6bd6de27f5ffa9a46776bc2 Mon Sep 17 00:00:00 2001 From: Prabhu Ram Date: Wed, 18 Nov 2020 12:02:07 -0600 Subject: [PATCH 40/45] compare-products - Fixed errors on creating comparelist and adding products. --- .../CompareListGraphQl/Model/Service/AddToCompareList.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php index 1a0aace67b6c5..6f976bdd07d4d 100644 --- a/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php +++ b/app/code/Magento/CompareListGraphQl/Model/Service/AddToCompareList.php @@ -62,6 +62,10 @@ public function execute(int $listId, array $products, ContextInterface $context) { $storeId = (int)$context->getExtensionAttributes()->getStore()->getStoreId(); $customerId = $context->getUserId(); + if ($customerId) { + $this->itemCollection->setListIdToCustomerCompareItems($listId, $customerId); + } + if (count($products)) { $existedProducts = $this->itemCollection->getProductsByListId($listId); foreach ($products as $productId) { @@ -80,10 +84,6 @@ public function execute(int $listId, array $products, ContextInterface $context) } } - if ($customerId) { - $this->itemCollection->setListIdToCustomerCompareItems($listId, $customerId); - } - return (int)$listId; } From 8aadac8f5cd47d23721b3e86efa3d8603bf6e7a3 Mon Sep 17 00:00:00 2001 From: Deepty Thampy Date: Wed, 18 Nov 2020 21:25:35 -0600 Subject: [PATCH 41/45] MC-37399: Product Compare :: Atwix PR review and delivery - add test coverage --- .../GraphQl/CompareList/CompareListTest.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php index 3f522199974cd..645012030f439 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php @@ -10,6 +10,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; use Magento\TestFramework\TestCase\GraphQlAbstract; /** @@ -83,11 +84,30 @@ public function testAddProductToCompareList() { $compareList = $this->createCompareList(); $uid = $compareList['createCompareList']['uid']; + $this->assertEquals(0, $compareList['createCompareList']['item_count'],'Incorrect count'); $this->uidAssertion($uid); $response = $this->addProductsToCompareList($uid); $resultUid = $response['addProductsToCompareList']['uid']; $this->uidAssertion($resultUid); $this->itemsAssertion($response['addProductsToCompareList']['items']); + $this->assertEquals(2, $response['addProductsToCompareList']['item_count'],'Incorrect count'); + $this->assertResponseFields( + $response['addProductsToCompareList']['attributes'], + [ + [ + 'code'=> 'sku', + 'label'=> 'SKU' + ], + [ + 'code'=> 'description', + 'label'=> 'Description' + ], + [ + 'code'=> 'short_description', + 'label'=> 'Short Description' + ] + ] + ); } /** @@ -246,6 +266,61 @@ public function testAssignCompareListToCustomer() $this->itemsAssertion($customerAssignedResponse['customer']['compare_list']['items']); } + /** + * Assign compare list of one customer to another customer + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + * @magentoApiDataFixture Magento/Customer/_files/two_customers.php + */ + public function testCompareListsNotAccessibleBetweenCustomers() + { + $uidCustomer1 = $this->createCompareListForCustomer('customer@example.com', 'password'); + $uidcustomer2 = $this->createCompareListForCustomer('customer_two@example.com', 'password'); + $assignCompareListToCustomer = <<expectException(ResponseContainsErrorsException::class); + $this->expectExceptionMessage($expectedExceptionsMessage); + //customer2 not allowed to assign compareList belonging to customer1 + $this->graphQlMutation( + $assignCompareListToCustomer, + [], + '', + $this->getCustomerAuthHeaders('customer_two@example.com', 'password') + ); + + $deleteCompareList = <<expectException(ResponseContainsErrorsException::class); + $this->expectExceptionMessage($expectedExceptionsMessage); + //customer1 not allowed to delete compareList belonging to customer2 + $this->graphQlMutation( + $assignCompareListToCustomer, + [], + '', + $this->getCustomerAuthHeaders('customer@example.com', 'password') + ); + + } + /** * Get customer Header * @@ -271,12 +346,33 @@ private function createCompareList(): array mutation{ createCompareList { uid + item_count + attributes{code label} } } MUTATION; return $this->graphQlMutation($mutation); } + private function createCompareListForCustomer(string $username, string $password): string + { + $compareListCustomer = <<graphQlMutation( + $compareListCustomer, + [], + '', + $this->getCustomerAuthHeaders($username, $password) + ); + + return $response['createCompareList']['uid']; + } + /** * Add products to compare list * @@ -292,6 +388,8 @@ private function addProductsToCompareList($uid): array mutation{ addProductsToCompareList(input: { uid: "{$uid}", products: [{$product1->getId()}, {$product2->getId()}]}) { uid + item_count + attributes{code label} items { product { sku From 058a0be3cdd359be2b87e888bfeec04de639d03b Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 23 Nov 2020 20:53:22 +0200 Subject: [PATCH 42/45] Update app/code/Magento/CompareListGraphQl/etc/schema.graphqls Co-authored-by: Kevin Harper --- app/code/Magento/CompareListGraphQl/etc/schema.graphqls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index ebae3ca0ff940..73ccb9ba4bc67 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -21,7 +21,7 @@ type CompareList { uid: ID! @doc(description: "The unique ID assigned to the compare list") items: [ComparableItem] @doc(description: "An array of products to compare") attributes: [ComparableAttribute] @doc(description: "An array of attributes that can be used for comparing products") - item_count: Int! + item_count: Int! @doc(description: "The number of items in the compare list") } type Customer { From 9640f32149764323992bb7caccabf8b4fd59498a Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 23 Nov 2020 20:53:36 +0200 Subject: [PATCH 43/45] Update app/code/Magento/CompareListGraphQl/etc/schema.graphqls Co-authored-by: Kevin Harper --- app/code/Magento/CompareListGraphQl/etc/schema.graphqls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls index 73ccb9ba4bc67..e533d476ddd59 100644 --- a/app/code/Magento/CompareListGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CompareListGraphQl/etc/schema.graphqls @@ -60,5 +60,5 @@ type DeleteCompareListOutput { type AssignCompareListToCustomerOutput { result: Boolean! - compare_list: CompareList + compare_list: CompareList @doc(description: "The contents of the customer's compare list") } From 17ed57e25fb5831a2d2c8b9f2267a1f509ae6548 Mon Sep 17 00:00:00 2001 From: Prabhu Ram Date: Tue, 1 Dec 2020 15:54:59 -0600 Subject: [PATCH 44/45] - Updated constraints on the catalog_compare_list table --- app/code/Magento/Catalog/etc/db_schema.xml | 7 +++---- app/code/Magento/Catalog/etc/db_schema_whitelist.json | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index d601a882cb25e..ce34914a2f5d4 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -586,16 +586,15 @@ - + + + - - -
diff --git a/app/code/Magento/Catalog/etc/db_schema_whitelist.json b/app/code/Magento/Catalog/etc/db_schema_whitelist.json index 3bbd99653e860..efc45112920e2 100644 --- a/app/code/Magento/Catalog/etc/db_schema_whitelist.json +++ b/app/code/Magento/Catalog/etc/db_schema_whitelist.json @@ -1132,12 +1132,12 @@ "customer_id": true }, "index": { - "CATALOG_COMPARE_LIST_LIST_ID_MASK": true, - "CATALOG_COMPARE_LIST_CUSTOMER_ID": true + "CATALOG_COMPARE_LIST_LIST_ID_MASK": true }, "constraint": { "PRIMARY": true, + "CATALOG_COMPARE_LIST_CUSTOMER_ID": true, "CATALOG_COMPARE_LIST_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID": true } } -} \ No newline at end of file +} From afb1581d15c5813708ea673ac30d436a22035175 Mon Sep 17 00:00:00 2001 From: Prabhu Ram Date: Tue, 1 Dec 2020 20:26:16 -0600 Subject: [PATCH 45/45] Removing additional calls to the db. --- app/code/Magento/Catalog/Controller/Product/Compare/Clear.php | 1 - app/code/Magento/Catalog/Controller/Product/Compare/Remove.php | 3 --- 2 files changed, 4 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php b/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php index 81f63f3e75c54..2703e9869bd47 100644 --- a/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php +++ b/app/code/Magento/Catalog/Controller/Product/Compare/Clear.php @@ -31,7 +31,6 @@ public function execute() try { $items->clear(); - $items->removeCompareList($this->_customerSession->getCustomerId()); $this->messageManager->addSuccessMessage(__('You cleared the comparison list.')); $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class)->calculate(); } catch (\Magento\Framework\Exception\LocalizedException $e) { diff --git a/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php b/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php index 23bb7e0e3d459..3f94ffd0909aa 100644 --- a/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php +++ b/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php @@ -50,9 +50,6 @@ public function execute() $helper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class); if ($item->getId()) { $item->delete(); - /** @var Collection $items */ - $items = $this->_itemCollectionFactory->create(); - $items->removeCompareList($this->_customerSession->getCustomerId()); $productName = $this->_objectManager->get(\Magento\Framework\Escaper::class) ->escapeHtml($product->getName()); $this->messageManager->addSuccessMessage(