Completed
Push — master ( cfda60...9a8bcf )
by Olivier
05:15
created

Loader::getTables()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 11.375

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 10
cts 16
cp 0.625
rs 8.1795
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 11.375
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 2
    public function addTable(Table $table, array $groups = []): void
15
    {
16 2
        $this->tables[$table->getTableName()] = $table;
17
18 2
        foreach ($groups as $group) {
19
            if (!isset($this->groups[$group])) {
20
                $this->groups[$group] = [];
21
            }
22
            $this->groups[$group][] = $table->getTableName();
23
        }
24 2
    }
25
26 2
    public function getSchema(array $groups = []): Schema
0 ignored issues
show
Unused Code introduced by
The parameter $groups is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    {
28 2
        $schema = new Schema();
29
30 2
        foreach ($this->getTables() as $table) {
31 1
            $table->addTableToSchema($schema);
32
        }
33
34 2
        return $schema;
35
    }
36
37 2
    public function getFixtures(array $groups = []): iterable
38
    {
39 2
        foreach ($this->getTables($groups) 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 2
    }
48
49 3
    private function getTables(array $groups = []): array
50
    {
51
        // Check that all groups exists.
52 3
        foreach ($groups as $groups) {
53
            if (!isset($this->groups[$group])) {
0 ignored issues
show
Bug introduced by
The variable $group seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
54
                throw new \InvalidArgumentException(sprintf('Unknown group %s. Available: [%s]', $group, implode(', ', array_keys($this->groups))));
55
            }
56
        }
57
58 3
        if (0 === count($groups)) {
59 3
            $tables = $this->tables;
60
        } else {
61
            $tables = [];
62
            foreach ($groups as $group) {
63
                foreach ($this->groups[$group] as $tableInGroup) {
64
                    $tables[] = $this->tables[$tableInGroup];
65
                }
66
            }
67
        }
68
69
        // Order all tables
70 3
        usort($tables, function ($a, $b) {
71 2
            if ($a->getOrder() === $b->getOrder()) {
72 2
                return 0;
73
            }
74
75 2
            return $a->getOrder() < $b->getOrder() ? -1 : 1;
76 3
        });
77
78 3
        return $tables;
79
    }
80
}
81