Passed
Push — master ( f42972...200c28 )
by Supun
02:29
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Lifeeka\JSQL;
4
5
use Lifeeka\JSQL\Extractor\JsonExtractor;
6
use Lifeeka\JSQL\Helpers\Json;
7
use Illuminate\Database\Capsule\Manager as Capsule;
8
9
/**
10
 * Class Client.
11
 */
12
class Client
13
{
14
    const TABLE_ONLY = "table_only";
15
    const DATA_ONLY = "data_only";
16
    const ALL = "all";
17
18
    public $capsule;
19
    public $error = false;
20
    public $file_content = null;
21
    public $sql = null;
22
23
24
    /**
25
     * Client constructor.
26
     *
27
     * @param $config
28
     */
29
    public function __construct($config)
30
    {
31
        $this->capsule = new Capsule();
32
33
        $this->capsule->addConnection([
34
            'driver' => 'mysql',
35
            'host' => $config['host'],
36
            'database' => $config['db'],
37
            'username' => $config['username'],
38
            'password' => $config['password'],
39
            'charset' => 'utf8mb4',
40
            'collation' => 'utf8mb4_general_ci',
41
            'prefix' => '',
42
            'strict' => false
43
        ]);
44
45
        $this->capsule->setAsGlobal();
46
    }
47
48
49
    /**
50
     * @param String $text
51
     * @return bool
52
     */
53
    public function loadText(String $text)
54
    {
55
        try {
56
            $this->file_content = $text;
57
            return true;
58
        } catch (\Exception $e) {
59
            $this->error = $e->getMessage();
60
61
            return false;
62
        }
63
    }
64
65
    /**
66
     * @param $file_name
67
     *
68
     * @return bool
69
     */
70
    public function loadFile($file_name)
71
    {
72
        try {
73
            $this->file_content = file_get_contents($file_name);
74
75
            return true;
76
        } catch (\Exception $e) {
77
            $this->error = $e->getMessage();
78
79
            return false;
80
        }
81
    }
82
83
84
    /**
85
     * @param string $type
86
     * @return bool
87
     */
88
    public function migrate($type = Client::ALL)
89
    {
90
        $JsonExtractor = new JsonExtractor(new Json($this->file_content));
91
        $JsonExtractor->toMysqlTables();
92
93
        switch ($type) {
94
            case Client::DATA_ONLY:
95
                $JsonExtractor->toMysqlData();
96
                $this->insertData($JsonExtractor);
97
                break;
98
            case Client::TABLE_ONLY:
99
                $this->createTables($JsonExtractor);
100
                break;
101
            default:
102
                $JsonExtractor->toMysqlData();
103
                $this->createTables($JsonExtractor);
104
                $this->insertData($JsonExtractor);
105
                break;
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * @param JsonExtractor $JsonExtractor
113
     */
114
    private function createTables(JsonExtractor $JsonExtractor)
115
    {
116
        $this->capsule::schema()->disableForeignKeyConstraints();
117
118
        //create tables
119
        foreach ($JsonExtractor->getTablesArray() as $TableName => $TableColumn) {
120
            $this->capsule::schema()->dropIfExists($TableName);
121
            $this->capsule::schema()->create($TableName, function ($table) use ($TableColumn) {
122
                foreach ($TableColumn as $column_item) {
123
                    switch ($column_item['type']) {
124
                        case 'int':
125
                            $table->int($column_item['name']);
126
                            break;
127
                        case 'primary_key':
128
                            $table->increments($column_item['name']);
129
                            break;
130
                        case 'integer':
131
                            $table->decimal($column_item['name'], 65)->nullable();
132
                            break;
133
                        case 'boolean':
134
                            $table->boolean($column_item['name'])->nullable();
135
                            break;
136
                        case 'double':
137
                            $table->double($column_item['name'])->nullable();
138
                            break;
139
                        case 'foreign_key':
140
                            $table->integer($column_item['name'])->unsigned();
141
                            $table->foreign($column_item['name'])->references('id')->on($column_item['ref']);
142
                            break;
143
                        default:
144
                            $table->text($column_item['name'])->nullable();
145
                            break;
146
147
                    }
148
                }
149
            });
150
        }
151
152
        $this->capsule::schema()->enableForeignKeyConstraints();
153
154
    }
155
156
    /**
157
     * @param JsonExtractor $JsonExtractor
158
     * @return bool
159
     */
160
    private function insertData(JsonExtractor $JsonExtractor)
161
    {
162
        $this->capsule::schema()->disableForeignKeyConstraints();
163
        foreach ($JsonExtractor->getDataArray() as $TableName => $TableData) {
164
            $this->capsule::table($TableName)->insert($TableData);
165
        }
166
        $this->capsule::schema()->enableForeignKeyConstraints();
167
168
        return true;
169
    }
170
171
    public function clearDatabase()
172
    {
173
    }
174
}
175