Passed
Push — master ( 791672...96c1a3 )
by Webnet
01:40
created

TargetFactory::createTargets()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 11
nop 1
dl 0
loc 33
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizer\Config;
4
5
use Doctrine\DBAL\Connection;
6
use WebnetFr\DatabaseAnonymizer\Exception\UnknownPrimaryKeyException;
7
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\GeneratorFactoryInterface;
8
use WebnetFr\DatabaseAnonymizer\TargetField;
9
use WebnetFr\DatabaseAnonymizer\TargetTable;
10
11
/**
12
 * Creates targets based on configuration.
13
 *
14
 * @author Vlad Riabchenko <[email protected]>
15
 */
16
class TargetFactory
17
{
18
    /**
19
     * Creates generator based on configuration.
20
     *
21
     * @var GeneratorFactoryInterface
22
     */
23
    private $generatorFactory;
24
25
    /**
26
     * @var Connection
27
     */
28
    private $connection;
29
30
    /**
31
     * @param GeneratorFactoryInterface $generatorFactory
32
     */
33
    public function __construct(GeneratorFactoryInterface $generatorFactory)
34
    {
35
        $this->generatorFactory = $generatorFactory;
36
    }
37
38
    /**
39
     * Set connection
40
     *
41
     * @param Connection $connection
42
     *
43
     * @return $this
44
     */
45
    public function setConnection(Connection $connection)
46
    {
47
        $this->connection = $connection;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Given configuration returns an array of @see TargetTable. 
54
     * 
55
     * @param array $config
56
     * [
57
     *     <table name> => [
58
     *         'primary_key' => <name of primary key field>,
59
     *         'fields' => [
60
     *             <field name> => <field config>,
61
     *             ...
62
     *         ]
63
     *     ],
64
     *     ...
65
     * ]
66
     *
67
     * @return TargetTable[]
68
     */
69
    public function createTargets(array $config): array
70
    {
71
        $targetTables = [];
72
73
        foreach ($config['tables'] as $tableName => $tableConfig) {
74
            $targetFields = [];
75
76
            foreach ($tableConfig['fields'] as $fieldName => $fieldConfig) {
77
                $generator = $this->generatorFactory->getGenerator($fieldConfig);
78
                $targetFields[] = new TargetField($fieldName, $generator);
79
            }
80
81
            $primaryKey = $tableConfig['primary_key'] ?? null;
82
            if (!$primaryKey) {
83
                if (!$this->connection) {
84
                    throw new UnknownPrimaryKeyException(sprintf("You must eigher set 'primary_key' on '%s' table or provide %s with Doctrine\DBAL\Connection instance via 'setConnection' method.", $tableName, self::class));
85
                }
86
                $schemaManager = $this->connection->getSchemaManager();
87
                $indexes = $schemaManager->listTableIndexes($tableName);
88
                foreach ($indexes as $index) {
89
                    /** @var $index \Doctrine\DBAL\Schema\Index */
90
                    if ($index->isPrimary()) {
91
                        $primaryKey = $index->getColumns();
92
93
                        break;
94
                    }
95
                }
96
            }
97
98
            $targetTables[] = new TargetTable($tableName, $primaryKey, $targetFields);
99
        }
100
101
        return $targetTables;
102
    }
103
}
104