Completed
Push — master ( 189392...3a5307 )
by Olivier
03:25
created

Loader::getTables()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 23
cts 23
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 18
nop 2
crap 12

How to fix   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
declare(strict_types=1);
4
5
namespace Shapin\Datagen\DBAL;
6
7
use Doctrine\DBAL\Schema\Schema;
8
9
class Loader
10
{
11
    private $tables = [];
12
    private $groups = [];
13
14 7
    public function addTable(Table $table, array $groups = []): void
15
    {
16 7
        $this->tables[$table->getTableName()] = $table;
17
18 7
        foreach ($groups as $group) {
19 7
            if (!isset($this->groups[$group])) {
20 7
                $this->groups[$group] = [];
21
            }
22 7
            $this->groups[$group][] = $table->getTableName();
23
        }
24 7
    }
25
26 8
    public function getSchema(array $groups = [], array $excludeGroups = []): Schema
27
    {
28 8
        $schema = new Schema();
29
30 8
        foreach ($this->getTables($groups, $excludeGroups) as $table) {
31 3
            $table->addTableToSchema($schema);
32
        }
33
34 6
        return $schema;
35
    }
36
37 3
    public function getFixtures(array $groups = [], array $excludeGroups = []): iterable
38
    {
39 3
        foreach ($this->getTables($groups, $excludeGroups) as $table) {
40 1
            $tableName = $table->getTableName();
41 1
            $types = $table->getTypes();
42
43 1
            foreach ($table->getRows() as $row) {
44 1
                yield [$tableName, $row, $types];
45
            }
46
        }
47 3
    }
48
49 10
    private function getTables(array $groups, array $excludeGroups): array
50
    {
51 10
        $duplicatedGroups = array_intersect($groups, $excludeGroups);
52 10
        if (0 < count($duplicatedGroups)) {
53 1
            throw new \InvalidArgumentException(sprintf('You can\'t both select & ignore a given group. Errored: ["%s"]', implode('", "', $duplicatedGroups)));
54
        }
55
56
        // Check that all groups exists.
57 9
        foreach ($groups + $excludeGroups as $group) {
58 4
            if (!isset($this->groups[$group])) {
59 1
                throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $group, implode('", "', array_keys($this->groups))));
60
            }
61
        }
62
63
        // Select all relevant tables according to asked groups
64 8
        if (0 === count($groups)) {
65 7
            $tables = $this->tables;
66
        } else {
67 1
            $tables = [];
68 1
            foreach ($groups as $group) {
69 1
                foreach ($this->groups[$group] as $tableInGroup) {
70 1
                    $tables[$tableInGroup] = $this->tables[$tableInGroup];
71
                }
72
            }
73
        }
74
75
        // Remove all tables to exclude
76 8
        foreach ($excludeGroups as $excludeGroup) {
77 2
            foreach ($this->groups[$excludeGroup] as $tableToExclude) {
78 2
                if (array_key_exists($tableToExclude, $tables)) {
79 2
                    unset($tables[$tableToExclude]);
80
                }
81
            }
82
        }
83
84
        // Order all tables
85 8
        usort($tables, function ($a, $b) {
86 4
            if ($a->getOrder() === $b->getOrder()) {
87 3
                return 0;
88
            }
89
90 4
            return $a->getOrder() < $b->getOrder() ? -1 : 1;
91 8
        });
92
93 8
        return $tables;
94
    }
95
}
96