DmsFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDmsTable() 0 21 2
A createDmsDatabase() 0 10 2
A createDmsColumn() 0 9 1
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\Generator;
4
5
use Janisbiz\LightOrm\Connection\ConnectionInterface;
6
use Janisbiz\LightOrm\Generator\AbstractDmsFactory;
7
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsColumn;
8
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsDatabase;
9
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsTable;
10
use Janisbiz\LightOrm\Generator\Dms\DmsColumnInterface;
11
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface;
12
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface;
13
14
class DmsFactory extends AbstractDmsFactory
15
{
16
    /**
17
     * @param string $databaseName
18
     * @param ConnectionInterface $connection
19
     *
20
     * @return DmsDatabase
21
     */
22
    public function createDmsDatabase(string $databaseName, ConnectionInterface $connection): DmsDatabaseInterface
23
    {
24
        $tablesInDatabase = $connection->query('SHOW TABLES', \PDO::FETCH_CLASS, \stdClass::class);
0 ignored issues
show
Bug introduced by
The method query() does not exist on Janisbiz\LightOrm\Connection\ConnectionInterface. It seems like you code against a sub-type of Janisbiz\LightOrm\Connection\ConnectionInterface such as Janisbiz\LightOrm\Dms\MySQL\Connection\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $tablesInDatabase = $connection->query('SHOW TABLES', \PDO::FETCH_CLASS, \stdClass::class);
Loading history...
25
26
        $tables = [];
27
        foreach ($tablesInDatabase as $tableInDatabase) {
28
            $tables[] = $this->createDmsTable($tableInDatabase->{\sprintf('Tables_in_%s', $databaseName)}, $connection);
29
        }
30
31
        return new DmsDatabase($databaseName, $tables);
32
    }
33
34
    /**
35
     * @param string $tableName
36
     * @param ConnectionInterface $connection
37
     *
38
     * @return DmsTable
39
     */
40
    protected function createDmsTable(string $tableName, ConnectionInterface $connection): DmsTableInterface
41
    {
42
        $columnsInTable = $connection->query(
43
            \sprintf('SHOW COLUMNS FROM %s', $tableName),
44
            \PDO::FETCH_CLASS,
45
            \stdClass::class
46
        );
47
48
        $columns = [];
49
        foreach ($columnsInTable as $columnInTable) {
50
            $columns[] = $this->createDmsColumn(
51
                $columnInTable->Field,
52
                $columnInTable->Type,
53
                $columnInTable->Null === 'YES',
54
                $columnInTable->Key,
55
                (string) $columnInTable->Default,
56
                $columnInTable->Extra
57
            );
58
        }
59
60
        return new DmsTable($tableName, $columns);
61
    }
62
63
    /**
64
     * @param string $name
65
     * @param string $type
66
     * @param bool $nullable
67
     * @param string $key
68
     * @param string $default
69
     * @param null|string $extra
70
     *
71
     * @return DmsColumn
72
     */
73
    protected function createDmsColumn(
74
        string $name,
75
        string $type,
76
        bool $nullable,
77
        string $key,
78
        string $default,
79
        ?string $extra
80
    ): DmsColumnInterface {
81
        return new DmsColumn($name, $type, $nullable, $key, $default, $extra);
82
    }
83
}
84