1 | <?php |
||
2 | |||
3 | namespace WebnetFr\DatabaseAnonymizerBundle\Config; |
||
4 | |||
5 | use Doctrine\Common\Annotations\Reader; |
||
6 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
7 | use WebnetFr\DatabaseAnonymizer\ConfigGuesser\ConfigGuesser; |
||
8 | use WebnetFr\DatabaseAnonymizer\Exception\GuesserMissingHintException; |
||
9 | use WebnetFr\DatabaseAnonymizerBundle\Annotation\Field as AnonymizerField; |
||
10 | use WebnetFr\DatabaseAnonymizerBundle\Annotation\Table as AnonymizerTable; |
||
11 | |||
12 | /** |
||
13 | * @author Vlad Riabchenko <[email protected]> |
||
14 | */ |
||
15 | class AnnotationConfigFactory |
||
16 | { |
||
17 | /** |
||
18 | * @var Reader |
||
19 | */ |
||
20 | private $annotationReader; |
||
21 | |||
22 | /** |
||
23 | * @var ConfigGuesser |
||
24 | */ |
||
25 | private $configGuesser; |
||
26 | |||
27 | /** |
||
28 | * @param Reader $annotationReader |
||
29 | * @param ConfigGuesser $configGuesser |
||
30 | */ |
||
31 | public function __construct(Reader $annotationReader, ConfigGuesser $configGuesser) |
||
32 | { |
||
33 | $this->annotationReader = $annotationReader; |
||
34 | $this->configGuesser = $configGuesser; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param ClassMetadata[] $allMetadata |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getConfig(array $allMetadata) |
||
42 | { |
||
43 | $config = []; |
||
44 | |||
45 | foreach ($allMetadata as $metadata) { |
||
46 | $reflClass = new \ReflectionClass($metadata->name); |
||
47 | $classAnnotation = $this->annotationReader->getClassAnnotation($reflClass, AnonymizerTable::class); |
||
48 | if (!$classAnnotation instanceof AnonymizerTable) { |
||
49 | continue; |
||
50 | } |
||
51 | |||
52 | $tableName = $metadata->table['name']; |
||
53 | $config[$tableName] = [ |
||
54 | 'primary_key' => $metadata->identifier, |
||
55 | 'fields' => [], |
||
56 | ]; |
||
57 | |||
58 | foreach ($metadata->fieldMappings as $fieldName => $fieldMapping) { |
||
59 | if (in_array($fieldName, $metadata->identifier)) { |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | $reflProperty = $reflClass->getProperty($fieldName); |
||
64 | |||
65 | $fieldAnnotation = $this->annotationReader->getPropertyAnnotation($reflProperty, AnonymizerField::class); |
||
66 | $fieldConfig = null; |
||
67 | if ($fieldAnnotation instanceof AnonymizerField) { |
||
68 | $fieldConfig = $fieldAnnotation->getConfig(); |
||
69 | } elseif ($classAnnotation->guess) { |
||
70 | try { |
||
71 | $fieldConfig = $this->configGuesser::guessColumn($fieldName)->getConfigArray(); |
||
72 | } catch (GuesserMissingHintException $e) { |
||
73 | try { |
||
74 | $fieldConfig = $this->configGuesser::guessColumn($fieldMapping['columnName'])->getConfigArray(); |
||
75 | } catch (GuesserMissingHintException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | if ($fieldConfig) { |
||
81 | $config[$tableName]['fields'][$fieldMapping['columnName']] = $fieldConfig; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if (empty($config[$tableName]['fields'])) { |
||
86 | unset($config[$tableName]); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return ['tables' => $config]; |
||
91 | } |
||
92 | } |
||
93 |