AddressRepository.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Customer address entity resource model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\ResourceModel;
  9. use Magento\Customer\Api\Data\AddressInterface;
  10. use Magento\Customer\Model\Address as CustomerAddressModel;
  11. use Magento\Customer\Model\Customer as CustomerModel;
  12. use Magento\Customer\Model\ResourceModel\Address\Collection;
  13. use Magento\Framework\Api\Search\FilterGroup;
  14. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  15. use Magento\Framework\Api\SearchCriteriaInterface;
  16. use Magento\Framework\Exception\InputException;
  17. /**
  18. * Address repository.
  19. *
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class AddressRepository implements \Magento\Customer\Api\AddressRepositoryInterface
  23. {
  24. /**
  25. * Directory data
  26. *
  27. * @var \Magento\Directory\Helper\Data
  28. */
  29. protected $directoryData;
  30. /**
  31. * @var \Magento\Customer\Model\AddressFactory
  32. */
  33. protected $addressFactory;
  34. /**
  35. * @var \Magento\Customer\Model\AddressRegistry
  36. */
  37. protected $addressRegistry;
  38. /**
  39. * @var \Magento\Customer\Model\CustomerRegistry
  40. */
  41. protected $customerRegistry;
  42. /**
  43. * @var \Magento\Customer\Model\ResourceModel\Address
  44. */
  45. protected $addressResourceModel;
  46. /**
  47. * @var \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory
  48. */
  49. protected $addressSearchResultsFactory;
  50. /**
  51. * @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory
  52. */
  53. protected $addressCollectionFactory;
  54. /**
  55. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  56. */
  57. protected $extensionAttributesJoinProcessor;
  58. /**
  59. * @var CollectionProcessorInterface
  60. */
  61. private $collectionProcessor;
  62. /**
  63. * @param \Magento\Customer\Model\AddressFactory $addressFactory
  64. * @param \Magento\Customer\Model\AddressRegistry $addressRegistry
  65. * @param \Magento\Customer\Model\CustomerRegistry $customerRegistry
  66. * @param \Magento\Customer\Model\ResourceModel\Address $addressResourceModel
  67. * @param \Magento\Directory\Helper\Data $directoryData
  68. * @param \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory
  69. * @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressCollectionFactory
  70. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
  71. * @param CollectionProcessorInterface $collectionProcessor
  72. */
  73. public function __construct(
  74. \Magento\Customer\Model\AddressFactory $addressFactory,
  75. \Magento\Customer\Model\AddressRegistry $addressRegistry,
  76. \Magento\Customer\Model\CustomerRegistry $customerRegistry,
  77. \Magento\Customer\Model\ResourceModel\Address $addressResourceModel,
  78. \Magento\Directory\Helper\Data $directoryData,
  79. \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory,
  80. \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressCollectionFactory,
  81. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
  82. CollectionProcessorInterface $collectionProcessor = null
  83. ) {
  84. $this->addressFactory = $addressFactory;
  85. $this->addressRegistry = $addressRegistry;
  86. $this->customerRegistry = $customerRegistry;
  87. $this->addressResourceModel = $addressResourceModel;
  88. $this->directoryData = $directoryData;
  89. $this->addressSearchResultsFactory = $addressSearchResultsFactory;
  90. $this->addressCollectionFactory = $addressCollectionFactory;
  91. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  92. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  93. }
  94. /**
  95. * Save customer address.
  96. *
  97. * @param \Magento\Customer\Api\Data\AddressInterface $address
  98. * @return \Magento\Customer\Api\Data\AddressInterface
  99. * @throws \Magento\Framework\Exception\LocalizedException
  100. */
  101. public function save(\Magento\Customer\Api\Data\AddressInterface $address)
  102. {
  103. $addressModel = null;
  104. $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
  105. if ($address->getId()) {
  106. $addressModel = $this->addressRegistry->retrieve($address->getId());
  107. }
  108. if ($addressModel === null) {
  109. /** @var \Magento\Customer\Model\Address $addressModel */
  110. $addressModel = $this->addressFactory->create();
  111. $addressModel->updateData($address);
  112. $addressModel->setCustomer($customerModel);
  113. } else {
  114. $addressModel->updateData($address);
  115. }
  116. $addressModel->setStoreId($customerModel->getStoreId());
  117. $errors = $addressModel->validate();
  118. if ($errors !== true) {
  119. $inputException = new InputException();
  120. foreach ($errors as $error) {
  121. $inputException->addError($error);
  122. }
  123. throw $inputException;
  124. }
  125. $addressModel->save();
  126. $address->setId($addressModel->getId());
  127. // Clean up the customer registry since the Address save has a
  128. // side effect on customer : \Magento\Customer\Model\ResourceModel\Address::_afterSave
  129. $this->addressRegistry->push($addressModel);
  130. $this->updateAddressCollection($customerModel, $addressModel);
  131. return $addressModel->getDataModel();
  132. }
  133. /**
  134. * Update address collection.
  135. *
  136. * @param Customer $customer
  137. * @param Address $address
  138. * @throws \Magento\Framework\Exception\LocalizedException
  139. * @return void
  140. */
  141. private function updateAddressCollection(CustomerModel $customer, CustomerAddressModel $address)
  142. {
  143. $customer->getAddressesCollection()->removeItemByKey($address->getId());
  144. $customer->getAddressesCollection()->addItem($address);
  145. }
  146. /**
  147. * Retrieve customer address.
  148. *
  149. * @param int $addressId
  150. * @return \Magento\Customer\Api\Data\AddressInterface
  151. * @throws \Magento\Framework\Exception\LocalizedException
  152. */
  153. public function getById($addressId)
  154. {
  155. $address = $this->addressRegistry->retrieve($addressId);
  156. return $address->getDataModel();
  157. }
  158. /**
  159. * Retrieve customers addresses matching the specified criteria.
  160. *
  161. * @param SearchCriteriaInterface $searchCriteria
  162. * @return \Magento\Customer\Api\Data\AddressSearchResultsInterface
  163. * @throws \Magento\Framework\Exception\LocalizedException
  164. */
  165. public function getList(SearchCriteriaInterface $searchCriteria)
  166. {
  167. /** @var Collection $collection */
  168. $collection = $this->addressCollectionFactory->create();
  169. $this->extensionAttributesJoinProcessor->process(
  170. $collection,
  171. \Magento\Customer\Api\Data\AddressInterface::class
  172. );
  173. $this->collectionProcessor->process($searchCriteria, $collection);
  174. /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
  175. $addresses = [];
  176. /** @var \Magento\Customer\Model\Address $address */
  177. foreach ($collection->getItems() as $address) {
  178. $addresses[] = $this->getById($address->getId());
  179. }
  180. /** @var \Magento\Customer\Api\Data\AddressSearchResultsInterface $searchResults */
  181. $searchResults = $this->addressSearchResultsFactory->create();
  182. $searchResults->setItems($addresses);
  183. $searchResults->setSearchCriteria($searchCriteria);
  184. $searchResults->setTotalCount($collection->getSize());
  185. return $searchResults;
  186. }
  187. /**
  188. * Helper function that adds a FilterGroup to the collection.
  189. *
  190. * @deprecated 101.0.0
  191. * @param FilterGroup $filterGroup
  192. * @param Collection $collection
  193. * @return void
  194. * @throws \Magento\Framework\Exception\InputException
  195. */
  196. protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
  197. {
  198. $fields = [];
  199. $conditions = [];
  200. foreach ($filterGroup->getFilters() as $filter) {
  201. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  202. $fields[] = ['attribute' => $filter->getField(), $condition => $filter->getValue()];
  203. $conditions[] = [$condition => $filter->getValue()];
  204. }
  205. if ($fields) {
  206. $collection->addFieldToFilter($fields, $conditions);
  207. }
  208. }
  209. /**
  210. * Delete customer address.
  211. *
  212. * @param \Magento\Customer\Api\Data\AddressInterface $address
  213. * @return bool true on success
  214. * @throws \Magento\Framework\Exception\LocalizedException
  215. */
  216. public function delete(\Magento\Customer\Api\Data\AddressInterface $address)
  217. {
  218. $addressId = $address->getId();
  219. $address = $this->addressRegistry->retrieve($addressId);
  220. $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
  221. $customerModel->getAddressesCollection()->clear();
  222. $this->addressResourceModel->delete($address);
  223. $this->addressRegistry->remove($addressId);
  224. return true;
  225. }
  226. /**
  227. * Delete customer address by ID.
  228. *
  229. * @param int $addressId
  230. * @return bool true on success
  231. * @throws \Magento\Framework\Exception\NoSuchEntityException
  232. * @throws \Magento\Framework\Exception\LocalizedException
  233. */
  234. public function deleteById($addressId)
  235. {
  236. $address = $this->addressRegistry->retrieve($addressId);
  237. $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
  238. $customerModel->getAddressesCollection()->removeItemByKey($addressId);
  239. $this->addressResourceModel->delete($address);
  240. $this->addressRegistry->remove($addressId);
  241. return true;
  242. }
  243. /**
  244. * Retrieve collection processor
  245. *
  246. * @deprecated 101.0.0
  247. * @return CollectionProcessorInterface
  248. */
  249. private function getCollectionProcessor()
  250. {
  251. if (!$this->collectionProcessor) {
  252. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  253. 'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
  254. );
  255. }
  256. return $this->collectionProcessor;
  257. }
  258. }