Address.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Controller\Adminhtml\Group\Delete;
  10. use Magento\Customer\Model\CustomerRegistry;
  11. use Magento\Customer\Model\ResourceModel\Address\DeleteRelation;
  12. use Magento\Framework\App\ObjectManager;
  13. /**
  14. * Class Address
  15. *
  16. * @package Magento\Customer\Model\ResourceModel
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class Address extends \Magento\Eav\Model\Entity\VersionControl\AbstractEntity
  20. {
  21. /**
  22. * @var \Magento\Framework\Validator\Factory
  23. */
  24. protected $_validatorFactory;
  25. /**
  26. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  27. */
  28. protected $customerRepository;
  29. /**
  30. * @param \Magento\Eav\Model\Entity\Context $context
  31. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
  32. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite
  33. * @param \Magento\Framework\Validator\Factory $validatorFactory
  34. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  35. * @param array $data
  36. */
  37. public function __construct(
  38. \Magento\Eav\Model\Entity\Context $context,
  39. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
  40. \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite,
  41. \Magento\Framework\Validator\Factory $validatorFactory,
  42. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  43. $data = []
  44. ) {
  45. $this->customerRepository = $customerRepository;
  46. $this->_validatorFactory = $validatorFactory;
  47. parent::__construct($context, $entitySnapshot, $entityRelationComposite, $data);
  48. }
  49. /**
  50. * Resource initialization.
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. $this->connectionName = 'customer';
  57. }
  58. /**
  59. * Getter and lazy loader for _type
  60. *
  61. * @throws \Magento\Framework\Exception\LocalizedException
  62. * @return \Magento\Eav\Model\Entity\Type
  63. */
  64. public function getEntityType()
  65. {
  66. if (empty($this->_type)) {
  67. $this->setType('customer_address');
  68. }
  69. return parent::getEntityType();
  70. }
  71. /**
  72. * Check customer address before saving
  73. *
  74. * @param \Magento\Framework\DataObject $address
  75. * @return $this
  76. */
  77. protected function _beforeSave(\Magento\Framework\DataObject $address)
  78. {
  79. parent::_beforeSave($address);
  80. $this->_validate($address);
  81. return $this;
  82. }
  83. /**
  84. * Validate customer address entity
  85. *
  86. * @param \Magento\Framework\DataObject $address
  87. * @return void
  88. * @throws \Magento\Framework\Validator\Exception When validation failed
  89. */
  90. protected function _validate($address)
  91. {
  92. if ($address->getDataByKey('should_ignore_validation')) {
  93. return;
  94. };
  95. $validator = $this->_validatorFactory->createValidator('customer_address', 'save');
  96. if (!$validator->isValid($address)) {
  97. throw new \Magento\Framework\Validator\Exception(
  98. null,
  99. null,
  100. $validator->getMessages()
  101. );
  102. }
  103. }
  104. /**
  105. * @inheritdoc
  106. */
  107. public function delete($object)
  108. {
  109. $result = parent::delete($object);
  110. $object->setData([]);
  111. return $result;
  112. }
  113. /**
  114. * Get instance of DeleteRelation class
  115. *
  116. * @deprecated 101.0.0
  117. * @return DeleteRelation
  118. */
  119. private function getDeleteRelation()
  120. {
  121. return ObjectManager::getInstance()->get(DeleteRelation::class);
  122. }
  123. /**
  124. * Get instance of CustomerRegistry class
  125. *
  126. * @deprecated 101.0.0
  127. * @return CustomerRegistry
  128. */
  129. private function getCustomerRegistry()
  130. {
  131. return ObjectManager::getInstance()->get(CustomerRegistry::class);
  132. }
  133. /**
  134. * After delete entity process
  135. *
  136. * @param \Magento\Customer\Model\Address $address
  137. * @return $this
  138. */
  139. protected function _afterDelete(\Magento\Framework\DataObject $address)
  140. {
  141. $customer = $this->getCustomerRegistry()->retrieve($address->getCustomerId());
  142. $this->getDeleteRelation()->deleteRelation($address, $customer);
  143. return parent::_afterDelete($address);
  144. }
  145. }