Failed Conditions
Pull Request — master (#707)
by Jonathan
03:12
created

TableDefinition::getColumnLength()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tracking;
6
7
use Doctrine\DBAL\Schema\AbstractSchemaManager;
8
use Doctrine\DBAL\Schema\Column;
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Types\Type;
11
12
class TableDefinition
13
{
14
    public const MIGRATION_COLUMN_TYPE             = 'string';
15
    public const MIGRATION_EXECUTED_AT_COLUMN_TYPE = 'datetime_immutable';
16
17
    /** @var AbstractSchemaManager */
18
    private $schemaManager;
19
20
    /** @var string */
21
    private $name;
22
23
    /** @var string */
24
    private $columnName;
25
26
    /** @var int */
27
    private $columnLength;
28
29
    /** @var string */
30
    private $executedAtColumnName;
31
32 92
    public function __construct(
33
        AbstractSchemaManager $schemaManager,
34
        string $name,
35
        string $columnName,
36
        int $columnLength,
37
        string $executedAtColumnName
38
    ) {
39 92
        $this->schemaManager        = $schemaManager;
40 92
        $this->name                 = $name;
41 92
        $this->columnName           = $columnName;
42 92
        $this->columnLength         = $columnLength;
43 92
        $this->executedAtColumnName = $executedAtColumnName;
44 92
    }
45
46 79
    public function getName() : string
47
    {
48 79
        return $this->name;
49
    }
50
51 79
    public function getColumnName() : string
52
    {
53 79
        return $this->columnName;
54
    }
55
56 1
    public function getColumnLength() : int
57
    {
58 1
        return $this->columnLength;
59
    }
60
61 1
    public function getExecutedAtColumnName() : string
62
    {
63 1
        return $this->executedAtColumnName;
64
    }
65
66 85
    public function getMigrationsColumn() : Column
67
    {
68 85
        return new Column(
69 85
            $this->columnName,
70 85
            Type::getType(self::MIGRATION_COLUMN_TYPE),
71 85
            ['length' => $this->columnLength]
72
        );
73
    }
74
75 85
    public function getExecutedAtColumn() : Column
76
    {
77 85
        return new Column(
78 85
            $this->executedAtColumnName,
79 85
            Type::getType(self::MIGRATION_EXECUTED_AT_COLUMN_TYPE)
80
        );
81
    }
82
83
    /**
84
     * @return string[]
85
     */
86 60
    public function getColumnNames() : array
87
    {
88
        return [
89 60
            $this->columnName,
90 60
            $this->executedAtColumnName,
91
        ];
92
    }
93
94 2
    public function getDBALTable() : Table
95
    {
96 2
        $executedAtColumn = $this->getExecutedAtColumn();
97 2
        $executedAtColumn->setNotnull(false);
98
99
        $columns = [
100 2
            $this->columnName           => $this->getMigrationsColumn(),
101 2
            $this->executedAtColumnName => $executedAtColumn,
102
        ];
103
104 2
        return $this->createDBALTable($columns);
105
    }
106
107 76
    public function getNewDBALTable() : Table
108
    {
109 76
        $executedAtColumn = $this->getExecutedAtColumn();
110 76
        $executedAtColumn->setNotnull(true);
111
112
        $columns = [
113 76
            $this->columnName           => $this->getMigrationsColumn(),
114 76
            $this->executedAtColumnName => $executedAtColumn,
115
        ];
116
117 76
        return $this->createDBALTable($columns);
118
    }
119
120
    /**
121
     * @param Column[] $columns
122
     */
123 78
    public function createDBALTable(array $columns) : Table
124
    {
125 78
        $schemaConfig = $this->schemaManager->createSchemaConfig();
126
127 78
        $table = new Table($this->getName(), $columns);
128 78
        $table->setPrimaryKey([$this->getColumnName()]);
129
130 78
        foreach ($schemaConfig->getDefaultTableOptions() as $name => $value) {
131 4
            $table->addOption($name, $value);
132
        }
133
134 78
        return $table;
135
    }
136
}
137