GenerateCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 84
c 2
b 0
f 0
dl 0
loc 157
ccs 35
cts 42
cp 0.8333
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A execute() 0 10 1
A generateMigration() 0 39 5
A getDirectory() 0 5 2
A buildVersionPath() 0 6 1
A getVersionString() 0 3 1
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
93 1
        return 0;
94
    }
95
96
    /**
97
     * @param \AntiMattr\MongoDB\Migrations\Configuration\Configuration
98
     * @param \Symfony\Component\Console\Input\InputInterface
99
     * @param string $version
100
     * @param string $up
101
     * @param string $down
102
     *
103
     * @return string $path
104
     *
105
     * @throws \InvalidArgumentException
106
     */
107 2
    protected function generateMigration(Configuration $configuration, InputInterface $input, $version, $up = null, $down = null)
108
    {
109
        $placeHolders = [
110 2
            '<namespace>',
111
            '<version>',
112
            '<up>',
113
            '<down>',
114
        ];
115
        $replacements = [
116 2
            $configuration->getMigrationsNamespace(),
117 2
            $version,
118 2
            $up ? '        ' . implode("\n        ", explode("\n", $up)) : null,
119 2
            $down ? '        ' . implode("\n        ", explode("\n", $down)) : null,
120
        ];
121 2
        $code = str_replace($placeHolders, $replacements, self::$_template);
122
123 2
        $dir = $this->getDirectory($configuration);
124
125
        // Verify Migrations directory exists
126 2
        if (!file_exists($dir)) {
127 1
            throw new \InvalidArgumentException(sprintf('Migrations directory "%s" does not exist.', $dir));
128
        }
129
130 1
        $path = $this->buildVersionPath($dir, $version);
131
132
        // Output Version
133 1
        file_put_contents($path, $code);
134
135 1
        if ($editorCmd = $input->getOption('editor-cmd')) {
136
            shell_exec(
137
                sprintf(
138
                    '%s %s',
139
                    $editorCmd,
140
                    escapeshellarg($path)
141
                )
142
            );
143
        }
144
145 1
        return $path;
146
    }
147
148
    /**
149
     * @return string
150
     */
151 2
    protected function getDirectory(Configuration $configuration)
152
    {
153 2
        $dir = $configuration->getMigrationsDirectory() ?: getcwd();
154
155 2
        return rtrim($dir, '/');
156
    }
157
158
    /**
159
     * @param string $dir
160
     * @param string $version
161
     *
162
     * @return string
163
     */
164 1
    protected function buildVersionPath($dir, $version)
165
    {
166 1
        return sprintf(
167 1
            '%s/Version%s.php',
168 1
            $dir,
169 1
            $version
170
        );
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    protected function getVersionString()
177
    {
178
        return date('YmdHis');
179
    }
180
}
181