BillingAddressManagement.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Framework\Exception\InputException;
  8. use Magento\Quote\Model\Quote\Address\BillingAddressPersister;
  9. use Psr\Log\LoggerInterface as Logger;
  10. use Magento\Quote\Api\BillingAddressManagementInterface;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Quote billing address write service object.
  14. */
  15. class BillingAddressManagement implements BillingAddressManagementInterface
  16. {
  17. /**
  18. * Validator.
  19. *
  20. * @var QuoteAddressValidator
  21. */
  22. protected $addressValidator;
  23. /**
  24. * Logger.
  25. *
  26. * @var Logger
  27. */
  28. protected $logger;
  29. /**
  30. * Quote repository.
  31. *
  32. * @var \Magento\Quote\Api\CartRepositoryInterface
  33. */
  34. protected $quoteRepository;
  35. /**
  36. * @var \Magento\Customer\Api\AddressRepositoryInterface
  37. */
  38. protected $addressRepository;
  39. /**
  40. * @var \Magento\Quote\Model\ShippingAddressAssignment
  41. */
  42. private $shippingAddressAssignment;
  43. /**
  44. * Constructs a quote billing address service object.
  45. *
  46. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
  47. * @param QuoteAddressValidator $addressValidator Address validator.
  48. * @param Logger $logger Logger.
  49. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  50. */
  51. public function __construct(
  52. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  53. QuoteAddressValidator $addressValidator,
  54. Logger $logger,
  55. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  56. ) {
  57. $this->addressValidator = $addressValidator;
  58. $this->logger = $logger;
  59. $this->quoteRepository = $quoteRepository;
  60. $this->addressRepository = $addressRepository;
  61. }
  62. /**
  63. * @inheritdoc
  64. * @SuppressWarnings(PHPMD.NPathComplexity)
  65. */
  66. public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
  67. {
  68. /** @var \Magento\Quote\Model\Quote $quote */
  69. $quote = $this->quoteRepository->getActive($cartId);
  70. $address->setCustomerId($quote->getCustomerId());
  71. $quote->removeAddress($quote->getBillingAddress()->getId());
  72. $quote->setBillingAddress($address);
  73. try {
  74. $this->getShippingAddressAssignment()->setAddress($quote, $address, $useForShipping);
  75. $quote->setDataChanges(true);
  76. $this->quoteRepository->save($quote);
  77. } catch (\Exception $e) {
  78. $this->logger->critical($e);
  79. throw new InputException(__('The address failed to save. Verify the address and try again.'));
  80. }
  81. return $quote->getBillingAddress()->getId();
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function get($cartId)
  87. {
  88. $cart = $this->quoteRepository->getActive($cartId);
  89. return $cart->getBillingAddress();
  90. }
  91. /**
  92. * Get shipping address assignment
  93. *
  94. * @return \Magento\Quote\Model\ShippingAddressAssignment
  95. * @deprecated 101.0.0
  96. */
  97. private function getShippingAddressAssignment()
  98. {
  99. if (!$this->shippingAddressAssignment) {
  100. $this->shippingAddressAssignment = ObjectManager::getInstance()
  101. ->get(\Magento\Quote\Model\ShippingAddressAssignment::class);
  102. }
  103. return $this->shippingAddressAssignment;
  104. }
  105. }