Completed
Push — development ( bff125...0f5c1f )
by Fabian
07:22
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
    implements IntegerNet\Anonymizer\Implementor\AnonymizableEntity
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
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()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Int...zer\AnonymizableValue>> of property $_values.

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..

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Mage_Eav_Model_Entity_Abstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Mage_Eav_Model_Entity_Abstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
143
                $resource->isPartialSave(true);
144
            }
145
            try {
146
                $resource->save($this->_entity);
147
            } catch (Mage_Customer_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Mage_Customer_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Mage_Eav_Model_Entity_Collection_Abstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
180
            $collection->addAttributeToSelect($fields, 'left');
181
        } elseif ($collection instanceof Mage_Core_Model_Resource_Db_Collection_Abstract) {
0 ignored issues
show
Bug introduced by
The class Mage_Core_Model_Resource_Db_Collection_Abstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
}