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