Failed Conditions
Pull Request — master (#725)
by Michael
02:19
created

Generator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
dl 0
loc 118
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B loadCustomTemplate() 0 23 6
A getTemplate() 0 11 3
B generateMigration() 0 29 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Generator;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Generator\Exception\InvalidTemplateSpecified;
9
use Doctrine\Migrations\Tools\Console\Helper\MigrationDirectoryHelper;
10
use function explode;
11
use function file_get_contents;
12
use function file_put_contents;
13
use function implode;
14
use function is_file;
15
use function is_readable;
16
use function preg_replace;
17
use function str_replace;
18
use function trim;
19
20
/**
21
 * The Generator class is responsible for generating a migration class.
22
 *
23
 * @internal
24
 */
25
class Generator
26
{
27
    private const MIGRATION_TEMPLATE = <<<'TEMPLATE'
28
<?php
29
30
declare(strict_types=1);
31
32
namespace <namespace>;
33
34
use Doctrine\DBAL\Schema\Schema;
35
use Doctrine\Migrations\AbstractMigration;
36
37
/**
38
 * Auto-generated Migration: Please modify to your needs!
39
 */
40
final class Version<version> extends AbstractMigration
41
{
42
    public function getDescription() : string
43
    {
44
        return '';
45
    }
46
47
    public function up(Schema $schema) : void
48
    {
49
        // this up() migration is auto-generated, please modify it to your needs
50
<up>
51
    }
52
53
    public function down(Schema $schema) : void
54
    {
55
        // this down() migration is auto-generated, please modify it to your needs
56
<down>
57
    }
58
}
59
60
TEMPLATE;
61
62
    /** @var Configuration */
63
    private $configuration;
64
65
    /** @var string|null */
66
    private $template;
67
68 12
    public function __construct(Configuration $configuration)
69
    {
70 12
        $this->configuration = $configuration;
71 12
    }
72
73 11
    public function generateMigration(
74
        string $version,
75
        ?string $up = null,
76
        ?string $down = null
77
    ) : string {
78
        $placeHolders = [
79 11
            '<namespace>',
80
            '<version>',
81
            '<up>',
82
            '<down>',
83
        ];
84
85
        $replacements = [
86 11
            $this->configuration->getMigrationsNamespace(),
87 11
            $version,
88 7
            $up !== null ? '        ' . implode("\n        ", explode("\n", $up)) : null,
89 7
            $down !== null ? '        ' . implode("\n        ", explode("\n", $down)) : null,
90
        ];
91
92 11
        $code = str_replace($placeHolders, $replacements, $this->getTemplate());
93 9
        $code = preg_replace('/^ +$/m', '', $code);
94
95 9
        $directoryHelper = new MigrationDirectoryHelper($this->configuration);
96 9
        $dir             = $directoryHelper->getMigrationDirectory();
97 9
        $path            = $dir . '/Version' . $version . '.php';
98
99 9
        file_put_contents($path, $code);
100
101 9
        return $path;
102
    }
103
104 11
    private function getTemplate() : string
105
    {
106 11
        if ($this->template === null) {
107 11
            $this->template = $this->loadCustomTemplate();
108
109 9
            if ($this->template === null) {
110 8
                $this->template = self::MIGRATION_TEMPLATE;
111
            }
112
        }
113
114 9
        return $this->template;
115
    }
116
117
    /**
118
     * @throws InvalidTemplateSpecified
119
     */
120 11
    private function loadCustomTemplate() : ?string
121
    {
122 11
        $customTemplate = $this->configuration->getCustomTemplate();
123
124 11
        if ($customTemplate === null) {
125 8
            return null;
126
        }
127
128 3
        if (! is_file($customTemplate) || ! is_readable($customTemplate)) {
129 1
            throw InvalidTemplateSpecified::notFoundOrNotReadable($customTemplate);
130
        }
131
132 2
        $content = file_get_contents($customTemplate);
133
134 2
        if ($content === false) {
135
            throw InvalidTemplateSpecified::notReadable($customTemplate);
136
        }
137
138 2
        if (trim($content) === '') {
139 1
            throw InvalidTemplateSpecified::empty($customTemplate);
140
        }
141
142 1
        return $content;
143
    }
144
}
145