Skip to content

Commit 4fcf9e4

Browse files
committed
Added remaining quote andorder anonymization
1 parent dcd6022 commit 4fcf9e4

2 files changed

Lines changed: 104 additions & 24 deletions

File tree

app/code/community/IntegerNet/Anonymizer/Model/Customer.php renamed to app/code/community/IntegerNet/Anonymizer/Model/Anonymizer.php

Lines changed: 103 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @package IntegerNet_Anonymizer
77
* @author Andreas von Studnitz <avs@integer-net.de>
88
*/
9-
class IntegerNet_Anonymizer_Model_Customer
9+
class IntegerNet_Anonymizer_Model_Anonymizer
1010
{
1111
protected $_unusedCustomerData = array();
1212
protected $_anonymizedOrderIds = array();
@@ -15,7 +15,6 @@ class IntegerNet_Anonymizer_Model_Customer
1515
protected $_anonymizedQuoteAddressIds = array();
1616
protected $_anonymizedNewsletterSubscriberIds = array();
1717

18-
1918
public function anonymizeAll()
2019
{
2120
/** @var $customers Mage_Customer_Model_Resource_Customer_Collection */
@@ -28,6 +27,9 @@ public function anonymizeAll()
2827
$this->_fetchRandomCustomerData($customerCount * 2);
2928

3029
$this->_anonymizeCustomers($customers);
30+
31+
$this->_anonymizeRemainingOrders();
32+
$this->_anonymizeRemainingQuotes();
3133
}
3234

3335
/**
@@ -90,25 +92,95 @@ protected function _anonymizeOrders($customer, $randomData)
9092
->getCollection()
9193
->addFieldToFilter('customer_email', $customer->getOrigData('email'));
9294

95+
foreach($orders as $order) {
96+
$this->_anonymizeOrder($order, $randomData);
97+
}
98+
}
99+
100+
/**
101+
*
102+
*/
103+
protected function _anonymizeRemainingOrders()
104+
{
105+
$orders = Mage::getModel('sales/order')
106+
->getCollection()
107+
->addFieldToFilter('entity_id', array('nin' => $this->_anonymizedOrderIds));
108+
93109
foreach($orders as $order) {
94110

95111
/** @var $order Mage_Sales_Model_Order */
96-
foreach ($this->_getOrderMapping() as $orderKey => $randomDataKey) {
97-
if (!$order->getData($orderKey)) {
98-
continue;
112+
$randomData = $this->_getRandomData();
113+
$this->_anonymizeOrder($order, $randomData);
114+
115+
foreach($order->getAddressesCollection() as $orderAddress) {
116+
117+
/** @var $orderAddress Mage_Sales_Model_Order_Address */
118+
if ($orderAddress->getCustomerFirstname() == $order->getOrigData('customer_firstname')
119+
&& $orderAddress->getCustomerLastname() == $order->getOrigData('customer_lastname')) {
120+
121+
$newRandomData = $randomData;
122+
} else {
123+
$newRandomData = $this->_getRandomData();
99124
}
100125

101-
if (strlen($randomDataKey)) {
102-
$order->setData($orderKey, $randomData[$randomDataKey]);
126+
$this->_anonymizeOrderAddress($orderAddress, $newRandomData);
127+
}
128+
}
129+
}
130+
131+
/**
132+
*
133+
*/
134+
protected function _anonymizeRemainingQuotes()
135+
{
136+
$quotes = Mage::getModel('sales/quote')
137+
->getCollection()
138+
->addFieldToFilter('entity_id', array('nin' => $this->_anonymizedQuoteIds));
139+
140+
foreach($quotes as $quote) {
141+
142+
/** @var $quote Mage_Sales_Model_Quote */
143+
$randomData = $this->_getRandomData();
144+
$this->_anonymizeQuote($quote, $randomData);
145+
146+
foreach($quote->getAddressesCollection() as $quoteAddress) {
147+
148+
/** @var $quoteAddress Mage_Sales_Model_Quote_Address */
149+
if ($quoteAddress->getCustomerFirstname() == $quote->getOrigData('customer_firstname')
150+
&& $quoteAddress->getCustomerLastname() == $quote->getOrigData('customer_lastname')) {
151+
152+
$newRandomData = $randomData;
103153
} else {
104-
$order->setData($orderKey, '');
154+
$newRandomData = $this->_getRandomData();
105155
}
156+
157+
$this->_anonymizeQuoteAddress($quoteAddress, $newRandomData);
106158
}
159+
}
160+
}
107161

108-
$order->getResource()->save($order);
162+
/**
163+
* @param Mage_Sales_Model_Order $order
164+
* @param array $randomData
165+
*/
166+
protected function _anonymizeOrder($order, $randomData)
167+
{
168+
/** @var $order Mage_Sales_Model_Order */
169+
foreach ($this->_getOrderMapping() as $orderKey => $randomDataKey) {
170+
if (!$order->getData($orderKey)) {
171+
continue;
172+
}
109173

110-
$this->_anonymizedOrderIds[] = $order->getId();
174+
if (strlen($randomDataKey)) {
175+
$order->setData($orderKey, $randomData[$randomDataKey]);
176+
} else {
177+
$order->setData($orderKey, '');
178+
}
111179
}
180+
181+
$order->getResource()->save($order);
182+
183+
$this->_anonymizedOrderIds[] = $order->getId();
112184
}
113185

114186
/**
@@ -122,24 +194,32 @@ protected function _anonymizeQuotes($customer, $randomData)
122194
->addFieldToFilter('customer_id', $customer->getId());
123195

124196
foreach($quotes as $quote) {
197+
$this->_anonymizeQuote($quote, $randomData);
198+
}
199+
}
125200

126-
/** @var $quote Mage_Sales_Model_Quote */
127-
foreach ($this->_getQuoteMapping() as $quoteKey => $randomDataKey) {
128-
if (!$quote->getData($quoteKey)) {
129-
continue;
130-
}
201+
/**
202+
* @param Mage_Sales_Model_Quote $quote
203+
* @param array $randomData
204+
*/
205+
protected function _anonymizeQuote($quote, $randomData)
206+
{
207+
/** @var $quote Mage_Sales_Model_Quote */
208+
foreach ($this->_getQuoteMapping() as $quoteKey => $randomDataKey) {
209+
if (!$quote->getData($quoteKey)) {
210+
continue;
211+
}
131212

132-
if (strlen($randomDataKey)) {
133-
$quote->setData($quoteKey, $randomData[$randomDataKey]);
134-
} else {
135-
$quote->setData($quoteKey, '');
136-
}
213+
if (strlen($randomDataKey)) {
214+
$quote->setData($quoteKey, $randomData[$randomDataKey]);
215+
} else {
216+
$quote->setData($quoteKey, '');
137217
}
218+
}
138219

139-
$quote->getResource()->save($quote);
220+
$quote->getResource()->save($quote);
140221

141-
$this->_anonymizedQuoteIds[] = $quote->getId();
142-
}
222+
$this->_anonymizedQuoteIds[] = $quote->getId();
143223
}
144224

145225
/**

shell/anonymizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Mage_Shell_Anonymizer extends Mage_Shell_Abstract
4141
*/
4242
public function run()
4343
{
44-
Mage::getModel('anonymizer/customer')->anonymizeAll();
44+
Mage::getModel('anonymizer/anonymizer')->anonymizeAll();
4545
}
4646

4747
/**

0 commit comments

Comments
 (0)