Completed
Push — master ( 7f7360...1347a3 )
by Emmanuel
01:58
created

AbstractAnonymizer::processEntity()

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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 15
    public function setConfiguration(Reader $configuration)
73
    {
74 15
        $this->configuration = $configuration;
75 15
        $configEntites = $configuration->getConfigValues();
76 15
        $this->configEntites = $configEntites['entities'];
77 15
    }
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 14
    public function whatToDoWithEntity(string $entity): int
87
    {
88 14
        $this->checkEntityIsInConfig($entity);
89
90 12
        $entityConfig = $this->configEntites[$entity];
91
92 12
        $actions = 0;
93 12
        if (array_key_exists('delete', $entityConfig) && $entityConfig['delete'] === true) {
94 4
            $actions |= self::TRUNCATE_TABLE;
95
        }
96
97 12
        if (array_key_exists('cols', $entityConfig)) {
98 10
            $actions |= self::UPDATE_TABLE;
99
        }
100
101 12
        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 4
    public function getWhereConditionInConfig(string $entity): string
112
    {
113 4
        $this->checkEntityIsInConfig($entity);
114
115 4
        if (!array_key_exists('delete_where', $this->configEntites[$entity])) {
116 1
            return '';
117
        }
118
119 3
        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 8
    public function generateFakeData(string $entity): array
130
    {
131 8
        $this->checkEntityIsInConfig($entity);
132
133 8
        $faker = \Faker\Factory::create();
134
135 8
        $entityCols = $this->configEntites[$entity]['cols'];
136 8
        $entity = [];
137 8
        foreach ($entityCols as $colName => $colProps) {
138 8
            $args = empty($colProps['params']) ? [] : $colProps['params'];
139 8
            $data = call_user_func_array([$faker, $colProps['method']], $args);
140 8
            $entity[$colName] = $data;
141
        }
142
143 8
        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 14
    private function checkEntityIsInConfig(string $entity)
154
    {
155 14
        if (empty($this->configEntites)) {
156 1
            throw new NeuralizerConfigurationException('No entities found. Have you loaded a configuration file ?');
157
        }
158 13
        if (!array_key_exists($entity, $this->configEntites)) {
159 1
            throw new NeuralizerConfigurationException("No configuration for that entity ($entity)");
160
        }
161 12
    }
162
}
163