Completed
Push — 4.x ( 61b761...7a7a18 )
by Kit Loong
05:10
created

IntegerField::checkIsMySQLBoolean()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 3
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 * Time: 12:50
7
 */
8
9
namespace KitLoong\MigrationsGenerator\Generators;
10
11
use Doctrine\DBAL\Schema\Column;
12
use Illuminate\Support\Collection;
13
use Illuminate\Support\Facades\DB;
14
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
15
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
16
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
17
18
class IntegerField
19
{
20
    public function makeField(string $tableName, array $field, Column $column, Collection $indexes): array
21
    {
22
        if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) {
23
            $field['type'] = FieldGenerator::$fieldTypeMap[$field['type']];
24
        }
25
26
        $isBoolean = $this->checkIsMySQLBoolean($tableName, $field, $column);
27
        if ($isBoolean) {
28
            return $this->handleBoolean($field, $column);
29
        } else {
30
            return $this->handleInteger($field, $column, $indexes);
31
        }
32
    }
33
34
    private function handleBoolean(array $field, Column $column): array
35
    {
36
        $field['type'] = ColumnType::BOOLEAN;
37
        if ($column->getUnsigned()) {
38
            $field['decorators'][] = ColumnModifier::UNSIGNED;
39
        }
40
41
        return $field;
42
    }
43
44
    private function handleInteger(array $field, Column $column, Collection $indexes): array
45
    {
46
        if ($column->getUnsigned() && $column->getAutoincrement()) {
47
            if ($field['type'] === 'integer') {
48
                $field['type'] = ColumnType::INCREMENTS;
49
            } else {
50
                $field['type'] = str_replace('Integer', 'Increments', $field['type']);
51
            }
52
53
            $indexes->forget($field['field']);
54
        } else {
55
            if ($column->getUnsigned()) {
56
                $field['type'] = 'unsigned'.ucfirst($field['type']);
57
            }
58
            if ($column->getAutoincrement()) {
59
                $field['args'][] = 'true';
60
                $indexes->forget($field['field']);
61
            }
62
        }
63
        return $field;
64
    }
65
66
    private function checkIsMySQLBoolean(string $tableName, array $field, Column $column): bool
67
    {
68
        /** @var MigrationGeneratorSetting $setting */
69
        $setting = app(MigrationGeneratorSetting::class);
70
71
        if ($setting->getPlatform() === Platform::MYSQL &&
72
            $field['type'] === ColumnType::TINY_INTEGER &&
73
            !$column->getAutoincrement()) {
74
            $column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'");
75
            return !empty($column);
76
        }
77
        return false;
78
    }
79
}
80