1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* integer_net Magento Module |
4
|
|
|
* |
5
|
|
|
* @category IntegerNet |
6
|
|
|
* @package IntegerNet_Anonymizer |
7
|
|
|
* @copyright Copyright (c) 2015 integer_net GmbH (http://www.integer-net.de/) |
8
|
|
|
* @author Fabian Schmengler <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace IntegerNet\Anonymizer; |
11
|
|
|
|
12
|
|
|
use IntegerNet\Anonymizer\Implementor\AnonymizableEntity; |
13
|
|
|
use IntegerNet\Anonymizer\Implementor\CollectionIterator; |
14
|
|
|
|
15
|
|
|
class Updater |
16
|
|
|
{ |
17
|
|
|
const __CLASS = __CLASS__; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Anonymizer |
21
|
|
|
*/ |
22
|
|
|
private $anonymizer; |
23
|
|
|
/** |
24
|
|
|
* @var null|resource |
25
|
|
|
*/ |
26
|
|
|
private $outputStream = null; |
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $showProgress = true; |
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $progressSteps = 1; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var AnonymizableEntity |
38
|
|
|
*/ |
39
|
|
|
private $entityModel; |
40
|
|
|
|
41
|
|
|
public function __construct(Anonymizer $anonymizer) |
42
|
|
|
{ |
43
|
|
|
$this->anonymizer = $anonymizer; |
44
|
|
|
} |
45
|
|
|
/** |
46
|
|
|
* @param null|resource $stream writable stream resource or null if no output required |
47
|
|
|
*/ |
48
|
|
|
public function setOutputStream($stream) |
49
|
|
|
{ |
50
|
|
|
$this->outputStream = $stream; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param boolean $showProgress |
55
|
|
|
*/ |
56
|
|
|
public function setShowProgress($showProgress) |
57
|
|
|
{ |
58
|
|
|
$this->showProgress = $showProgress; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $progressSteps |
63
|
|
|
*/ |
64
|
|
|
public function setProgressSteps($progressSteps) |
65
|
|
|
{ |
66
|
|
|
$this->progressSteps = $progressSteps; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function update(CollectionIterator $iterator, AnonymizableEntity $entityModel) |
70
|
|
|
{ |
71
|
|
|
$this->entityModel = $entityModel; |
72
|
|
|
$this->outputStart(); |
73
|
|
|
$iterator->walk(array($this, 'updateCurrentRow')); |
74
|
|
|
$this->anonymizer->resetUniqueGenerator(); |
75
|
|
|
$this->outputDone(); |
76
|
|
|
$this->entityModel = null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param CollectionIterator $iterator |
81
|
|
|
*/ |
82
|
|
|
public function updateCurrentRow(CollectionIterator $iterator) |
83
|
|
|
{ |
84
|
|
|
$this->entityModel->setRawData($iterator->getRawData()); |
85
|
|
|
$this->anonymizer->anonymize(array($this->entityModel)); |
86
|
|
|
$this->entityModel->updateValues(); |
87
|
|
|
$this->entityModel->clearInstance(); |
88
|
|
|
$this->outputIteratorStatus($iterator); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function outputStart() |
92
|
|
|
{ |
93
|
|
View Code Duplication |
if (is_resource($this->outputStream) && get_resource_type($this->outputStream) === 'stream') { |
|
|
|
|
94
|
|
|
fwrite($this->outputStream, sprintf("Updater started at %s.\n", date('H:i:s'))); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param CollectionIterator $iterator |
100
|
|
|
*/ |
101
|
|
|
private function outputIteratorStatus(CollectionIterator $iterator) |
102
|
|
|
{ |
103
|
|
|
if ($this->showProgress && (($iterator->getIteration() + 1) % $this->progressSteps === 0) |
104
|
|
|
&& is_resource($this->outputStream) && get_resource_type($this->outputStream) === 'stream' |
105
|
|
|
) { |
106
|
|
|
fwrite($this->outputStream, sprintf("\rUpdating %s: %s/%s - Memory usage %s Bytes", |
107
|
|
|
$this->entityModel->getEntityName(), |
108
|
|
|
str_pad($iterator->getIteration() + 1, strlen($iterator->getSize()), ' '), $iterator->getSize(), |
109
|
|
|
number_format(memory_get_usage()))); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function outputDone() |
114
|
|
|
{ |
115
|
|
View Code Duplication |
if (is_resource($this->outputStream) && get_resource_type($this->outputStream) === 'stream') { |
|
|
|
|
116
|
|
|
fwrite($this->outputStream, sprintf("\nUpdater finished at %s.\n\n", date('H:i:s'))); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.