forked from webnet-fr/database-anonymizer-bundle
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAnnotationConfigFactory.php
More file actions
98 lines (83 loc) · 3.19 KB
/
AnnotationConfigFactory.php
File metadata and controls
98 lines (83 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace WebnetFr\DatabaseAnonymizerBundle\Config;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping\ClassMetadata;
use WebnetFr\DatabaseAnonymizer\ConfigGuesser\ConfigGuesser;
use WebnetFr\DatabaseAnonymizer\Exception\GuesserMissingHintException;
use WebnetFr\DatabaseAnonymizerBundle\Annotation\Field as AnonymizerField;
use WebnetFr\DatabaseAnonymizerBundle\Annotation\Table as AnonymizerTable;
/**
* @author Vlad Riabchenko <vriabchenko@webnet.fr>
*/
class AnnotationConfigFactory
{
/**
* @var Reader
*/
private $annotationReader;
/**
* @var ConfigGuesser
*/
private $configGuesser;
/**
* @param Reader $annotationReader
* @param ConfigGuesser $configGuesser
*/
public function __construct(Reader $annotationReader, ConfigGuesser $configGuesser)
{
$this->annotationReader = $annotationReader;
$this->configGuesser = $configGuesser;
}
/**
* @param ClassMetadata[] $allMetadata
* @return array
*/
public function getConfig(array $allMetadata)
{
$config = [];
foreach ($allMetadata as $metadata) {
$reflClass = new \ReflectionClass($metadata->name);
$classAnnotation = $this->annotationReader->getClassAnnotation($reflClass, AnonymizerTable::class);
if (!$classAnnotation instanceof AnonymizerTable) {
continue;
}
$tableName = $metadata->table['name'];
$config[$tableName] = [
'primary_key' => [],
'fields' => [],
];
if ($classAnnotation->truncate) {
$config[$tableName]['truncate'] = true;
continue;
}
foreach ($metadata->fieldMappings as $fieldName => $fieldMapping) {
if (in_array($fieldName, $metadata->identifier)) {
$config[$tableName]['primary_key'][] = $fieldMapping['columnName'];
continue;
}
$reflProperty = $reflClass->getProperty($fieldName);
$fieldAnnotation = $this->annotationReader->getPropertyAnnotation($reflProperty, AnonymizerField::class);
$fieldConfig = null;
if ($fieldAnnotation instanceof AnonymizerField) {
$fieldConfig = $fieldAnnotation->getConfig();
} elseif ($classAnnotation->guess) {
try {
$fieldConfig = $this->configGuesser::guessColumn($fieldName)->getConfigArray();
} catch (GuesserMissingHintException $e) {
try {
$fieldConfig = $this->configGuesser::guessColumn($fieldMapping['columnName'])->getConfigArray();
} catch (GuesserMissingHintException $e) {
}
}
}
if ($fieldConfig) {
$config[$tableName]['fields'][$fieldMapping['columnName']] = $fieldConfig;
}
}
if (empty($config[$tableName]['fields'])) {
unset($config[$tableName]);
}
}
return ['tables' => $config];
}
}