Completed
Branch 4.x (7a7a18)
by Kit Loong
03:20
created

IntegerField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 60
ccs 32
cts 32
cp 1
rs 10
c 1
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleBoolean() 0 8 2
A makeField() 0 11 3
A checkIsMySQLBoolean() 0 12 4
A handleInteger() 0 20 6
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