Skip to content

graphQl-200: Product Compare #23704

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/code/Magento/Catalog/Model/CompareList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model;

use Magento\Framework\Model\AbstractModel;

/**
* @method string getHashedId()
* @method CompareList setHashedId()
* @method int getCustomerId()
* @method CompareList setCustomerId()
*/
class CompareList extends AbstractModel
{
/**
* Initialize resource
*
* @return void
*/
protected function _construct()
{
$this->_init(\Magento\Catalog\Model\ResourceModel\CompareList::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

use Magento\Catalog\Model\CompareListFactory;
use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList;

/**
* @inheritdoc
*/
class CustomerIdByHashedIdProvider implements CustomerIdByHashedIdProviderInterface
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var ResourceCompareList
*/
private $resourceCompareList;

/**
* @param CompareListFactory $compareListFactory
* @param ResourceCompareList $resourceCompareList
*/
public function __construct(
CompareListFactory $compareListFactory,
ResourceCompareList $resourceCompareList
) {
$this->compareListFactory = $compareListFactory;
$this->resourceCompareList = $resourceCompareList;
}

/**
* @inheritdoc
*/
public function get(string $hashedListId): int
{
$compareList = $this->compareListFactory->create();
$this->resourceCompareList->load($compareList, $hashedListId, 'hashed_id');

return (int)$compareList->getCustomerId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

/**
* Get customer id by hashed id.
* @api
*/
interface CustomerIdByHashedIdProviderInterface
{
/**
* @param string $hashedListId
*
* @return int
*/
public function get(string $hashedListId): int;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

use Magento\Catalog\Model\CompareListFactory;
use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList;

/**
* @inheritdoc
*/
class HashedListIdToListId implements HashedListIdToListIdInterface
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var ResourceCompareList
*/
private $resourceCompareList;

/**
* @param CompareListFactory $compareListFactory
* @param ResourceCompareList $resourceCompareList
*/
public function __construct(
CompareListFactory $compareListFactory,
ResourceCompareList $resourceCompareList
) {
$this->compareListFactory = $compareListFactory;
$this->resourceCompareList = $resourceCompareList;
}

/**
* @inheritdoc
*/
public function execute(string $hashedListId): int
{
$compareList = $this->compareListFactory->create();
$this->resourceCompareList->load($compareList, $hashedListId, 'hashed_id');

return (int)$compareList->getListId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

/**
* Converts hashed list id to the list id.
* @api
*/
interface HashedListIdToListIdInterface
{
/**
* @param string $hashedListId
*
* @return int
*/
public function execute(string $hashedListId): int;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

use Magento\Catalog\Model\CompareListFactory;
use Magento\Catalog\Model\ResourceModel\CompareList as ResourceCompareList;

/**
* @inheritdoc
*/
class ListIdToHashedListId implements ListIdToHashedListIdInterface
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var ResourceCompareList
*/
private $resourceCompareList;

/**
* @param CompareListFactory $compareListFactory
* @param ResourceCompareList $resourceCompareList
*/
public function __construct(
CompareListFactory $compareListFactory,
ResourceCompareList $resourceCompareList
) {
$this->compareListFactory = $compareListFactory;
$this->resourceCompareList = $resourceCompareList;
}

/**
* @inheritdoc
*/
public function execute(int $listId): string
{
$compareList = $this->compareListFactory->create();
$this->resourceCompareList->load($compareList, $listId, 'list_id');

return $compareList->getListId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\CompareList;

/**
* Converts list id to the list hashed id.
* @api
*/
interface ListIdToHashedListIdInterface
{
/**
* @param int $listId
*
* @return string
*/
public function execute(int $listId): string;
}
67 changes: 67 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Compare/AddToList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model\Product\Compare;

use Magento\Catalog\Model\CompareList\CustomerIdByHashedIdProviderInterface;
use Magento\Catalog\Model\CompareList\HashedListIdToListIdInterface;

class AddToList
{
/**
* @var ItemFactory
*/
private $compareItemFactory;

/**
* @var HashedListIdToListIdInterface
*/
private $hashedListIdToListId;

/**
* @var CustomerIdByHashedIdProviderInterface
*/
private $customerIdByHashedIdProvider;

/**
* @param ItemFactory $compareItemFactory
* @param HashedListIdToListIdInterface $hashedListIdToListId
* @param CustomerIdByHashedIdProviderInterface $customerIdByHashedIdProvider
*/
public function __construct(
ItemFactory $compareItemFactory,
HashedListIdToListIdInterface $hashedListIdToListId,
CustomerIdByHashedIdProviderInterface $customerIdByHashedIdProvider
) {
$this->compareItemFactory = $compareItemFactory;
$this->hashedListIdToListId = $hashedListIdToListId;
$this->customerIdByHashedIdProvider = $customerIdByHashedIdProvider;
}

/**
* @param int $customerId
* @param string $hashedId
* @param int $productId
*/
public function execute(int $customerId, string $hashedId, int $productId)
{
$item = $this->compareItemFactory->create();
if (0 !== $customerId) {
$item->setCustomerId($customerId);
}

$listId = $this->hashedListIdToListId->execute($hashedId);
$listCustomerId = $this->customerIdByHashedIdProvider->get($hashedId);

$item->setCatalogCompareListId($listId);
$item->loadByProduct($productId);

if (!$item->getId() && $customerId === $listCustomerId) {
$item->addProductData($productId);
$item->save();
}
}
}
56 changes: 56 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Compare/CreateList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model\Product\Compare;

use Magento\Catalog\Model\CompareListFactory;
use Magento\Framework\Math\Random;

/**
* Create new compare list.
*/
class CreateList
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var Random
*/
private $randomDataGenerator;

/**
* @param CompareListFactory $compareListFactory
* @param Random $randomDataGenerator
*/
public function __construct(
CompareListFactory $compareListFactory,
Random $randomDataGenerator
) {
$this->compareListFactory = $compareListFactory;
$this->randomDataGenerator = $randomDataGenerator;
}

/**
* If customerId === 0 list will be created for guest.
*
* @param int $customerId
*/
public function execute(int $customerId)
{
$compareList = $this->compareListFactory->create();

if (0 !== $customerId) {
$compareList->setCustomerId($customerId);
}

$compareList->setHashedId($this->randomDataGenerator->getUniqueHash())->save();

return $compareList;
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Compare/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* @api
* @method \Magento\Catalog\Model\Product\Compare\Item setVisitorId(int $value)
* @method int getCatalogCompareListId()
* @method \Magento\Catalog\Model\Product\Compare\Item setCatalogCompareListId(int $value)
* @method \Magento\Catalog\Model\Product\Compare\Item setCustomerId(int $value)
* @method int getProductId()
* @method \Magento\Catalog\Model\Product\Compare\Item setProductId(int $value)
Expand Down
Loading