Completed
Push — master ( d1d2ef...0e04b4 )
by Kit Loong
04:05
created

MySQLRepository::spaceAfterComma()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
11
12
class MySQLRepository
13
{
14 6
    public function getEnumPresetValues(string $table, string $columnName): ?string
15
    {
16
        /** @var MigrationsGeneratorSetting $setting */
17 6
        $setting = app(MigrationsGeneratorSetting::class);
18
19 6
        $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'enum(%'");
20 6
        if (count($column) > 0) {
21 3
            return substr(
22 3
                str_replace('enum(', '[', $this->spaceAfterComma($column[0]->Type)),
23 3
                0,
24 3
                -1
25 3
            ).']';
26
        }
27 3
        return null;
28
    }
29
30 6
    public function getSetPresetValues(string $table, string $columnName): ?string
31
    {
32
        /** @var MigrationsGeneratorSetting $setting */
33 6
        $setting = app(MigrationsGeneratorSetting::class);
34
35 6
        $column = $setting->getConnection()->select("SHOW COLUMNS FROM `${table}` where Field = '${columnName}' AND Type LIKE 'set(%'");
36 6
        if (count($column) > 0) {
37 3
            return substr(
38 3
                str_replace('set(', '[', $this->spaceAfterComma($column[0]->Type)),
39 3
                0,
40 3
                -1
41 3
            ).']';
42
        }
43
44 3
        return null;
45
    }
46
47 6
    private function spaceAfterComma(string $value): string
48
    {
49 6
        return str_replace("','", "', '", $value);
50
    }
51
}
52