Skip to content

Commit

Permalink
Module skeleton and basic anonymization of customers
Browse files Browse the repository at this point in the history
  • Loading branch information
avstudnitz committed Oct 13, 2012
0 parents commit 651a8b6
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
115 changes: 115 additions & 0 deletions app/code/community/IntegerNet/Anonymizer/Model/Customer.php
@@ -0,0 +1,115 @@
<?php
/**
* Magento Anonymizer Script
*
* @category IntegerNet
* @package IntegerNet_Anonymizer
* @author Andreas von Studnitz <avs@integer-net.de>
*/
class IntegerNet_Anonymizer_Model_Customer
{
protected $_unusedCustomerData = array();
protected $_anonymizedOrders = array();
protected $_anonymizedQuotes = array();
protected $_anonymizedNewsletterSubscribers = array();


public function anonymizeAll()
{
/** @var $customers Mage_Customer_Model_Resource_Customer_Collection */
$customers = Mage::getModel('customer/customer')
->getCollection();

$customerCount = $customers->getSize();

$this->_fetchRandomCustomerData($customerCount);

$this->_anonymizeCustomers($customers);
}

/**
* @param Mage_Customer_Model_Resource_Customer_Collection $customers
*/
protected function _anonymizeCustomers($customers)
{
foreach ($customers as $customer) {

$this->_anonymizeCustomer($customer);
}
}

/**
* @param Mage_Customer_Model_Customer $customer
*/
protected function _anonymizeCustomer($customer)
{
$randomData = array_pop($this->_unusedCustomerData);
if (is_null($randomData)) {
$this->_fetchRandomCustomerData(1);
$randomData = array_pop($this->_unusedCustomerData);
}

$customer->setData('prefix', $randomData['prefix']);
$customer->setData('firstname', $randomData['first_name']);
$customer->setData('middlename', '');
$customer->setData('lastname', $randomData['last_name']);
$customer->setData('suffix', $randomData['suffix']);
$customer->setData('email', $randomData['email']);
$customer->getResource()->save($customer);
}

/**
* @param Mage_Customer_Model_Customer $customer
* @param string $attributeCode
* @param mixed $value
*/
protected function setData($customer, $attributeCode, $value)
{
$customer->setData($attributeCode, $value);
}

/**
* @param int $count
* @return array
*/
protected function _fetchRandomCustomerData($count)
{
$url = "http://fakester.biz/json?n=$count";
$json = file_get_contents($url);
$this->_unusedCustomerData = Zend_Json::decode($json);

/*
* Fakester return these fields for customers:
*
* [name] => Johnson, Kreiger and Jenkins
* [first_name] => Citlalli
* [last_name] => Gorczany
* [prefix] => Dr.
* [suffix] => Inc
* [city] => Loisshire
* [city_prefix] => Lake
* [city_suffix] => bury
* [country] => United Arab Emirates
* [secondary_address] => Suite 720
* [state] => Wyoming
* [state_abbr] => OK
* [street_address] => 61204 Lang Garden
* [street_name] => Lakin Unions
* [street_suffix] => Dam
* [zip_code] => 38126-1906
* [bs] => unleash world-class technologies
* [catch_phrase] => Vision-oriented grid-enabled throughput
* [domain_name] => mayer.org
* [domain_suffix] => info
* [domain_word] => hoppe
* [email] => jefferey@baileysimonis.name
* [free_email] => emmitt@hotmail.com
* [ip_v4_address] => 163.49.36.30
* [ip_v6_address] => 61b4:5b6:7d1d:db11:ab29:e003:eb4:161f
* [user_name] => meghan
*
*/

return $this->_unusedCustomerData;
}
}
56 changes: 56 additions & 0 deletions app/code/community/IntegerNet/Anonymizer/etc/config.xml
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<!--
/**
* @category IntegerNet
* @package IntegerNet_Anonymizer
* @author Andreas von Studnitz <avs@integer-net.de>
*/
-->
<config>
<modules>
<IntegerNet_Anonymizer>
<version>0.1.0</version>
</IntegerNet_Anonymizer>
</modules>
<global>
<models>
<anonymizer>
<class>IntegerNet_Anonymizer_Model</class>
</anonymizer>
</models>
<helpers>
<anonymizer>
<class>IntegerNet_Anonymizer_Helper</class>
</anonymizer>
</helpers>
</global>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<anonymizer after="Mage_Adminhtml">IntegerNet_Anonymizer</anonymizer>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<adminhtml>
<translate>
<modules>
<IntegerNet_Anonymizer>
<files>
<default>IntegerNet_Anonymizer.csv</default>
</files>
</IntegerNet_Anonymizer>
</modules>
</translate>
<layout>
<updates>
<anonymizer>
<file>anonymizer.xml</file>
</anonymizer>
</updates>
</layout>
</adminhtml>
</config>
19 changes: 19 additions & 0 deletions app/etc/modules/IntegerNet_Anonymizer.xml
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!--
/**
* @category IntegerNet
* @package IntegerNet_Anonymizer
* @author Andreas von Studnitz <avs@integer-net.de>
*/
-->
<config>
<modules>
<IntegerNet_Anonymizer>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Customer />
</depends>
</IntegerNet_Anonymizer>
</modules>
</config>
61 changes: 61 additions & 0 deletions shell/anonymizer.php
@@ -0,0 +1,61 @@
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Shell
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

require_once 'abstract.php';

/**
* Magento Anonymizer Script
*
* @category IntegerNet
* @package IntegerNet_Anonymizer
* @author Andreas von Studnitz <avs@integer-net.de>
*/
class Mage_Shell_Anonymizer extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
Mage::getModel('anonymizer/customer')->anonymizeAll();
}

/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f anonymizer.php
USAGE;
}
}

$shell = new Mage_Shell_Anonymizer();
$shell->run();

0 comments on commit 651a8b6

Please sign in to comment.