Completed
Push — master ( d278b5...81f2b1 )
by Guillaume
02:18
created

MysqlDatabaseFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 16 3
B getIndex() 0 16 5
B getColumns() 0 21 5
A enableCheckCollate() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace Starkerxp\DatabaseChecker\Factory;
4
5
use Starkerxp\DatabaseChecker\Repository\MysqlRepository;
6
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseTable;
7
8
9
/**
10
 * Transcris l'�tat de la base de donn�es en version objet afin de pouvoir y appliquer les traitements.
11
 *
12
 * @package Starkerxp\DatabaseChecker\Factory
13
 */
14
class MysqlDatabaseFactory
15
{
16
17
    protected $databaseName;
18
    protected $repositoryMysql;
19
    private $checkCollate = false;
20
21
    /**
22
     * MysqlDatabaseFactory constructor.
23
     *
24
     * @param MysqlRepository $repositoryMysql
25
     * @param string          $databaseName
26
     */
27
    public function __construct(MysqlRepository $repositoryMysql, $databaseName)
28
    {
29
        $this->repositoryMysql = $repositoryMysql;
30
        $this->databaseName = $databaseName;
31
    }
32
33
    public function enableCheckCollate()
34
    {
35
        $this->checkCollate = true;
36
    }
37
38
    /**
39
     * @return MysqlDatabaseTable[]
40
     *
41
     * @throws \LogicException
42
     */
43
    public function generate()
44
    {
45
        $export = [];
46
        $tables = $this->repositoryMysql->getTablesStructure($this->databaseName);
47
        foreach ($tables as $table) {
48
            $export[$table] = $this->getIndex($table);
49
            $export[$table]['columns'] = $this->getColumns($table);
50
        }
51
        $factoryJsonDatabase = new JsonDatabaseFactory(json_encode($export));
52
        try {
53
            $export = $factoryJsonDatabase->generate();
54
        } catch (\Exception $e) {
55
            throw new \LogicException('Un expected error with json.' . $e->getMessage());
56
        }
57
58
        return $export;
59
    }
60
61
62
    protected function getIndex($table)
63
    {
64
        if (!$results = $this->repositoryMysql->fetchIndexStructure($this->databaseName, $table)) {
65
            return [];
66
        }
67
        $export = [];
68
        foreach ($results as $row) {
69
            if ($row['INDEX_NAME'] === 'PRIMARY') {
70
                $export['primary'] = array_filter(explode(',', $row['COLUMN_NAME']));
71
                continue;
72
            }
73
            $key = !$row['NON_UNIQUE'] ? 'uniques' : 'indexes';
74
            $export[$key][] = array_filter(['name' => $row['INDEX_NAME'], 'columns' => explode(',', $row['COLUMN_NAME'])]);
75
        }
76
77
        return $export;
78
    }
79
80
    protected function getColumns($table)
81
    {
82
        $export = [];
83
        $results = $this->repositoryMysql->fetchColumnsStructure($this->databaseName, $table);
84
        foreach ($results as $row) {
85
            $type = $row['DATA_TYPE'];
86
            $length = str_replace([$type, '(', ')'], '', $row['COLUMN_TYPE']);
87
            if ($type === 'enum') {
88
                $type = $row['COLUMN_TYPE'];
89
                $length = null;
90
            }
91
            $export[$row['COLUMN_NAME']] = array_filter([
92
                'type' => $type,
93
                'length' => $length,
94
                'nullable' => $row['IS_NULLABLE'] !== 'NO',
95
                'defaultValue' => $row['IS_NULLABLE'] !== 'NO' && empty($row['COLUMN_DEFAULT']) ? 'NULL' : $row['COLUMN_DEFAULT'],
96
                'extra' => $row['EXTRA'],
97
            ]);
98
        }
99
100
        return $export;
101
    }
102
103
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
104