Completed
Push — master ( ffefc4...9322fd )
by Emmanuel
04:19
created

AbstractAnonymizer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 126
ccs 33
cts 33
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
processEntity() 0 1 ?
A setConfiguration() 0 6 1
A whatToDoWithEntity() 0 11 3
A getWhereConditionInConfig() 0 10 2
A generateFakeData() 0 16 3
A checkEntityIsInConfig() 0 9 3
1
<?php
2
/**
3
 * Inet Data Anonymization
4
 *
5
 * PHP Version 5.3 -> 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2005-2015 iNet Process
10
 *
11
 * @package inetprocess/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link http://www.inetprocess.com
16
 */
17
18
namespace Inet\Neuralyzer\Anonymizer;
19
20
use Inet\Neuralyzer\Configuration\Reader;
21
use Inet\Neuralyzer\Exception\InetAnonConfigurationException;
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 = array();
51
52
    /**
53
     * Process the entity according to the anonymizer type
54
     *
55
     * @param string        $entity
56
     * @param callable|null $callback
57
     * @param bool          $pretend
58
     * @param bool          $returnResult
59
     */
60
    abstract public function processEntity($entity, $callback = null, $pretend = true, $returnResult = false);
61
62
    /**
63
     * Set the configuration
64
     *
65
     * @param Reader $configuration
66
     */
67 12
    public function setConfiguration(Reader $configuration)
68
    {
69 12
        $this->configuration = $configuration;
70 12
        $configEntites = $configuration->getConfigValues();
71 12
        $this->configEntites = $configEntites['entities'];
72 12
    }
73
74
    /**
75
     * Evaluate, from the configuration if I have to update or Truncate the table
76
     *
77
     * @param string $entity
78
     *
79
     * @return int
80
     */
81 11
    public function whatToDoWithEntity($entity)
82
    {
83 11
        $this->checkEntityIsInConfig($entity);
84
85 9
        $entityConfig = $this->configEntites[$entity];
86 9
        if (array_key_exists('empty', $entityConfig) && $entityConfig['empty'] === true) {
87 4
            return self::TRUNCATE_TABLE;
88
        }
89
90 5
        return self::UPDATE_TABLE;
91
    }
92
93
    /**
94
     * Returns the 'where' parameter for an entity in config (or empty)
95
     *
96
     * @param string $entity
97
     *
98
     * @return string
99
     */
100 4
    public function getWhereConditionInConfig($entity)
101
    {
102 4
        $this->checkEntityIsInConfig($entity);
103
104 4
        if (!array_key_exists('where', $this->configEntites[$entity])) {
105 1
            return '';
106
        }
107
108 3
        return $this->configEntites[$entity]['where'];
109
    }
110
111
    /**
112
     * Generate fake data for an entity and return it as an Array
113
     *
114
     * @param string $entity
115
     *
116
     * @return array
117
     */
118 5
    public function generateFakeData($entity)
119
    {
120 5
        $this->checkEntityIsInConfig($entity);
121
122 5
        $faker = \Faker\Factory::create();
123
124 5
        $entityCols = $this->configEntites[$entity]['cols'];
125 5
        $entity = array();
126 5
        foreach ($entityCols as $colName => $colProps) {
127 5
            $args = empty($colProps['params']) ? array() : $colProps['params'];
128 5
            $data = call_user_func_array(array($faker, $colProps['method']), $args);
129 5
            $entity[$colName] = $data;
130 5
        }
131
132 5
        return $entity;
133
    }
134
135
    /**
136
     * Make sure that entity is defined in the configuration
137
     *
138
     * @param string $entity
139
     *
140
     * @throws InetAnonConfigurationException
141
     */
142 11
    private function checkEntityIsInConfig($entity)
143
    {
144 11
        if (empty($this->configEntites)) {
145 1
            throw new InetAnonConfigurationException('No entities found. Have you loaded a configuration file ?');
146
        }
147 10
        if (!array_key_exists($entity, $this->configEntites)) {
148 1
            throw new InetAnonConfigurationException("No configuration for that entity ($entity)");
149
        }
150 9
    }
151
}
152