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

Generator::generateMigration()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 3
dl 0
loc 29
ccs 13
cts 13
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
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\Tools\Console\Helper\MigrationDirectoryHelper;
9
use InvalidArgumentException;
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 sprintf;
18
use function str_replace;
19
use function trim;
20
21
class Generator
22
{
23
    private const MIGRATION_TEMPLATE = <<<'TEMPLATE'
24
<?php
25
26
declare(strict_types=1);
27
28
namespace <namespace>;
29
30
use Doctrine\DBAL\Schema\Schema;
31
use Doctrine\Migrations\AbstractMigration;
32
33
/**
34
 * Auto-generated Migration: Please modify to your needs!
35
 */
36
final class Version<version> extends AbstractMigration
37
{
38
    public function getDescription() : string
39
    {
40
        return '';
41
    }
42
43
    public function up(Schema $schema) : void
44
    {
45
        // this up() migration is auto-generated, please modify it to your needs
46
<up>
47
    }
48
49
    public function down(Schema $schema) : void
50
    {
51
        // this down() migration is auto-generated, please modify it to your needs
52
<down>
53
    }
54
}
55
56
TEMPLATE;
57
58
    /** @var Configuration */
59
    private $configuration;
60
61
    /** @var string|null */
62
    private $template;
63
64 12
    public function __construct(Configuration $configuration)
65
    {
66 12
        $this->configuration = $configuration;
67 12
    }
68
69 11
    public function generateMigration(
70
        string $version,
71
        ?string $up = null,
72
        ?string $down = null
73
    ) : string {
74
        $placeHolders = [
75 11
            '<namespace>',
76
            '<version>',
77
            '<up>',
78
            '<down>',
79
        ];
80
81
        $replacements = [
82 11
            $this->configuration->getMigrationsNamespace(),
83 11
            $version,
84 7
            $up !== null ? '        ' . implode("\n        ", explode("\n", $up)) : null,
85 7
            $down !== null ? '        ' . implode("\n        ", explode("\n", $down)) : null,
86
        ];
87
88 11
        $code = str_replace($placeHolders, $replacements, $this->getTemplate());
89 9
        $code = preg_replace('/^ +$/m', '', $code);
90
91 9
        $directoryHelper = new MigrationDirectoryHelper($this->configuration);
92 9
        $dir             = $directoryHelper->getMigrationDirectory();
93 9
        $path            = $dir . '/Version' . $version . '.php';
94
95 9
        file_put_contents($path, $code);
96
97 9
        return $path;
98
    }
99
100 11
    private function getTemplate() : string
101
    {
102 11
        if ($this->template === null) {
103 11
            $this->template = $this->loadCustomTemplate();
104
105 9
            if ($this->template === null) {
106 8
                $this->template = self::MIGRATION_TEMPLATE;
107
            }
108
        }
109
110 9
        return $this->template;
111
    }
112
113 11
    private function loadCustomTemplate() : ?string
114
    {
115 11
        $customTemplate = $this->configuration->getCustomTemplate();
116
117 11
        if ($customTemplate === null) {
118 8
            return null;
119
        }
120
121 3
        if (! is_file($customTemplate) || ! is_readable($customTemplate)) {
122 1
            throw new InvalidArgumentException(
123 1
                sprintf(
124 1
                    'The specified template "%s" cannot be found or is not readable.',
125 1
                    $customTemplate
126
                )
127
            );
128
        }
129
130 2
        $content = file_get_contents($customTemplate);
131
132 2
        if ($content === false) {
133
            throw new InvalidArgumentException(
134
                sprintf(
135
                    'The specified template "%s" could not be read.',
136
                    $customTemplate
137
                )
138
            );
139
        }
140
141 2
        if (trim($content) === '') {
142 1
            throw new InvalidArgumentException(
143 1
                sprintf(
144 1
                    'The specified template "%s" is empty.',
145 1
                    $customTemplate
146
                )
147
            );
148
        }
149
150 1
        return $content;
151
    }
152
}
153