Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

[Cart Operations] Remove item mutation #373

Merged
merged 5 commits into from
Mar 1, 2019
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
Expand Up @@ -11,9 +11,11 @@
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
Expand Down Expand Up @@ -58,6 +60,7 @@ public function __construct(
* @param int|null $customerType
* @return void
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthenticationException
*/
Expand All @@ -74,13 +77,20 @@ public function execute(?int $customerId, ?int $customerType): void
__('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.'));
}

$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
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."));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\BillingAddressManagementInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Set billing address for a specified shopping cart
*/
class AssignBillingAddressToCart
{
/**
* @var BillingAddressManagementInterface
*/
private $billingAddressManagement;

/**
* @param BillingAddressManagementInterface $billingAddressManagement
*/
public function __construct(
BillingAddressManagementInterface $billingAddressManagement
) {
$this->billingAddressManagement = $billingAddressManagement;
}

/**
* Assign billing address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $billingAddress
* @param bool $useForShipping
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $billingAddress,
bool $useForShipping
): void {
try {
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\ShippingAddressManagementInterface;

/**
* Assign shipping address to cart
*/
class AssignShippingAddressToCart
{
/**
* @var ShippingAddressManagementInterface
*/
private $shippingAddressManagement;

/**
* @param ShippingAddressManagementInterface $shippingAddressManagement
*/
public function __construct(
ShippingAddressManagementInterface $shippingAddressManagement
) {
$this->shippingAddressManagement = $shippingAddressManagement;
}

/**
* Assign shipping address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $shippingAddress
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $shippingAddress
): void {
try {
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
}
}
68 changes: 0 additions & 68 deletions app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromCart.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
Expand All @@ -33,14 +34,14 @@ public function __construct(AddressRepositoryInterface $addressRepository)
}

/**
* Get customer address. Throws exception if customer is not owner of address
* Get customer address
*
* @param int $addressId
* @param int $customerId
* @return AddressInterface
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws LocalizedException
* @throws GraphQlAuthorizationException
*/
public function execute(int $addressId, int $customerId): AddressInterface
{
Expand All @@ -50,6 +51,8 @@ public function execute(int $addressId, int $customerId): AddressInterface
throw new GraphQlNoSuchEntityException(
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

if ((int)$customerAddress->getCustomerId() !== $customerId) {
Expand Down
67 changes: 67 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.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.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;

/**
* Create QuoteAddress
*/
class QuoteAddressFactory
{
/**
* @var BaseQuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory
) {
$this->quoteAddressFactory = $quoteAddressFactory;
}

/**
* Create QuoteAddress based on input data
*
* @param array $addressInput
* @return QuoteAddress
*/
public function createBasedOnInputData(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
}

/**
* Create QuoteAddress based on CustomerAddress
*
* @param CustomerAddress $customerAddress
* @return QuoteAddress
* @throws GraphQlInputException
*/
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
{
$quoteAddress = $this->quoteAddressFactory->create();
try {
$quoteAddress->importCustomerAddressData($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
return $quoteAddress;
}
}
Loading