Completed
Push — feature/collation ( 3fa00f...cce0c2 )
by Kit Loong
04:22
created

DatetimeField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: liow.kitloong
5
 * Date: 2020/03/29
6
 */
7
8
namespace KitLoong\MigrationsGenerator\Generators;
9
10
use Doctrine\DBAL\Schema\Column;
11
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
12
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnName;
13
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;
14
use KitLoong\MigrationsGenerator\Repositories\MySQLRepository;
15
use KitLoong\MigrationsGenerator\Types\DBALTypes;
16
17
class DatetimeField
18
{
19
    private $decorator;
20
    private $mySQLRepository;
21
22
    public function __construct(Decorator $decorator, MySQLRepository $mySQLRepository)
23
    {
24
        $this->decorator = $decorator;
25
        $this->mySQLRepository = $mySQLRepository;
26
    }
27
28
    public function makeField(string $table, array $field, Column $column, bool $useTimestamps): array
29
    {
30
        if ($useTimestamps) {
31
            if ($field['field'] === ColumnName::CREATED_AT) {
32
                return [];
33
            } elseif ($field['field'] === ColumnName::UPDATED_AT) {
34
                $field['type'] = ColumnType::TIMESTAMPS;
35
                $field['field'] = null;
36
            }
37
        }
38
39
        if ($field['field'] === ColumnName::DELETED_AT && !$column->getNotnull()) {
40
            $field['type'] = ColumnType::SOFT_DELETES;
41
            $field['field'] = null;
42
        }
43
44
        if (isset(FieldGenerator::$fieldTypeMap[$field['type']])) {
45
            $field['type'] = FieldGenerator::$fieldTypeMap[$field['type']];
46
        }
47
48
        if ($column->getLength() !== null && $column->getLength() > 0) {
49
            if ($field['type'] === ColumnType::SOFT_DELETES) {
50
                $field['field'] = ColumnName::DELETED_AT;
51
            }
52
            $field['args'][] = $column->getLength();
53
        }
54
55
        if ($column->getType()->getName() === DBALTypes::TIMESTAMP) {
56
            if ($this->mySQLRepository->useOnUpdateCurrentTimestamp($table, $column->getName())) {
57
                $field['decorators'][] = ColumnModifier::USE_CURRENT_ON_UPDATE;
58
            }
59
        }
60
        return $field;
61
    }
62
63
    public function makeDefault(Column $column): string
64
    {
65
        if (in_array($column->getDefault(), ['CURRENT_TIMESTAMP'], true)) {
66
            return ColumnModifier::USE_CURRENT;
67
        } else {
68
            $default = $this->decorator->columnDefaultToString($column->getDefault());
0 ignored issues
show
Bug introduced by
It seems like $column->getDefault() can also be of type null; however, parameter $args of KitLoong\MigrationsGener...columnDefaultToString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            $default = $this->decorator->columnDefaultToString(/** @scrutinizer ignore-type */ $column->getDefault());
Loading history...
69
            return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]);
70
        }
71
    }
72
73
    /**
74
     * @param  Column[]  $columns
75
     * @return bool
76
     */
77
    public function isUseTimestamps($columns): bool
78
    {
79
        /** @var Column[] $timestampsColumns */
80
        $timestampsColumns = [];
81
        foreach ($columns as $column) {
82
            if ($column->getName() === ColumnName::CREATED_AT || $column->getName() === ColumnName::UPDATED_AT) {
83
                $timestampsColumns[] = $column;
84
            }
85
        }
86
87
        $useTimestamps = false;
88
89
        if (count($timestampsColumns) === 2) {
90
            $useTimestamps = true;
91
            foreach ($timestampsColumns as $timestamp) {
92
                if ($timestamp->getNotnull() || $timestamp->getDefault() !== null) {
93
                    $useTimestamps = false;
94
                }
95
            }
96
        }
97
        return $useTimestamps;
98
    }
99
}
100