Save.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Address;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. use Magento\Framework\Controller\Result\Json;
  11. use Magento\Framework\Controller\Result\JsonFactory;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Exception\NoSuchEntityException;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Class for saving of customer address
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class Save extends Action implements HttpPostActionInterface
  20. {
  21. /**
  22. * Authorization level of a basic admin session
  23. *
  24. * @see _isAllowed()
  25. */
  26. public const ADMIN_RESOURCE = 'Magento_Customer::manage';
  27. /**
  28. * @var \Magento\Customer\Api\AddressRepositoryInterface
  29. */
  30. private $addressRepository;
  31. /**
  32. * @var \Magento\Customer\Model\Metadata\FormFactory
  33. */
  34. private $formFactory;
  35. /**
  36. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  37. */
  38. private $customerRepository;
  39. /**
  40. * @var \Magento\Framework\Api\DataObjectHelper
  41. */
  42. private $dataObjectHelper;
  43. /**
  44. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory
  45. */
  46. private $addressDataFactory;
  47. /**
  48. * @var LoggerInterface
  49. */
  50. private $logger;
  51. /**
  52. * @var JsonFactory
  53. */
  54. private $resultJsonFactory;
  55. /**
  56. * @param Action\Context $context
  57. * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
  58. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  59. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  60. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  61. * @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
  62. * @param LoggerInterface $logger
  63. * @param JsonFactory $resultJsonFactory
  64. */
  65. public function __construct(
  66. Action\Context $context,
  67. \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
  68. \Magento\Customer\Model\Metadata\FormFactory $formFactory,
  69. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  70. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  71. \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory,
  72. LoggerInterface $logger,
  73. JsonFactory $resultJsonFactory
  74. ) {
  75. parent::__construct($context);
  76. $this->addressRepository = $addressRepository;
  77. $this->formFactory = $formFactory;
  78. $this->customerRepository = $customerRepository;
  79. $this->dataObjectHelper = $dataObjectHelper;
  80. $this->addressDataFactory = $addressDataFactory;
  81. $this->logger = $logger;
  82. $this->resultJsonFactory = $resultJsonFactory;
  83. }
  84. /**
  85. * Save customer address action
  86. *
  87. * @return Json
  88. */
  89. public function execute(): Json
  90. {
  91. $customerId = $this->getRequest()->getParam('parent_id', false);
  92. $addressId = $this->getRequest()->getParam('entity_id', false);
  93. $error = false;
  94. try {
  95. /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
  96. $customer = $this->customerRepository->getById($customerId);
  97. $addressForm = $this->formFactory->create(
  98. 'customer_address',
  99. 'adminhtml_customer_address',
  100. [],
  101. false,
  102. false
  103. );
  104. $addressData = $addressForm->extractData($this->getRequest());
  105. $addressData = $addressForm->compactData($addressData);
  106. $addressData['region'] = [
  107. 'region' => $addressData['region'] ?? null,
  108. 'region_id' => $addressData['region_id'] ?? null,
  109. ];
  110. $addressToSave = $this->addressDataFactory->create();
  111. $this->dataObjectHelper->populateWithArray(
  112. $addressToSave,
  113. $addressData,
  114. \Magento\Customer\Api\Data\AddressInterface::class
  115. );
  116. $addressToSave->setCustomerId($customer->getId());
  117. $addressToSave->setIsDefaultBilling(
  118. (bool)$this->getRequest()->getParam('default_billing', false)
  119. );
  120. $addressToSave->setIsDefaultShipping(
  121. (bool)$this->getRequest()->getParam('default_shipping', false)
  122. );
  123. if ($addressId) {
  124. $addressToSave->setId($addressId);
  125. $message = __('Customer address has been updated.');
  126. } else {
  127. $addressToSave->setId(null);
  128. $message = __('New customer address has been added.');
  129. }
  130. $savedAddress = $this->addressRepository->save($addressToSave);
  131. $addressId = $savedAddress->getId();
  132. } catch (NoSuchEntityException $e) {
  133. $this->logger->critical($e);
  134. $error = true;
  135. $message = __('There is no customer with such id.');
  136. } catch (LocalizedException $e) {
  137. $error = true;
  138. $message = __($e->getMessage());
  139. $this->logger->critical($e);
  140. } catch (\Exception $e) {
  141. $error = true;
  142. $message = __('We can\'t change customer address right now.');
  143. $this->logger->critical($e);
  144. }
  145. $addressId = empty($addressId) ? null : $addressId;
  146. $resultJson = $this->resultJsonFactory->create();
  147. $resultJson->setData(
  148. [
  149. 'message' => $message,
  150. 'error' => $error,
  151. 'data' => [
  152. 'entity_id' => $addressId
  153. ]
  154. ]
  155. );
  156. return $resultJson;
  157. }
  158. }