Passed
Pull Request — master (#47)
by Simon
04:32
created

GenerateCommand::getName()   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
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command;
13
14
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * @author Matthew Fitzgerald <[email protected]>
21
 */
22
class GenerateCommand extends AbstractCommand
23
{
24
    protected static $defaultName = 'mongodb:migrations:generate';
25
26
    private static $_template =
27
            '<?php
28
29
namespace <namespace>;
30
31
use AntiMattr\MongoDB\Migrations\AbstractMigration;
32
use MongoDB\Database;
33
34
/**
35
 * Auto-generated Migration: Please modify to your needs!
36
 */
37
class Version<version> extends AbstractMigration
38
{
39
    /**
40
     * @return string
41
     */
42
    public function getDescription()
43
    {
44
        return "";
45
    }
46
47
    public function up(Database $db)
48
    {
49
        // this up() migration is auto-generated, please modify it to your needs
50
<up>
51
    }
52
53
    public function down(Database $db)
54
    {
55
        // this down() migration is auto-generated, please modify it to your needs
56
<down>
57
    }
58
}
59
';
60
61 2
    protected function configure()
62
    {
63
        $this
64 2
                ->setDescription('Generate a blank migration class.')
65 2
                ->addOption('editor-cmd', null, InputOption::VALUE_OPTIONAL, 'Open file with this command upon creation.')
66 2
                ->setHelp(<<<'EOT'
67 2
The <info>%command.name%</info> command generates a blank migration class:
68
69
    <info>%command.full_name%</info>
70
71
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor:
72
73
    <info>%command.full_name% --editor-cmd=mate</info>
74
EOT
75
        );
76
77 2
        parent::configure();
78 2
    }
79
80
    /**
81
     * @param \Symfony\Component\Console\Input\InputInterface
82
     * @param \Symfony\Component\Console\Output\OutputInterface
83
     */
84 2
    public function execute(InputInterface $input, OutputInterface $output)
85
    {
86 2
        $configuration = $this->getMigrationConfiguration($input, $output);
87
88 2
        $version = $this->getVersionString();
89 2
        $path = $this->generateMigration($configuration, $input, $version);
90
91 1
        $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));
92 1
    }
93
94
    /**
95
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration
96
     * @param \Symfony\Component\Console\Input\InputInterface
97
     * @param string $version
98
     * @param string $up
99
     * @param string $down
100
     *
101
     * @return string $path
102
     *
103
     * @throws \InvalidArgumentException
104
     */
105 2
    protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null)
106
    {
107
        $placeHolders = [
108 2
            '<namespace>',
109
            '<version>',
110
            '<up>',
111
            '<down>',
112
        ];
113
        $replacements = [
114 2
            $configuration->getMigrationsNamespace(),
115 2
            $version,
116 2
            $up ? '        ' . implode("\n        ", explode("\n", $up)) : null,
117 2
            $down ? '        ' . implode("\n        ", explode("\n", $down)) : null,
118
        ];
119 2
        $code = str_replace($placeHolders, $replacements, self::$_template);
120
121 2
        $dir = $this->getDirectory($configuration);
122
123
        // Verify Migrations directory exists
124 2
        if (!file_exists($dir)) {
125 1
            throw new \InvalidArgumentException(
126 1
                sprintf(
127 1
                    'Migrations directory "%s" does not exist.',
128 1
                    $dir
129
                )
130
            );
131
        }
132
133 1
        $path = $this->buildVersionPath($dir, $version);
134
135
        // Output Version
136 1
        file_put_contents($path, $code);
137
138 1
        if ($editorCmd = $input->getOption('editor-cmd')) {
139
            shell_exec(
140
                sprintf(
141
                    '%s %s',
142
                    $editorCmd,
143
                    escapeshellarg($path)
144
                )
145
            );
146
        }
147
148 1
        return $path;
149
    }
150
151
    /**
152
     * @param Configuration $configuration
153
     *
154
     * @return string
155
     */
156 2
    protected function getDirectory(Configuration $configuration)
157
    {
158 2
        $dir = $configuration->getMigrationsDirectory() ?: getcwd();
159
160 2
        return rtrim($dir, '/');
161
    }
162
163
    /**
164
     * @param string $dir
165
     * @param string $version
166
     *
167
     * @return string
168
     */
169 1
    protected function buildVersionPath($dir, $version)
170
    {
171 1
        return sprintf(
172 1
            '%s/Version%s.php',
173 1
            $dir,
174 1
            $version
175
        );
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    protected function getVersionString()
182
    {
183
        return date('YmdHis');
184
    }
185
}
186