Completed
Push — master ( 3a5c6d...7f7360 )
by Emmanuel
02:10
created

AbstractAnonymizer::getWhereConditionInConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2017 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 Inet\Neuralyzer\Anonymizer;
19
20
use Inet\Neuralyzer\Configuration\Reader;
21
use Inet\Neuralyzer\Exception\NeuralizerConfigurationException;
22
23
/**
24
 * Abstract Anonymizer
25
 */
26
abstract class AbstractAnonymizer
27
{
28
    /**
29
     * Constant to define the type of action for that table
30
     */
31
    const TRUNCATE_TABLE = 1;
32
33
    /**
34
     * Constant to define the type of action for that table
35
     */
36
    const UPDATE_TABLE = 2;
37
38
    /**
39
     * Contain the configuration object
40
     *
41
     * @var Reader
42
     */
43
    protected $configuration;
44
45
    /**
46
     * Configuration of entities
47
     *
48
     * @var array
49
     */
50
    protected $configEntites = [];
51
52
    /**
53
     * Process the entity according to the anonymizer type
54
     *
55
     * @param string        $entity
0 ignored issues
show
Bug introduced by
There is no parameter named $entity. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
56
     * @param callable|null $callback
57
     * @param bool          $pretend
58
     * @param bool          $returnResult
59
     */
60
    abstract public function processEntity(
61
        string $table,
62
        callable $callback = null,
63
        bool $pretend = true,
64
        bool $returnResult = false
65
    );
66
67
    /**
68
     * Set the configuration
69
     *
70
     * @param Reader $configuration
71
     */
72 2
    public function setConfiguration(Reader $configuration)
73
    {
74 2
        $this->configuration = $configuration;
75 2
        $configEntites = $configuration->getConfigValues();
76 2
        $this->configEntites = $configEntites['entities'];
77 2
    }
78
79
    /**
80
     * Evaluate, from the configuration if I have to update or Truncate the table
81
     *
82
     * @param string $entity
83
     *
84
     * @return int
85
     */
86 1
    public function whatToDoWithEntity(string $entity): int
87
    {
88 1
        $this->checkEntityIsInConfig($entity);
89
90 1
        $entityConfig = $this->configEntites[$entity];
91
92 1
        $actions = 0;
93 1
        if (array_key_exists('delete', $entityConfig) && $entityConfig['delete'] === true) {
94
            $actions |= self::TRUNCATE_TABLE;
95
        }
96
97 1
        if (array_key_exists('cols', $entityConfig)) {
98 1
            $actions |= self::UPDATE_TABLE;
99
        }
100
101 1
        return $actions;
102
    }
103
104
    /**
105
     * Returns the 'delete_where' parameter for an entity in config (or empty)
106
     *
107
     * @param string $entity
108
     *
109
     * @return string
110
     */
111
    public function getWhereConditionInConfig(string $entity): string
112
    {
113
        $this->checkEntityIsInConfig($entity);
114
115
        if (!array_key_exists('delete_where', $this->configEntites[$entity])) {
116
            return '';
117
        }
118
119
        return $this->configEntites[$entity]['delete_where'];
120
    }
121
122
    /**
123
     * Generate fake data for an entity and return it as an Array
124
     *
125
     * @param string $entity
126
     *
127
     * @return array
128
     */
129 1
    public function generateFakeData(string $entity): array
130
    {
131 1
        $this->checkEntityIsInConfig($entity);
132
133 1
        $faker = \Faker\Factory::create();
134
135 1
        $entityCols = $this->configEntites[$entity]['cols'];
136 1
        $entity = [];
137 1
        foreach ($entityCols as $colName => $colProps) {
138 1
            $args = empty($colProps['params']) ? [] : $colProps['params'];
139 1
            $data = call_user_func_array([$faker, $colProps['method']], $args);
140 1
            $entity[$colName] = $data;
141
        }
142
143 1
        return $entity;
144
    }
145
146
    /**
147
     * Make sure that entity is defined in the configuration
148
     *
149
     * @param string $entity
150
     *
151
     * @throws NeuralizerConfigurationException
152
     */
153 1
    private function checkEntityIsInConfig(string $entity)
154
    {
155 1
        if (empty($this->configEntites)) {
156
            throw new NeuralizerConfigurationException('No entities found. Have you loaded a configuration file ?');
157
        }
158 1
        if (!array_key_exists($entity, $this->configEntites)) {
159
            throw new NeuralizerConfigurationException("No configuration for that entity ($entity)");
160
        }
161 1
    }
162
}
163