Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CompareListGraphQl\Model\Resolver;

use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
use Magento\Catalog\Helper\Product\Compare;
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\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -21,6 +24,8 @@

/**
* Add products item to compare list
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddProductsToCompareList implements ResolverInterface
{
Expand All @@ -44,22 +49,40 @@ class AddProductsToCompareList implements ResolverInterface
*/
private $getListIdByCustomerId;

/**
* @var Compare
*/
private mixed $productCompareHelper;

/**
* @var CompareCookieManager
*/
private CompareCookieManager $compareCookieManager;

/**
* @param AddToCompareList $addProductToCompareList
* @param GetCompareList $getCompareList
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
* @param GetListIdByCustomerId $getListIdByCustomerId
* @param Compare|null $productCompareHelper
* @param CompareCookieManager|null $compareCookieManager
*/
public function __construct(
AddToCompareList $addProductToCompareList,
GetCompareList $getCompareList,
MaskedListIdToCompareListId $maskedListIdToCompareListId,
GetListIdByCustomerId $getListIdByCustomerId
GetListIdByCustomerId $getListIdByCustomerId,
?Compare $productCompareHelper = null,
?CompareCookieManager $compareCookieManager = null
) {
$this->addProductToCompareList = $addProductToCompareList;
$this->getCompareList = $getCompareList;
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
$this->getListIdByCustomerId = $getListIdByCustomerId;
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
->get(Compare::class);
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
->get(CompareCookieManager::class);
}

/**
Expand Down Expand Up @@ -97,13 +120,14 @@ public function resolve(
throw new GraphQlInputException(__($exception->getMessage()));
}


if (!$listId) {
throw new GraphQlInputException(__('"uid" value does not exist'));
}

try {
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
$this->productCompareHelper->calculate();
$this->compareCookieManager->invalidate();
} catch (\Exception $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CompareListGraphQl\Model\Resolver;

use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
use Magento\Catalog\Helper\Product\Compare;
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService;
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -22,6 +25,8 @@

/**
* Class for creating compare list
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreateCompareList implements ResolverInterface
{
Expand Down Expand Up @@ -50,25 +55,43 @@ class CreateCompareList implements ResolverInterface
*/
private $createCompareList;

/**
* @var Compare
*/
private mixed $productCompareHelper;

/**
* @var CompareCookieManager
*/
private CompareCookieManager $compareCookieManager;

/**
* @param Random $mathRandom
* @param GetListIdByCustomerId $getListIdByCustomerId
* @param AddToCompareList $addProductToCompareList
* @param GetCompareList $getCompareList
* @param CreateCompareListService $createCompareList
* @param Compare|null $productCompareHelper
* @param CompareCookieManager|null $compareCookieManager
*/
public function __construct(
Random $mathRandom,
GetListIdByCustomerId $getListIdByCustomerId,
AddToCompareList $addProductToCompareList,
GetCompareList $getCompareList,
CreateCompareListService $createCompareList
CreateCompareListService $createCompareList,
?Compare $productCompareHelper = null,
?CompareCookieManager $compareCookieManager = null
) {
$this->mathRandom = $mathRandom;
$this->getListIdByCustomerId = $getListIdByCustomerId;
$this->addProductToCompareList = $addProductToCompareList;
$this->getCompareList = $getCompareList;
$this->createCompareList = $createCompareList;
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
->get(Compare::class);
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
->get(CompareCookieManager::class);
}

/**
Expand Down Expand Up @@ -111,6 +134,8 @@ public function resolve(
} else {
$listId = $this->createCompareList->execute($generatedListId, $customerId);
$this->addProductToCompareList->execute($listId, $products, $context);
$this->productCompareHelper->calculate();
$this->compareCookieManager->invalidate();
}
}
} catch (LocalizedException $exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2020 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CompareListGraphQl\Model\Resolver;

use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
use Magento\Catalog\Helper\Product\Compare;
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\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -21,6 +24,8 @@

/**
* Remove items from compare list
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class RemoveProductsFromCompareList implements ResolverInterface
{
Expand All @@ -44,22 +49,40 @@ class RemoveProductsFromCompareList implements ResolverInterface
*/
private $getListIdByCustomerId;

/**
* @var Compare
*/
private mixed $productCompareHelper;

/**
* @var CompareCookieManager
*/
private CompareCookieManager $compareCookieManager;

/**
* @param GetCompareList $getCompareList
* @param RemoveFromCompareList $removeFromCompareList
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
* @param GetListIdByCustomerId $getListIdByCustomerId
* @param Compare|null $productCompareHelper
* @param CompareCookieManager|null $compareCookieManager
*/
public function __construct(
GetCompareList $getCompareList,
RemoveFromCompareList $removeFromCompareList,
MaskedListIdToCompareListId $maskedListIdToCompareListId,
GetListIdByCustomerId $getListIdByCustomerId
GetListIdByCustomerId $getListIdByCustomerId,
?Compare $productCompareHelper = null,
?CompareCookieManager $compareCookieManager = null
) {
$this->getCompareList = $getCompareList;
$this->removeFromCompareList = $removeFromCompareList;
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
$this->getListIdByCustomerId = $getListIdByCustomerId;
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
->get(Compare::class);
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
->get(CompareCookieManager::class);
}

/**
Expand Down Expand Up @@ -111,6 +134,8 @@ public function resolve(

try {
$this->removeFromCompareList->execute($listId, $args['input']['products']);
$this->productCompareHelper->calculate();
$this->compareCookieManager->invalidate();
} catch (LocalizedException $exception) {
throw new GraphQlInputException(
__('Something was wrong during removing products from compare list')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CompareListGraphQl\Model\Service;

use Exception;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Stdlib\Cookie\FailureToSendException;
use Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException;
use Psr\Log\LoggerInterface;

/**
* Service for managing compare list cookies
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CompareCookieManager
{
/**
* Name of cookie that holds compare products section data
*/
public const COOKIE_COMPARE_PRODUCTS = 'section_data_ids';

/**
* The path for which the cookie will be available
*/
public const COOKIE_PATH = '/';

/**
* Cookie lifetime value in seconds (86400 = 24 hours)
*/
public const COOKIE_LIFETIME = 86400;

/**
* @param CookieManagerInterface $cookieManager
* @param CookieMetadataFactory $cookieMetadataFactory
* @param LoggerInterface $logger
*/
public function __construct(
private readonly CookieManagerInterface $cookieManager,
private readonly CookieMetadataFactory $cookieMetadataFactory,
private readonly LoggerInterface $logger
) {
}

/**
* Marks compare products section as invalid by updating the cookie value
*
* @return void
*/
public function invalidate(): void
{
try {
$cookieValue = json_encode(['compare-products' => time()]);
$this->setCookie($cookieValue);
} catch (Exception $e) {
$this->logger->error('Error invalidating compare products cookie: ' . $e->getMessage());
}
}

/**
* Set compare products cookie
*
* @param string $value
* @return void
* @throws InputException
* @throws CookieSizeLimitReachedException
* @throws FailureToSendException
*/
private function setCookie(string $value): void
{
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
->setDuration(self::COOKIE_LIFETIME)
->setPath(self::COOKIE_PATH)
->setHttpOnly(false);

$this->cookieManager->setPublicCookie(
self::COOKIE_COMPARE_PRODUCTS,
$value,
$publicCookieMetadata
);
}
}
Loading