Completed
Push — 4.x ( da046c...d9a6cf )
by Kit Loong
08:01
created

MySQLRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnumPresetValues() 0 15 2
A getSetPresetValues() 0 16 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/04/07
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Repositories;
9
10
use Illuminate\Support\Facades\DB;
11
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
12
13
class MySQLRepository
14
{
15
    public function getEnumPresetValues(string $table, string $columnName): ?string
16
    {
17
        /** @var MigrationGeneratorSetting $setting */
18
        $setting = app(MigrationGeneratorSetting::class);
19
20
        $column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'");
21
        if (count($column) > 0) {
22
            return substr(
23
                str_replace('enum(', '[', $column[0]->Type),
24
                0,
25
                -1
26
            ).']';
27
        }
28
        return null;
29
    }
30
31
    public function getSetPresetValues(string $table, string $columnName): ?string
32
    {
33
        /** @var MigrationGeneratorSetting $setting */
34
        $setting = app(MigrationGeneratorSetting::class);
35
36
        $column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'");
37
        if (count($column) > 0) {
38
            return substr(
39
                str_replace('set(', '[', $column[0]->Type),
40
                0,
41
                -1
42
            ).']';
43
        }
44
45
        return null;
46
    }
47
}
48