Completed
Push — master ( ed8648...7b08a2 )
by Supun
02:30
created

Client::loadText()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 1
dl 0
loc 10
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
    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
    /**
44
     * @param String $text
45
     * @return bool
46
     */
47
    public function loadText(String $text)
48
    {
49
        try {
50
            $this->file_content = $text;
51
52
            return true;
53
        } catch (\Exception $e) {
54
            $this->error = $e->getMessage();
55
56
            return false;
57
        }
58
    }
59
60
    /**
61
     * @param $file_name
62
     *
63
     * @return bool
64
     */
65
    public function loadFile($file_name)
66
    {
67
        try {
68
            $this->file_content = file_get_contents($file_name);
69
70
            return true;
71
        } catch (\Exception $e) {
72
            $this->error = $e->getMessage();
73
74
            return false;
75
        }
76
    }
77
78
79
    /**
80
     * @return bool
81
     */
82
    public function migrate()
83
    {
84
        $JsonExtractor = new JsonExtractor(new Json($this->file_content));
85
        $JsonExtractor->toMysqlTables();
86
        $this->createTables($JsonExtractor);
87
        return $this->insertData($JsonExtractor);
88
    }
89
90
    /**
91
     * @param JsonExtractor $JsonExtractor
92
     */
93
    private function createTables(JsonExtractor $JsonExtractor)
94
    {
95
        foreach ($JsonExtractor->getTablesArray() as $TableName => $TableColumn) {
96
            $this->capsule::schema()->dropIfExists($TableName);
97
            $this->capsule::schema()->create($TableName, function ($table) use ($TableColumn) {
98
                foreach ($TableColumn as $column_item) {
99
                    switch ($column_item['type']) {
100
                        case 'int':
101
                            $table->increments($column_item['name']);
102
                            break;
103
                        case 'integer':
104
                            $table->decimal($column_item['name'], 65)->nullable();
105
                            break;
106
                        case 'boolean':
107
                            $table->boolean($column_item['name'])->nullable();
108
                            break;
109
                        case 'double':
110
                            $table->double($column_item['name'])->nullable();
111
                            break;
112
                        default:
113
                            $table->text($column_item['name'])->nullable();
114
                            break;
115
116
                    }
117
                }
118
            });
119
        }
120
    }
121
122
    /**
123
     * @param JsonExtractor $JsonExtractor
124
     * @return bool
125
     */
126
    private function insertData(JsonExtractor $JsonExtractor)
127
    {
128
        foreach ($JsonExtractor->getDataArray() as $TableName => $TableData) {
129
            $this->capsule::table($TableName)->insert($TableData);
130
        }
131
        return true;
132
    }
133
134
    public function clearDatabase()
135
    {
136
    }
137
}
138