Failed Conditions
Push — master ( 51f284...368556 )
by Jonathan
02:26
created

MigrationTableCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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;
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
use Doctrine\Migrations\Configuration\Configuration;
12
13
/**
14
 * @internal
15
 */
16
final class MigrationTableCreator
17
{
18
    private const MIGRATION_COLUMN_TYPE   = 'string';
19
    private const MIGRATION_COLUMN_LENGTH = 255;
20
21
    /** @var Configuration */
22
    private $configuration;
23
24
    /** @var AbstractSchemaManager */
25
    private $schemaManager;
26
27
    /** @var bool|null */
28
    private $migrationTableCreated;
29
30 74
    public function __construct(Configuration $configuration, AbstractSchemaManager $schemaManager)
31
    {
32 74
        $this->configuration = $configuration;
33 74
        $this->schemaManager = $schemaManager;
34 74
    }
35
36 68
    public function isMigrationTableCreated() : bool
37
    {
38 68
        if ($this->migrationTableCreated === null) {
39 68
            $this->configuration->connect();
40
41 68
            $migrationsTableName = $this->configuration->getMigrationsTableName();
42
43 68
            if ($this->schemaManager->tablesExist([$migrationsTableName])) {
44 6
                $this->migrationTableCreated = true;
45
            } else {
46 65
                $this->migrationTableCreated = false;
47
            }
48
        }
49
50 68
        return $this->migrationTableCreated;
51
    }
52
53 67
    public function createMigrationTable() : bool
54
    {
55 67
        $this->configuration->validate();
56
57 67
        if ($this->configuration->isDryRun()) {
58 1
            return false;
59
        }
60
61 67
        if ($this->isMigrationTableCreated()) {
62 54
            return false;
63
        }
64
65 65
        $migrationsTableName  = $this->configuration->getMigrationsTableName();
66 65
        $migrationsColumnName = $this->configuration->getMigrationsColumnName();
67
68
        $columns = [
69 65
            $migrationsColumnName => $this->getMigrationsColumn(),
70
        ];
71
72 65
        $table = new Table($migrationsTableName, $columns);
73 65
        $table->setPrimaryKey([$migrationsColumnName]);
74
75 65
        $this->schemaManager->createTable($table);
76
77 65
        $this->migrationTableCreated = true;
78
79 65
        return true;
80
    }
81
82 72
    public function getMigrationsColumn() : Column
83
    {
84 72
        return new Column(
85 72
            $this->configuration->getMigrationsColumnName(),
86 72
            Type::getType(self::MIGRATION_COLUMN_TYPE),
87 72
            ['length' => self::MIGRATION_COLUMN_LENGTH]
88
        );
89
    }
90
}
91