Test Setup Failed
Push — master ( 43ee18...a50601 )
by Josh
04:51
created

GenerateMigration::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 35
rs 8.9297
c 0
b 0
f 0
nc 10
nop 2
cc 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 2/15/18
6
 * Time: 3:23 PM
7
 */
8
9
namespace LCI\Blend\Console;
10
11
use LCI\Blend\Migrations\MigrationsCreator;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
16
class GenerateMigration extends BaseCommand
17
{
18
    protected $loadMODX = false;
19
20
    /**
21
     * @see https://symfony.com/doc/current/console.html
22
     *
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('blend:generate')
28
            ->setDescription('Generate a Blend Migration Class')
29
            ->addOption(
30
                'name',
31
                'n',
32
                InputOption::VALUE_OPTIONAL,
33
                'The name of the generated migration file'
34
            )
35
            ->addOption(
36
                'type',
37
                't',
38
                InputOption::VALUE_OPTIONAL,
39
                'Server type to run migrations as, default is master. Possible master, staging, dev and local',
40
                'master'
41
            )
42
            ->addOption(
43
                'path',
44
                'p',
45
                InputOption::VALUE_OPTIONAL,
46
                'Optional set the directory path to write the migration. Note database/migrations will be added to the path and if needed created.',
47
                ''
48
            );
49
    }
50
51
    /**
52
     * @param InputInterface $input
53
     * @param OutputInterface $output
54
     * @return int|null|void
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $name = (string)$input->getOption('name');
59
        $type = (string)$input->getOption('type');
60
        $path = (string)$input->getOption('path');
61
62
        $output->writeln('Generate name: '.$name);
63
64
        $migrationCreator = new MigrationsCreator($this->consoleUserInteractionHandler);
65
66
        if (empty($path)) {
67
            $config = $this->console->getConfig();
68
            if (isset($config['BLEND_LOCAL_MIGRATION_PATH'])) {
69
                $path = $config['BLEND_LOCAL_MIGRATION_PATH'];
70
71
            } else {
72
                $path = dirname(dirname(dirname(dirname(__DIR__)))).'components/';
73
                if (file_exists($path)) {
74
                    $path .= 'blend/';
75
                    if (!file_exists($path)) {
76
                        mkdir($path);
77
                    }
78
                }
79
            }
80
        }
81
82
        $success = $migrationCreator
83
            ->setName($name)
84
            ->setDescription('')
85
            ->setServerType($type)
86
            ->setBaseMigrationsPath($path)
87
            ->createBlankMigrationClassFile();
88
89
        if (!$success) {
90
            $output->writeln('Could not write file');
91
        }
92
    }
93
}