1
|
|
|
<?php |
2
|
|
|
use IntegerNet\Anonymizer\AnonymizableValue; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* integer_net Magento Module |
6
|
|
|
* |
7
|
|
|
* @category IntegerNet |
8
|
|
|
* @package IntegerNet_Anonymizer |
9
|
|
|
* @copyright Copyright (c) 2015 integer_net GmbH (http://www.integer-net.de/) |
10
|
|
|
* @author Fabian Schmengler <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class IntegerNet_Anonymizer_Model_Bridge_Entity_Abstract |
|
|
|
|
13
|
|
|
implements IntegerNet\Anonymizer\Implementor\AnonymizableEntity |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
const ROWS_PER_QUERY = 50000; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $_identifier; |
20
|
|
|
/** |
21
|
|
|
* @var Mage_Core_Model_Abstract |
22
|
|
|
*/ |
23
|
|
|
protected $_entity; |
24
|
|
|
/** |
25
|
|
|
* @var string|null is overridden with entity type code if the model is a EAV entity |
26
|
|
|
*/ |
27
|
|
|
protected $_entityType = null; |
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
protected $_formattersByAttribute = array(); |
32
|
|
|
/** |
33
|
|
|
* @var string[] Specifies attributes that will be loaded |
34
|
|
|
*/ |
35
|
|
|
protected $_attributesUsedForIdentifier = array(); |
36
|
|
|
/** |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
protected $_uniqueAttributes = array(); |
40
|
|
|
/** |
41
|
|
|
* @var AnonymizableValue[] |
42
|
|
|
*/ |
43
|
|
|
protected $_values; |
44
|
|
|
/** |
45
|
|
|
* @var int Current page of collection for chunking |
46
|
|
|
*/ |
47
|
|
|
protected $currentPage = 0; |
48
|
|
|
/** |
49
|
|
|
* @var \IntegerNet\Anonymizer\Config\ExcludedEmailDomains |
50
|
|
|
*/ |
51
|
|
|
protected $excludedEmailDomains; |
52
|
|
|
|
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
$this->excludedEmailDomains = Mage::helper(IntegerNet_Anonymizer_Helper_Data::ALIAS)->excludedEmailDomains(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns true if entity is anonymizable, false if it should be left unchanged |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
function isAnonymizable() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns identifier, for example the customer email address. Entities with the same identifier will get the same |
70
|
|
|
* anonymized values. |
71
|
|
|
* |
72
|
|
|
* Important: The return value must not be affected by anonymization! |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getIdentifier() |
77
|
|
|
{ |
78
|
|
|
return $this->_identifier; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sets identifier based on current entity |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
abstract protected function _setIdentifier(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return AnonymizableValue[] |
90
|
|
|
*/ |
91
|
|
|
public function getValues() |
92
|
|
|
{ |
93
|
|
|
if ($this->_values === null) { |
94
|
|
|
$this->_values = array(); |
95
|
|
|
foreach ($this->_formattersByAttribute as $attribute => $formatter) { |
96
|
|
|
$this->_values[$attribute] = new AnonymizableValue( |
97
|
|
|
$formatter, $this->_entity->getDataUsingMethod($attribute), |
98
|
|
|
in_array($attribute, $this->_uniqueAttributes)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return $this->_values; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets raw data from database |
106
|
|
|
* |
107
|
|
|
* @param string[] $data |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function setRawData($data) |
111
|
|
|
{ |
112
|
|
|
$this->_entity->addData($data); |
113
|
|
|
// reset derived attributes: |
114
|
|
|
$this->_setIdentifier(); |
115
|
|
|
$this->_values = null; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Update values in database |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function updateValues() |
124
|
|
|
{ |
125
|
|
|
$resource = $this->_entity->getResource(); |
126
|
|
|
$staticAttributes = array(); |
127
|
|
|
foreach ($this->getValues() as $attributeCode => $value) { |
128
|
|
|
if (!$resource instanceof Mage_Eav_Model_Entity_Abstract) { |
|
|
|
|
129
|
|
|
$staticAttributes[$attributeCode] = $value->getValue(); |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
$attribute = Mage::getSingleton('eav/config')->getAttribute($this->_entityType, $attributeCode); |
133
|
|
|
if ($attribute->isStatic()) { |
134
|
|
|
$staticAttributes[$attributeCode] = $value->getValue(); |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
$this->_entity->setData($attributeCode, $value->getValue()); |
138
|
|
|
$resource->saveAttribute($this->_entity, $attributeCode); |
139
|
|
|
} |
140
|
|
|
if (!empty($staticAttributes)) { |
141
|
|
|
$this->_entity->addData($staticAttributes); |
142
|
|
|
if ($resource instanceof Mage_Eav_Model_Entity_Abstract) { |
|
|
|
|
143
|
|
|
$resource->isPartialSave(true); |
144
|
|
|
} |
145
|
|
|
try { |
146
|
|
|
$resource->save($this->_entity); |
147
|
|
|
} catch (Mage_Customer_Exception $e) { |
|
|
|
|
148
|
|
|
// 'This customer email already exists' |
149
|
|
|
//TODO instead exclude all customers with @example.* email addresses from the customer collection |
150
|
|
|
// AND from associated collections as well |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Reset to empty instance |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function clearInstance() |
161
|
|
|
{ |
162
|
|
|
$this->_entity->setData(array()); |
163
|
|
|
$this->_entity->clearInstance(); |
164
|
|
|
// reset derived attributes |
165
|
|
|
$this->setRawData(array()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return IntegerNet_Anonymizer_Model_Bridge_Iterator|null |
170
|
|
|
*/ |
171
|
|
|
public function getCollectionIterator() |
172
|
|
|
{ |
173
|
|
|
/** @var Varien_Data_Collection_Db $collection */ |
174
|
|
|
$collection = $this->_entity->getCollection(); |
175
|
|
|
$fields = array_unique(array_merge( |
176
|
|
|
array_keys($this->_formattersByAttribute), |
177
|
|
|
$this->_attributesUsedForIdentifier |
178
|
|
|
)); |
179
|
|
|
if ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) { |
|
|
|
|
180
|
|
|
$collection->addAttributeToSelect($fields, 'left'); |
181
|
|
|
} elseif ($collection instanceof Mage_Core_Model_Resource_Db_Collection_Abstract) { |
|
|
|
|
182
|
|
|
$collection->addFieldToSelect($fields); |
183
|
|
|
} |
184
|
|
|
$iterationOffset = $this->currentPage * self::ROWS_PER_QUERY; |
185
|
|
|
if ($collection->getSize() <= $iterationOffset) { |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
$this->currentPage++; |
189
|
|
|
$collection->getSelect()->limitPage($this->currentPage, self::ROWS_PER_QUERY); |
190
|
|
|
/** @var IntegerNet_Anonymizer_Model_Bridge_Iterator $iterator */ |
191
|
|
|
$iterator = Mage::getModel('integernet_anonymizer/bridge_iterator', $collection); |
192
|
|
|
$iterator->setIterationOffset($iterationOffset); |
193
|
|
|
return $iterator; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.