Passed
Push — master ( c10078...a55606 )
by Emmanuel
01:54
created

AbstractAnonymizer::getWhereConditionInConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 5
nop 0
crap 2
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Anonymizer;
19
20
use Edyan\Neuralyzer\Configuration\Reader;
21
use Edyan\Neuralyzer\Exception\NeuralizerConfigurationException;
22
23
/**
24
 * Abstract Anonymizer, that can be implemented as DB Anonymizer for example
25
 * Its goal is only to anonymize any data, from a simple array
26
 * not to write or read it from anywhere
27
 *
28
 */
29
abstract class AbstractAnonymizer
30
{
31
    /**
32
     * Constant to define the type of action for that table
33
     */
34
    const TRUNCATE_TABLE = 1;
35
36
    /**
37
     * Constant to define the type of action for that table
38
     */
39
    const UPDATE_TABLE = 2;
40
41
    /**
42
     * Contain the configuration object
43
     *
44
     * @var Reader
45
     */
46
    protected $configuration;
47
48
    /**
49
     * Configuration of entities
50
     *
51
     * @var array
52
     */
53
    protected $configEntites = [];
54
55
    /**
56
     * Current table (entity) to process
57
     * @var string
58
     */
59
    protected $entity;
60
61
    /**
62
     * Current table (entity) Columns
63
     * @var array
64
     */
65
    protected $entityCols;
66
67
68
    /**
69
     * Process the entity according to the anonymizer type
70
     *
71
     * @param string        $entity         Entity's name
72
     * @param callable|null $callback       Callback function with current row num as parameter
73
     * @param bool          $pretend        Simulate update
74
     * @param bool          $returnRes      Return queries
75
     */
76
    abstract public function processEntity(
77
        string $entity,
78
        callable $callback = null,
79
        bool $pretend = true,
80
        bool $returnRes = false
81
    ): array;
82
83
84
    /**
85
     * Set the configuration
86
     *
87
     * @param Reader $configuration
88
     */
89 17
    public function setConfiguration(Reader $configuration)
90
    {
91 17
        $this->configuration = $configuration;
92 17
        $this->configEntites = $configuration->getConfigValues()['entities'];
93 17
    }
94
95
96
    /**
97
     * Evaluate, from the configuration if I have to update or Truncate the table
98
     *
99
     * @return int
100
     */
101 14
    protected function whatToDoWithEntity(): int
102
    {
103 14
        $this->checkEntityIsInConfig();
104
105 12
        $entityConfig = $this->configEntites[$this->entity];
106
107 12
        $actions = 0;
108 12
        if (array_key_exists('delete', $entityConfig) && $entityConfig['delete'] === true) {
109 4
            $actions |= self::TRUNCATE_TABLE;
110
        }
111
112 12
        if (array_key_exists('cols', $entityConfig)) {
113 12
            $actions |= self::UPDATE_TABLE;
114
        }
115
116 12
        return $actions;
117
    }
118
119
120
    /**
121
     * Returns the 'delete_where' parameter for an entity in config (or empty)
122
     *
123
     * @return string
124
     */
125 4
    public function getWhereConditionInConfig(): string
126
    {
127 4
        $this->checkEntityIsInConfig();
128
129 4
        if (!array_key_exists('delete_where', $this->configEntites[$this->entity])) {
130 1
            return '';
131
        }
132
133 3
        return $this->configEntites[$this->entity]['delete_where'];
134
    }
135
136
137
    /**
138
     * Generate fake data for an entity and return it as an Array
139
     *
140
     * @return array
141
     */
142 9
    protected function generateFakeData(): array
143
    {
144 9
        $this->checkEntityIsInConfig();
145
146 9
        $faker = \Faker\Factory::create($this->configuration->getConfigValues()['language']);
147
148 9
        $colsInConfig = $this->configEntites[$this->entity]['cols'];
149 9
        $row = [];
150 9
        foreach ($colsInConfig as $colName => $colProps) {
151 9
            $this->checkColIsInEntity($colName);
152 8
            $args = empty($colProps['params']) ? [] : $colProps['params'];
153 8
            $data = call_user_func_array([$faker, $colProps['method']], $args);
154 8
            if (!is_scalar($data)) {
155 1
                $msg = "You must use faker methods that generate strings: '{$colProps['method']}' forbidden";
156 1
                throw new NeuralizerConfigurationException($msg);
157
            }
158
159 8
            $row[$colName] = $data;
160
161 8
            $colLength = $this->entityCols[$colName]['length'];
162
            // Cut the value if too long ...
163 8
            if (!empty($colLength) && strlen($data) > $colLength) {
164 8
                $row[$colName] = substr($data, 0, $this->entityCols[$colName]['length']);
165
            }
166
        }
167
168 7
        return $row;
169
    }
170
171
172
    /**
173
     * Make sure that entity is defined in the configuration
174
     *
175
     * @throws NeuralizerConfigurationException
176
     */
177 14
    private function checkEntityIsInConfig()
178
    {
179 14
        if (empty($this->configEntites)) {
180 1
            throw new NeuralizerConfigurationException(
181 1
                'No entities found. Have you loaded a configuration file ?'
182
            );
183
        }
184 13
        if (!array_key_exists($this->entity, $this->configEntites)) {
185 1
            throw new NeuralizerConfigurationException(
186 1
                "No configuration for that entity ({$this->entity})"
187
            );
188
        }
189 12
    }
190
191
    /**
192
     * Verify a column is defined in the real entityCols
193
     * @param  string $colName
194
     */
195 9
    private function checkColIsInEntity(string $colName)
196
    {
197 9
        if (!array_key_exists($colName, $this->entityCols)) {
198 1
            throw new NeuralizerConfigurationException("Col $colName does not exist");
199
        }
200 8
    }
201
}
202