Passed
Push — master ( c103e2...560387 )
by Emmanuel
03:39
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
72
     * @param callable|null $callback
73
     * @param bool          $pretend
74
     * @param bool          $returnResult
0 ignored issues
show
Documentation introduced by
There is no parameter named $returnResult. Did you maybe mean $returnRes?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
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 15
    public function setConfiguration(Reader $configuration)
90
    {
91 15
        $this->configuration = $configuration;
92 15
        $this->configEntites = $configuration->getConfigValues()['entities'];
93 15
    }
94
95
96
    /**
97
     * Evaluate, from the configuration if I have to update or Truncate the table
98
     *
99
     * @return int
100
     */
101 12
    protected function whatToDoWithEntity(): int
102
    {
103 12
        $this->checkEntityIsInConfig();
104
105 10
        $entityConfig = $this->configEntites[$this->entity];
106
107 10
        $actions = 0;
108 10
        if (array_key_exists('delete', $entityConfig) && $entityConfig['delete'] === true) {
109 4
            $actions |= self::TRUNCATE_TABLE;
110
        }
111
112 10
        if (array_key_exists('cols', $entityConfig)) {
113 8
            $actions |= self::UPDATE_TABLE;
114
        }
115
116 10
        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 7
    protected function generateFakeData(): array
143
    {
144 7
        $this->checkEntityIsInConfig();
145
146 7
        $faker = \Faker\Factory::create();
147
148 7
        $colsInConfig = $this->configEntites[$this->entity]['cols'];
149 7
        $row = [];
150 7
        foreach ($colsInConfig as $colName => $colProps) {
151 7
            $args = empty($colProps['params']) ? [] : $colProps['params'];
152 7
            $data = call_user_func_array([$faker, $colProps['method']], $args);
153
154 7
            $row[$colName] = $data;
155
156 7
            $colLength = $this->entityCols[$colName]['length'];
157
            // Cut the value if too long ...
158 7
            if (!empty($colLength) && strlen($data) > $colLength) {
159 7
                $row[$colName] = substr($data, 0, $this->entityCols[$colName]['length']);
160
            }
161
        }
162
163 7
        return $row;
164
    }
165
166
167
    /**
168
     * Make sure that entity is defined in the configuration
169
     *
170
     * @throws NeuralizerConfigurationException
171
     */
172 12
    private function checkEntityIsInConfig()
173
    {
174 12
        if (empty($this->configEntites)) {
175 1
            throw new NeuralizerConfigurationException(
176 1
                'No entities found. Have you loaded a configuration file ?'
177
            );
178
        }
179 11
        if (!array_key_exists($this->entity, $this->configEntites)) {
180 1
            throw new NeuralizerConfigurationException(
181 1
                "No configuration for that entity ({$this->entity})"
182
            );
183
        }
184 10
    }
185
}
186