-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from magento-lynx/2.4-gl-graphql-v1
LYNX-258 & LYNX-259
- Loading branch information
Showing
7 changed files
with
591 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Quote\Test\Fixture; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Exception\InvalidArgumentException; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
use Magento\Quote\Model\QuoteFactory; | ||
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; | ||
use Magento\TestFramework\Fixture\DataFixtureInterface; | ||
|
||
/** | ||
* Mark cart as inactive | ||
*/ | ||
class MakeCartInactive implements DataFixtureInterface | ||
{ | ||
private const FIELD_CART_ID = 'cart_id'; | ||
|
||
/** | ||
* @var CartRepositoryInterface | ||
*/ | ||
private CartRepositoryInterface $cartRepository; | ||
|
||
/** | ||
* @var QuoteFactory | ||
*/ | ||
private QuoteFactory $quoteFactory; | ||
|
||
/** | ||
* @var QuoteResource | ||
*/ | ||
private QuoteResource $quoteResource; | ||
|
||
/** | ||
* @param CartRepositoryInterface $cartRepository | ||
* @param QuoteFactory $quoteFactory | ||
* @param QuoteResource $quoteResource | ||
*/ | ||
public function __construct( | ||
CartRepositoryInterface $cartRepository, | ||
QuoteFactory $quoteFactory, | ||
QuoteResource $quoteResource | ||
) { | ||
$this->cartRepository = $cartRepository; | ||
$this->quoteFactory = $quoteFactory; | ||
$this->quoteResource = $quoteResource; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return void | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function apply(array $data = []): ?DataObject | ||
{ | ||
if (empty($data[self::FIELD_CART_ID])) { | ||
throw new InvalidArgumentException(__('"%field" is required', ['field' => self::FIELD_CART_ID])); | ||
} | ||
|
||
$quote = $this->quoteFactory->create(); | ||
$this->quoteResource->load($quote, $data[self::FIELD_CART_ID]); | ||
$quote->setIsActive(false); | ||
$this->cartRepository->save($quote); | ||
|
||
return $quote; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
app/code/Magento/QuoteGraphQl/Model/Cart/ValidateMaskedQuoteId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Cart; | ||
|
||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; | ||
|
||
/** | ||
* Validates a pre-defined masked quote id | ||
*/ | ||
class ValidateMaskedQuoteId | ||
{ | ||
/** | ||
* @var MaskedQuoteIdToQuoteIdInterface | ||
*/ | ||
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId; | ||
|
||
/** | ||
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId | ||
*/ | ||
public function __construct( | ||
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, | ||
) { | ||
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; | ||
} | ||
|
||
/** | ||
* Validate masked id | ||
* | ||
* @param string $maskedId | ||
* @throws GraphQlAlreadyExistsException | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(string $maskedId): void | ||
{ | ||
if (mb_strlen($maskedId) != 32) { | ||
throw new GraphQlInputException(__('Cart ID length should to be 32 symbols.')); | ||
} | ||
|
||
if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) { | ||
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId)); | ||
} | ||
} | ||
|
||
/** | ||
* Check is quote with such maskedId already exists | ||
* | ||
* @param string $maskedId | ||
* @return bool | ||
*/ | ||
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool | ||
{ | ||
try { | ||
$this->maskedQuoteIdToQuoteId->execute($maskedId); | ||
return true; | ||
} catch (NoSuchEntityException $e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/code/Magento/QuoteGraphQl/Model/Resolver/CreateGuestCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2023 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; | ||
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest; | ||
use Magento\QuoteGraphQl\Model\Cart\ValidateMaskedQuoteId; | ||
|
||
/** | ||
* Creates a guest cart | ||
*/ | ||
class CreateGuestCart implements ResolverInterface | ||
{ | ||
/** | ||
* @var CreateEmptyCartForGuest | ||
*/ | ||
private CreateEmptyCartForGuest $createEmptyCartForGuest; | ||
|
||
/** | ||
* @var MaskedQuoteIdToQuoteIdInterface | ||
*/ | ||
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId; | ||
|
||
/** | ||
* @var CartRepositoryInterface | ||
*/ | ||
private CartRepositoryInterface $cartRepository; | ||
|
||
/** | ||
* @var ValidateMaskedQuoteId | ||
*/ | ||
private ValidateMaskedQuoteId $validateMaskedQuoteId; | ||
|
||
/** | ||
* @param CreateEmptyCartForGuest $createEmptyCartForGuest | ||
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId | ||
* @param CartRepositoryInterface $cartRepository | ||
* @param ValidateMaskedQuoteId $validateMaskedQuoteId | ||
*/ | ||
public function __construct( | ||
CreateEmptyCartForGuest $createEmptyCartForGuest, | ||
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, | ||
CartRepositoryInterface $cartRepository, | ||
ValidateMaskedQuoteId $validateMaskedQuoteId | ||
) { | ||
$this->createEmptyCartForGuest = $createEmptyCartForGuest; | ||
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; | ||
$this->cartRepository = $cartRepository; | ||
$this->validateMaskedQuoteId = $validateMaskedQuoteId; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$customerId = $context->getUserId(); | ||
|
||
$predefinedMaskedQuoteId = null; | ||
if (isset($args['input']['cart_uid'])) { | ||
$predefinedMaskedQuoteId = $args['input']['cart_uid']; | ||
$this->validateMaskedQuoteId->execute($predefinedMaskedQuoteId); | ||
} | ||
|
||
if ($customerId === 0 || $customerId === null) { | ||
$maskedQuoteId = $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId); | ||
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedQuoteId); | ||
$cart = $this->cartRepository->get($cartId); | ||
} else { | ||
throw new GraphQlAlreadyExistsException( | ||
__('Use `Query.customerCart` for logged in customer.') | ||
); | ||
} | ||
|
||
return [ | ||
'cart' => [ | ||
'model' => $cart, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.