Passed
Branch 4.x (7a7a18)
by Kit Loong
64:42
created

IntegerField::handleInteger()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 3
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
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 12
    public function makeField(string $tableName, array $field, Column $column, Collection $indexes): array
21
    {
22 12
        if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) {
23 9
            $field['type'] = FieldGenerator::$fieldTypeMap[$field['type']];
24
        }
25
26 12
        $isBoolean = $this->checkIsMySQLBoolean($tableName, $field, $column);
27 12
        if ($isBoolean) {
28 3
            return $this->handleBoolean($field, $column);
29
        } else {
30 9
            return $this->handleInteger($field, $column, $indexes);
31
        }
32
    }
33
34 3
    private function handleBoolean(array $field, Column $column): array
35
    {
36 3
        $field['type'] = ColumnType::BOOLEAN;
37 3
        if ($column->getUnsigned()) {
38 3
            $field['decorators'][] = ColumnModifier::UNSIGNED;
39
        }
40
41 3
        return $field;
42
    }
43
44 9
    private function handleInteger(array $field, Column $column, Collection $indexes): array
45
    {
46 9
        if ($column->getUnsigned() && $column->getAutoincrement()) {
47 3
            if ($field['type'] === 'integer') {
48 3
                $field['type'] = ColumnType::INCREMENTS;
49
            } else {
50 3
                $field['type'] = str_replace('Integer', 'Increments', $field['type']);
51
            }
52
53 3
            $indexes->forget($field['field']);
54
        } else {
55 6
            if ($column->getUnsigned()) {
56 3
                $field['type'] = 'unsigned'.ucfirst($field['type']);
57
            }
58 6
            if ($column->getAutoincrement()) {
59 3
                $field['args'][] = 'true';
60 3
                $indexes->forget($field['field']);
61
            }
62
        }
63 9
        return $field;
64
    }
65
66 12
    private function checkIsMySQLBoolean(string $tableName, array $field, Column $column): bool
67
    {
68
        /** @var MigrationGeneratorSetting $setting */
69 12
        $setting = app(MigrationGeneratorSetting::class);
70
71 12
        if ($setting->getPlatform() === Platform::MYSQL &&
72 9
            $field['type'] === ColumnType::TINY_INTEGER &&
73 9
            !$column->getAutoincrement()) {
74 3
            $column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'");
75 3
            return !empty($column);
76
        }
77 9
        return false;
78
    }
79
}
80