Completed
Push — master ( 03c10c...407786 )
by Supun
10s
created

Client::createTables()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 2
nop 1
dl 0
loc 28
rs 6.7272
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
    public $capsule;
15
    public $error = false;
16
    public $file_content = null;
17
    public $sql = null;
18
19
    /**
20
     * Client constructor.
21
     *
22
     * @param $config
23
     */
24
    public function __construct($config)
25
    {
26
        $this->capsule = new Capsule();
27
28
        $this->capsule->addConnection([
29
            'driver' => 'mysql',
30
            'host' => $config['host'],
31
            'database' => $config['db'],
32
            'username' => $config['username'],
33
            'password' => $config['password'],
34
            'charset' => 'utf8mb4',
35
            'collation' => 'utf8mb4_general_ci',
36
            'prefix' => '',
37
        ]);
38
39
        $this->capsule->setAsGlobal();
40
    }
41
42
    /**
43
     * @param $file_name
44
     *
45
     * @return bool
46
     */
47
    public function loadFile($file_name)
48
    {
49
        try {
50
            $this->file_content = file_get_contents($file_name);
51
52
            return true;
53
        } catch (\Exception $e) {
54
            $this->error = $e->getMessage();
55
56
            return false;
57
        }
58
    }
59
60
61
    /**
62
     * @return bool
63
     */
64
    public function migrate()
65
    {
66
        $JsonExtractor = new JsonExtractor(new Json($this->file_content));
67
        $JsonExtractor->toMysqlTables();
68
        $this->createTables($JsonExtractor);
69
        return $this->insertData($JsonExtractor);
70
71
    }
72
73
    /**
74
     * @param JsonExtractor $JsonExtractor
75
     */
76
    private function createTables(JsonExtractor $JsonExtractor)
77
    {
78
79
80
        foreach ($JsonExtractor->getTablesArray() as $TableName => $TableColumn) {
81
82
            $this->capsule::schema()->dropIfExists($TableName);
83
            $this->capsule::schema()->create($TableName, function ($table) use ($TableColumn) {
84
85
86
                foreach ($TableColumn as $column_item) {
87
88
                    switch ($column_item['type']) {
89
                        case 'int':
90
                            $table->increments($column_item['name']);
91
                            break;
92
                        case 'integer':
93
                            $table->decimal($column_item['name'], 65)->nullable();
94
                            break;
95
                        case 'boolean':
96
                            $table->boolean($column_item['name'])->nullable();
97
                            break;
98
                        case 'double':
99
                            $table->double($column_item['name'])->nullable();
100
                            break;
101
                        default :
102
                            $table->text($column_item['name'])->nullable();
103
                            break;
104
105
                    }
106
                }
107
108
109
            });
110
        }
111
    }
112
113
    /**
114
     * @param JsonExtractor $JsonExtractor
115
     * @return bool
116
     */
117
    private function insertData(JsonExtractor $JsonExtractor)
118
    {
119
        foreach ($JsonExtractor->getDataArray() as $TableName => $TableData) {
120
            $this->capsule::table($TableName)->insert($TableData);
121
        }
122
        return true;
123
    }
124
125
    public function clearDatabase()
126
    {
127
128
    }
129
}
130