Issues (9)

src/Config/AnnotationConfigFactory.php (1 issue)

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
            if ($classAnnotation->truncate) {
59
                $config[$tableName]['truncate'] = true;
60
                continue;
61
            }
62
63
            foreach ($metadata->fieldMappings as $fieldName => $fieldMapping) {
64
                if (in_array($fieldName, $metadata->identifier)) {
65
                    continue;
66
                }
67
68
                $reflProperty = $reflClass->getProperty($fieldName);
69
70
                $fieldAnnotation = $this->annotationReader->getPropertyAnnotation($reflProperty, AnonymizerField::class);
71
                $fieldConfig = null;
72
                if ($fieldAnnotation instanceof AnonymizerField) {
73
                    $fieldConfig = $fieldAnnotation->getConfig();
74
                } elseif ($classAnnotation->guess) {
75
                    try {
76
                        $fieldConfig = $this->configGuesser::guessColumn($fieldName)->getConfigArray();
77
                    } catch (GuesserMissingHintException $e) {
78
                        try {
79
                            $fieldConfig = $this->configGuesser::guessColumn($fieldMapping['columnName'])->getConfigArray();
80
                        } catch (GuesserMissingHintException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
                        }
82
                    }
83
                }
84
85
                if ($fieldConfig) {
86
                    $config[$tableName]['fields'][$fieldMapping['columnName']] = $fieldConfig;
87
                }
88
            }
89
90
            if (empty($config[$tableName]['fields'])) {
91
                unset($config[$tableName]);
92
            }
93
        }
94
95
        return ['tables' => $config];
96
    }
97
}
98