Test Failed
Pull Request — master (#3)
by
unknown
05:32 queued 02:06
created

AnnotationConfigFactory::getConfig()   C

Complexity

Conditions 12
Paths 27

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 33
c 3
b 0
f 0
nc 27
nop 1
dl 0
loc 56
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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