Skip to content

Commit ccbf836

Browse files
committed
Added newsletter subscriber anonymization and error handling
1 parent 892aa18 commit ccbf836

2 files changed

Lines changed: 62 additions & 17 deletions

File tree

app/code/community/IntegerNet/Anonymizer/Model/Anonymizer.php

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function anonymizeAll()
3030

3131
$this->_anonymizeCustomers($customers);
3232

33+
$this->_anonymizeRemainingNewsletterSubscribers();
34+
3335
$this->_anonymizeRemainingOrders();
3436
$this->_anonymizeRemainingQuotes();
3537

@@ -70,24 +72,18 @@ protected function _anonymizeCustomer($customer)
7072
$customer->getResource()->save($customer);
7173
$this->_anonymizedCustomerIds[] = $customer->getId();
7274

75+
/* @var $subscriber Mage_Newsletter_Model_Subscriber */
76+
$subscriber = Mage::getModel('newsletter/subscriber');
77+
$subscriber->loadByEmail($customer->getOrigData('email'));
78+
if ($subscriber->getId()) {
79+
$this->_anonymizeNewsletterSubscriber($subscriber, $randomData);
80+
}
81+
7382
$this->_anonymizeQuotes($customer, $randomData);
7483
$this->_anonymizeOrders($customer, $randomData);
7584
$this->_anonymizeCustomerAddresses($customer, $randomData);
7685
}
7786

78-
/**
79-
* @return array
80-
*/
81-
protected function _getRandomData()
82-
{
83-
$randomData = array_pop($this->_unusedCustomerData);
84-
if (is_null($randomData)) {
85-
$this->_fetchRandomCustomerData(100);
86-
$randomData = array_pop($this->_unusedCustomerData);
87-
}
88-
return $randomData;
89-
}
90-
9187
/**
9288
* @param Mage_Customer_Model_Customer $customer
9389
* @param array $randomData
@@ -396,6 +392,35 @@ protected function _anonymizeOrderAddress($orderAddress, $randomData)
396392
}
397393
}
398394

395+
/**
396+
*
397+
*/
398+
protected function _anonymizeRemainingNewsletterSubscribers()
399+
{
400+
$newsletterSubscribers = Mage::getModel('newsletter/subscriber')
401+
->getCollection()
402+
->addFieldToFilter('subscriber_id', array('nin' => $this->_anonymizedNewsletterSubscriberIds));
403+
404+
foreach($newsletterSubscribers as $newsletterSubscriber) {
405+
406+
/** @var $newsletterSubscriber Mage_Newsletter_Model_Subscriber */
407+
$randomData = $this->_getRandomData();
408+
$this->_anonymizeNewsletterSubscriber($newsletterSubscriber, $randomData);
409+
}
410+
}
411+
412+
/**
413+
* @param Mage_Newsletter_Model_Subscriber $subscriber
414+
* @param array $randomData
415+
*/
416+
protected function _anonymizeNewsletterSubscriber($subscriber, $randomData)
417+
{
418+
$subscriber->setData('subscriber_email', $randomData['email']);
419+
$subscriber->getResource()->save($subscriber);
420+
421+
$this->_anonymizedNewsletterSubscriberIds[] = $subscriber->getId();
422+
}
423+
399424
/**
400425
* @return array
401426
*/
@@ -465,6 +490,19 @@ protected function _getAddressMapping()
465490
);
466491
}
467492

493+
/**
494+
* @return array
495+
*/
496+
protected function _getRandomData()
497+
{
498+
$randomData = array_pop($this->_unusedCustomerData);
499+
if (is_null($randomData)) {
500+
$this->_fetchRandomCustomerData(100);
501+
$randomData = array_pop($this->_unusedCustomerData);
502+
}
503+
return $randomData;
504+
}
505+
468506
/**
469507
* @param int $count
470508
* @return array
@@ -473,6 +511,9 @@ protected function _fetchRandomCustomerData($count)
473511
{
474512
$url = "http://fakester.biz/json?n=$count";
475513
$json = file_get_contents($url);
514+
if ($json === false) {
515+
Mage::throwException('Connection to http://fakester.biz failed.');
516+
}
476517
$this->_unusedCustomerData = Zend_Json::decode($json);
477518

478519
/*

shell/anonymizer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ class Mage_Shell_Anonymizer extends Mage_Shell_Abstract
4242
public function run()
4343
{
4444
/** @var $anonymizer IntegerNet_Anonymizer_Model_Anonymizer */
45-
$anonymizer = Mage::getModel('anonymizer/anonymizer');
46-
$anonymizer->anonymizeAll();
47-
foreach($anonymizer->getResults() as $resultLabel => $resultCount) {
48-
echo 'Anonymized ' . $resultCount . ' ' . $resultLabel . ".\n";
45+
try {
46+
$anonymizer = Mage::getModel('anonymizer/anonymizer');
47+
$anonymizer->anonymizeAll();
48+
foreach ($anonymizer->getResults() as $resultLabel => $resultCount) {
49+
echo 'Anonymized ' . $resultCount . ' ' . $resultLabel . ".\n";
50+
}
51+
} catch (Exception $e) {
52+
echo 'Error: ' . $e->getMessage();
4953
}
5054
}
5155

0 commit comments

Comments
 (0)