Address.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\Data\AddressInterface;
  9. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  10. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  11. use Magento\Framework\Indexer\StateInterface;
  12. /**
  13. * Customer address model
  14. *
  15. * @api
  16. * @method int getParentId() getParentId()
  17. * @method \Magento\Customer\Model\Address setParentId() setParentId(int $parentId)
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. * @since 100.0.2
  20. */
  21. class Address extends \Magento\Customer\Model\Address\AbstractAddress
  22. {
  23. /**
  24. * Customer entity
  25. *
  26. * @var Customer
  27. */
  28. protected $_customer;
  29. /**
  30. * @var CustomerFactory
  31. */
  32. protected $_customerFactory;
  33. /**
  34. * @var \Magento\Framework\Reflection\DataObjectProcessor
  35. */
  36. protected $dataProcessor;
  37. /**
  38. * @var \Magento\Framework\Api\DataObjectHelper
  39. */
  40. protected $dataObjectHelper;
  41. /**
  42. * @var \Magento\Framework\Indexer\IndexerRegistry
  43. */
  44. protected $indexerRegistry;
  45. /**
  46. * @var \Magento\Customer\Model\Address\CustomAttributeListInterface
  47. */
  48. private $attributeList;
  49. /**
  50. * @param \Magento\Framework\Model\Context $context
  51. * @param \Magento\Framework\Registry $registry
  52. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  53. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  54. * @param \Magento\Directory\Helper\Data $directoryData
  55. * @param \Magento\Eav\Model\Config $eavConfig
  56. * @param Address\Config $addressConfig
  57. * @param \Magento\Directory\Model\RegionFactory $regionFactory
  58. * @param \Magento\Directory\Model\CountryFactory $countryFactory
  59. * @param AddressMetadataInterface $metadataService
  60. * @param AddressInterfaceFactory $addressDataFactory
  61. * @param RegionInterfaceFactory $regionDataFactory
  62. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  63. * @param CustomerFactory $customerFactory
  64. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
  65. * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
  66. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  67. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  68. * @param array $data
  69. *
  70. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  71. */
  72. public function __construct(
  73. \Magento\Framework\Model\Context $context,
  74. \Magento\Framework\Registry $registry,
  75. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  76. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  77. \Magento\Directory\Helper\Data $directoryData,
  78. \Magento\Eav\Model\Config $eavConfig,
  79. \Magento\Customer\Model\Address\Config $addressConfig,
  80. \Magento\Directory\Model\RegionFactory $regionFactory,
  81. \Magento\Directory\Model\CountryFactory $countryFactory,
  82. AddressMetadataInterface $metadataService,
  83. AddressInterfaceFactory $addressDataFactory,
  84. RegionInterfaceFactory $regionDataFactory,
  85. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  86. CustomerFactory $customerFactory,
  87. \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor,
  88. \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
  89. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  90. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  91. array $data = []
  92. ) {
  93. $this->dataProcessor = $dataProcessor;
  94. $this->_customerFactory = $customerFactory;
  95. $this->indexerRegistry = $indexerRegistry;
  96. parent::__construct(
  97. $context,
  98. $registry,
  99. $extensionFactory,
  100. $customAttributeFactory,
  101. $directoryData,
  102. $eavConfig,
  103. $addressConfig,
  104. $regionFactory,
  105. $countryFactory,
  106. $metadataService,
  107. $addressDataFactory,
  108. $regionDataFactory,
  109. $dataObjectHelper,
  110. $resource,
  111. $resourceCollection,
  112. $data
  113. );
  114. }
  115. /**
  116. * Init model
  117. *
  118. * @return void
  119. */
  120. protected function _construct()
  121. {
  122. $this->_init(\Magento\Customer\Model\ResourceModel\Address::class);
  123. }
  124. /**
  125. * Update Model with the data from Data Interface
  126. *
  127. * @param AddressInterface $address
  128. * @return $this
  129. * Use Api/RepositoryInterface for the operations in the Data Interfaces. Don't rely on Address Model
  130. */
  131. public function updateData(AddressInterface $address)
  132. {
  133. // Set all attributes
  134. $attributes = $this->dataProcessor
  135. ->buildOutputDataArray($address, \Magento\Customer\Api\Data\AddressInterface::class);
  136. foreach ($attributes as $attributeCode => $attributeData) {
  137. if (AddressInterface::REGION === $attributeCode) {
  138. $this->setRegion($address->getRegion()->getRegion());
  139. $this->setRegionCode($address->getRegion()->getRegionCode());
  140. $this->setRegionId($address->getRegion()->getRegionId());
  141. } else {
  142. $this->setDataUsingMethod($attributeCode, $attributeData);
  143. }
  144. }
  145. // Need to explicitly set this due to discrepancy in the keys between model and data object
  146. $this->setIsDefaultBilling($address->isDefaultBilling());
  147. $this->setIsDefaultShipping($address->isDefaultShipping());
  148. $customAttributes = $address->getCustomAttributes();
  149. if ($customAttributes !== null) {
  150. foreach ($customAttributes as $attribute) {
  151. $this->setData($attribute->getAttributeCode(), $attribute->getValue());
  152. }
  153. }
  154. return $this;
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function getDataModel($defaultBillingAddressId = null, $defaultShippingAddressId = null)
  160. {
  161. if ($this->getCustomerId() || $this->getParentId()) {
  162. $customer = $this->getCustomer();
  163. $defaultBillingAddressId = $customer->getDefaultBilling() ?: $defaultBillingAddressId;
  164. $defaultShippingAddressId = $customer->getDefaultShipping() ?: $defaultShippingAddressId;
  165. }
  166. return parent::getDataModel($defaultBillingAddressId, $defaultShippingAddressId);
  167. }
  168. /**
  169. * Retrieve address customer identifier
  170. *
  171. * @return int
  172. */
  173. public function getCustomerId()
  174. {
  175. return $this->_getData('customer_id') ? $this->_getData('customer_id') : $this->getParentId();
  176. }
  177. /**
  178. * Declare address customer identifier
  179. *
  180. * @param int $id
  181. * @return $this
  182. */
  183. public function setCustomerId($id)
  184. {
  185. $this->setParentId($id);
  186. $this->setData('customer_id', $id);
  187. return $this;
  188. }
  189. /**
  190. * Retrieve address customer
  191. *
  192. * @return Customer|false
  193. */
  194. public function getCustomer()
  195. {
  196. if (!$this->getCustomerId()) {
  197. return false;
  198. }
  199. if (empty($this->_customer)) {
  200. $this->_customer = $this->_createCustomer()->load($this->getCustomerId());
  201. }
  202. return $this->_customer;
  203. }
  204. /**
  205. * Specify address customer
  206. *
  207. * @param Customer $customer
  208. * @return $this
  209. */
  210. public function setCustomer(Customer $customer)
  211. {
  212. $this->_customer = $customer;
  213. $this->setCustomerId($customer->getId());
  214. return $this;
  215. }
  216. /**
  217. * Retrieve address entity attributes
  218. *
  219. * @return Attribute[]
  220. */
  221. public function getAttributes()
  222. {
  223. $attributes = $this->getData('attributes');
  224. if ($attributes === null) {
  225. $attributes = $this->_getResource()->loadAllAttributes($this)->getSortedAttributes();
  226. $this->setData('attributes', $attributes);
  227. }
  228. return $attributes;
  229. }
  230. /**
  231. * Get attributes created by default
  232. *
  233. * @return string[]
  234. */
  235. public function getDefaultAttributeCodes()
  236. {
  237. return $this->_getResource()->getDefaultAttributes();
  238. }
  239. /**
  240. * Clone object handler
  241. *
  242. * @return void
  243. */
  244. public function __clone()
  245. {
  246. $this->setId(null);
  247. }
  248. /**
  249. * Return Entity Type instance
  250. *
  251. * @return \Magento\Eav\Model\Entity\Type
  252. */
  253. public function getEntityType()
  254. {
  255. return $this->_getResource()->getEntityType();
  256. }
  257. /**
  258. * Return Region ID
  259. *
  260. * @return int
  261. */
  262. public function getRegionId()
  263. {
  264. return (int)$this->getData('region_id');
  265. }
  266. /**
  267. * Set Region ID. $regionId is automatically converted to integer
  268. *
  269. * @param int $regionId
  270. * @return $this
  271. */
  272. public function setRegionId($regionId)
  273. {
  274. $this->setData('region_id', (int)$regionId);
  275. return $this;
  276. }
  277. /**
  278. * Create customer model
  279. *
  280. * @return Customer
  281. */
  282. protected function _createCustomer()
  283. {
  284. return $this->_customerFactory->create();
  285. }
  286. /**
  287. * Return Entity Type ID
  288. *
  289. * @return int
  290. */
  291. public function getEntityTypeId()
  292. {
  293. return $this->getEntityType()->getId();
  294. }
  295. /**
  296. * Processing object after save data
  297. *
  298. * @return $this
  299. */
  300. public function afterSave()
  301. {
  302. $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
  303. if ($indexer->getState()->getStatus() == StateInterface::STATUS_VALID) {
  304. $this->_getResource()->addCommitCallback([$this, 'reindex']);
  305. }
  306. return parent::afterSave();
  307. }
  308. /**
  309. * Init indexing process after customer delete
  310. *
  311. * @return \Magento\Framework\Model\AbstractModel
  312. */
  313. public function afterDeleteCommit()
  314. {
  315. $this->reindex();
  316. return parent::afterDeleteCommit();
  317. }
  318. /**
  319. * Init indexing process after customer save
  320. *
  321. * @return void
  322. */
  323. public function reindex()
  324. {
  325. /** @var \Magento\Framework\Indexer\IndexerInterface $indexer */
  326. $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
  327. $indexer->reindexRow($this->getCustomerId());
  328. }
  329. /**
  330. * @inheritdoc
  331. * @since 100.0.6
  332. */
  333. protected function getCustomAttributesCodes()
  334. {
  335. return array_keys($this->getAttributeList()->getAttributes());
  336. }
  337. /**
  338. * Get new AttributeList dependency for application code.
  339. *
  340. * @return \Magento\Customer\Model\Address\CustomAttributeListInterface
  341. * @deprecated 100.0.6
  342. */
  343. private function getAttributeList()
  344. {
  345. if (!$this->attributeList) {
  346. $this->attributeList = \Magento\Framework\App\ObjectManager::getInstance()->get(
  347. \Magento\Customer\Model\Address\CustomAttributeListInterface::class
  348. );
  349. }
  350. return $this->attributeList;
  351. }
  352. /**
  353. * Retrieve attribute set id for customer address.
  354. *
  355. * @return int
  356. * @since 101.0.0
  357. */
  358. public function getAttributeSetId()
  359. {
  360. return parent::getAttributeSetId() ?: AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
  361. }
  362. }