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
|
|
|
/** |
50
|
|
|
* Returns identifier, for example the customer email address. Entities with the same identifier will get the same |
51
|
|
|
* anonymized values. |
52
|
|
|
* |
53
|
|
|
* Important: The return value must not be affected by anonymization! |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getIdentifier() |
58
|
|
|
{ |
59
|
|
|
return $this->_identifier; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets identifier based on current entity |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
abstract protected function _setIdentifier(); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return AnonymizableValue[] |
71
|
|
|
*/ |
72
|
|
|
public function getValues() |
73
|
|
|
{ |
74
|
|
|
if ($this->_values === null) { |
75
|
|
|
$this->_values = array(); |
76
|
|
|
foreach ($this->_formattersByAttribute as $attribute => $formatter) { |
77
|
|
|
$this->_values[$attribute] = new AnonymizableValue( |
78
|
|
|
$formatter, $this->_entity->getDataUsingMethod($attribute), |
79
|
|
|
in_array($attribute, $this->_uniqueAttributes)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return $this->_values; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets raw data from database |
87
|
|
|
* |
88
|
|
|
* @param string[] $data |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function setRawData($data) |
92
|
|
|
{ |
93
|
|
|
$this->_entity->addData($data); |
94
|
|
|
// reset derived attributes: |
95
|
|
|
$this->_setIdentifier(); |
96
|
|
|
$this->_values = null; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Update values in database |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function updateValues() |
105
|
|
|
{ |
106
|
|
|
$resource = $this->_entity->getResource(); |
107
|
|
|
$staticAttributes = array(); |
108
|
|
|
foreach ($this->getValues() as $attributeCode => $value) { |
109
|
|
|
if (!$resource instanceof Mage_Eav_Model_Entity_Abstract) { |
|
|
|
|
110
|
|
|
$staticAttributes[$attributeCode] = $value->getValue(); |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
$attribute = Mage::getSingleton('eav/config')->getAttribute($this->_entityType, $attributeCode); |
114
|
|
|
if ($attribute->isStatic()) { |
115
|
|
|
$staticAttributes[$attributeCode] = $value->getValue(); |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
$this->_entity->setData($attributeCode, $value->getValue()); |
119
|
|
|
$resource->saveAttribute($this->_entity, $attributeCode); |
120
|
|
|
} |
121
|
|
|
if (!empty($staticAttributes)) { |
122
|
|
|
$this->_entity->addData($staticAttributes); |
123
|
|
|
if ($resource instanceof Mage_Eav_Model_Entity_Abstract) { |
|
|
|
|
124
|
|
|
$resource->isPartialSave(true); |
125
|
|
|
} |
126
|
|
|
try { |
127
|
|
|
$resource->save($this->_entity); |
128
|
|
|
} catch (Mage_Customer_Exception $e) { |
|
|
|
|
129
|
|
|
// 'This customer email already exists' |
130
|
|
|
//TODO instead exclude all customers with @example.* email addresses from the customer collection |
131
|
|
|
// AND from associated collections as well |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Reset to empty instance |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function clearInstance() |
142
|
|
|
{ |
143
|
|
|
$this->_entity->setData(array()); |
144
|
|
|
$this->_entity->clearInstance(); |
145
|
|
|
// reset derived attributes |
146
|
|
|
$this->setRawData(array()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return IntegerNet_Anonymizer_Model_Bridge_Iterator|null |
151
|
|
|
*/ |
152
|
|
|
public function getCollectionIterator() |
153
|
|
|
{ |
154
|
|
|
/** @var Varien_Data_Collection_Db $collection */ |
155
|
|
|
$collection = $this->_entity->getCollection(); |
156
|
|
|
$fields = array_unique(array_merge( |
157
|
|
|
array_keys($this->_formattersByAttribute), |
158
|
|
|
$this->_attributesUsedForIdentifier |
159
|
|
|
)); |
160
|
|
|
if ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) { |
|
|
|
|
161
|
|
|
$collection->addAttributeToSelect($fields, 'left'); |
162
|
|
|
} elseif ($collection instanceof Mage_Core_Model_Resource_Db_Collection_Abstract) { |
|
|
|
|
163
|
|
|
$collection->addFieldToSelect($fields); |
164
|
|
|
} |
165
|
|
|
$iterationOffset = $this->currentPage * self::ROWS_PER_QUERY; |
166
|
|
|
if ($collection->getSize() <= $iterationOffset) { |
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
$this->currentPage++; |
170
|
|
|
$collection->getSelect()->limitPage($this->currentPage, self::ROWS_PER_QUERY); |
171
|
|
|
/** @var IntegerNet_Anonymizer_Model_Bridge_Iterator $iterator */ |
172
|
|
|
$iterator = Mage::getModel('integernet_anonymizer/bridge_iterator', $collection); |
173
|
|
|
$iterator->setIterationOffset($iterationOffset); |
174
|
|
|
return $iterator; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Check if the entity exists |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function entityExists() |
182
|
|
|
{ |
183
|
|
|
return (bool)$this->_entity; |
184
|
|
|
} |
185
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..