Completed
Push — master ( cc0e46...765134 )
by Guillaume
02:18
created

JsonDatabaseFactory::generate()   C

Complexity

Conditions 11
Paths 52

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 28
nc 52
nop 0

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
namespace Starkerxp\DatabaseChecker\Factory;
4
5
use Starkerxp\DatabaseChecker\Exception\TablenameHasNotDefinedException;
6
use Starkerxp\DatabaseChecker\LoggerTrait;
7
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseColumn;
8
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseTable;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Convert json data to array object.
13
 *
14
 * @package Starkerxp\DatabaseChecker\Factory
15
 */
16
class JsonDatabaseFactory
17
{
18
19
    use LoggerTrait;
20
    /**
21
     * @var string
22
     */
23
    private $json;
24
25
    /**
26
     * JsonDatabaseFactory constructor.
27
     *
28
     * @param string $json
29
     */
30
    public function __construct($json)
31
    {
32
        $this->json = $json;
33
    }
34
35
    /**
36
     * @return MysqlDatabaseTable[]
37
     *
38
     * @throws \RuntimeException
39
     * @throws TablenameHasNotDefinedException
40
     */
41
    public function generate()
42
    {
43
        $tables = [];
44
        try {
45
            $dataTables = $this->resolve();
46
        } catch (\Exception $e) {
47
            $this->error('An unexpected error are throw when you check json syntax');
48
49
            return [];
50
        }
51
        foreach ($dataTables as $tableName => $dataTable) {
52
            try {
53
                $table = new MysqlDatabaseTable($tableName);
54
            } catch (TablenameHasNotDefinedException $e) {
55
                throw $e;
56
            } catch (\Exception $e) {
57
                continue;
58
            }
59
            if (isset($row['collate'])) {
60
                $table->setCollate($dataTable['collate']);
61
            }
62
            foreach ((array)$dataTable['columns'] as $columnName => $row) {
63
                $column = new MysqlDatabaseColumn($columnName, $row['type'], $row['length'], $row['nullable'], $row['defaultValue'], $row['extra']);
64
                if (isset($row['collate'])) {
65
                    $column->setCollate($row['collate']);
66
                }
67
                $table->addColumn($column);
68
69
            }
70
            foreach ((array)$dataTable['indexes'] as $row) {
71
                $table->addIndex($row['columns'], $row['name']);
72
            }
73
            if (isset($dataTable['primary'])) {
74
                $table->addPrimary((array)$dataTable['primary']);
75
            }
76
            foreach ((array)$dataTable['uniques'] as $row) {
77
                $table->addUnique($row['columns'], $row['name']);
78
            }
79
            $tables[] = $table;
80
        }
81
82
        return $tables;
83
    }
84
85
    /**
86
     * Check syntax of array.
87
     *
88
     * @return array
89
     *
90
     * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
91
     * @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
92
     * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
93
     * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
94
     * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
95
     * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
96
     */
97
    protected function resolve()
98
    {
99
        $data = $this->generateJsonData();
100
101
        // On force les valeurs par d�faut.
102
        $resolverTable = new OptionsResolver();
103
        $resolverTable->setRequired(['columns']);
104
        $resolverTable->setDefaults(
105
            [
106
                'columns' => [],
107
                'indexes' => [],
108
                'primary' => null,
109
                'uniques' => [],
110
                'collate' => null,
111
            ]
112
        );
113
114
        $resolverColumns = new OptionsResolver();
115
        $resolverColumns->setRequired(['type']);
116
        $resolverColumns->setDefaults(
117
            [
118
                'length' => null,
119
                'nullable' => false,
120
                'defaultValue' => null,
121
                'extra' => null,
122
                'collate' => null,
123
            ]
124
        );
125
126
        $resolverIndex = new OptionsResolver();
127
        $resolverIndex->setDefaults(
128
            [
129
                'name' => '',
130
                'columns' => [],
131
            ]
132
        );
133
        $export = [];
134
        foreach ($data as $nomTable => $table) {
135
            $dataTable = $resolverTable->resolve($table);
136
            foreach ((array)$dataTable['columns'] as $columnName => $column) {
137
                $dataTable['columns'][$columnName] = $resolverColumns->resolve($column);
138
            }
139
            foreach (['indexes', 'uniques'] as $indexKey) {
140
                foreach ((array)$dataTable[$indexKey] as $keyIndex => $index) {
141
                    $dataTable[$indexKey][$keyIndex] = $resolverIndex->resolve($index);
142
                }
143
            }
144
            $export[$nomTable] = $dataTable;
145
        }
146
147
        return $export;
148
    }
149
150
    private function generateJsonData()
151
    {
152
        if (empty($this->json)) {
153
            return [];
154
        }
155
        if (!$data = json_decode($this->json, true)) {
156
            $data = [];
157
        }
158
159
        return $data;
160
    }
161
}
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...
162