Passed
Push — master ( 59a5a5...30f57b )
by huang
02:58
created

SeedCreate::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Console;
11
12
use Phinx\Config\Config as MConfig;
13
use Phinx\Console\Command\Create;
14
use Phinx\Util\Util;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class SeedCreate extends Create
19
{
20 View Code Duplication
    public function getConfig()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $path = app()->getPath().'/database/schema';
23
        $this->setName('seed:create');
24
        $database = config()->get('database');
25
        $env = [];
26
        $keys = array_keys($database);
27
        $default = $keys[0];
28
        foreach ($database as $name => $config) {
29
            $env[$name] = [
30
                'adapter' => 'mysql',
31
                'host' => config()->get('database.'.$name.'.host'),
32
                'name' => config()->get('database.'.$name.'.name'),
33
                'user' => config()->get('database.'.$name.'.user'),
34
                'pass' => config()->get('database.'.$name.'.pass'),
35
                'port' => config()->get('database.'.$name.'.port'),
36
                'charset' => config()->get('database.'.$name.'.charset', 'utf8'),
37
            ];
38
        }
39
40
        return new MConfig(array(
41
            'paths' => array(
42
                'migrations' => $path,
43
                'seeds' => $path,
44
            ),
45
            'environments' => array_merge([
46
                'default_database' => $default,
47
            ], $env),
48
        ));
49
    }
50
51
    public function configure()
52
    {
53
        parent::configure();
54
55
        $this->setConfig($this->getConfig());
56
    }
57
58
    /**
59
     * Create the new migration.
60
     *
61
     * @param InputInterface  $input
62
     * @param OutputInterface $output
63
     *
64
     * @throws \RuntimeException
65
     * @throws \InvalidArgumentException
66
     */
67
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69
        $this->bootstrap($input, $output);
70
71
        // get the migration path from the config
72
        $path = $this->getConfig()->getMigrationPaths()[0];
73
74
        if (!file_exists($path)) {
75
            mkdir($path, 0755, true);
76
        }
77
78
        $this->verifyMigrationDirectory($path);
79
80
        $path = realpath($path);
81
        $className = $input->getArgument('name');
82
83
        if (!Util::isValidPhinxClassName($className)) {
84
            throw new \InvalidArgumentException(sprintf(
85
                'The migration class name "%s" is invalid. Please use CamelCase format.',
86
                $className
87
            ));
88
        }
89
90
        if (!Util::isUniqueMigrationClassName($className, $path)) {
91
            throw new \InvalidArgumentException(sprintf(
92
                'The migration class name "%s" already exists',
93
                $className
94
            ));
95
        }
96
97
        // Compute the file path
98
        $fileName = $className.'.php';
99
        $filePath = $path.DIRECTORY_SEPARATOR.$fileName;
100
101
        if (is_file($filePath)) {
102
            throw new \InvalidArgumentException(sprintf(
103
                'The file "%s" already exists',
104
                $filePath
105
            ));
106
        }
107
108
        $contents = $this->getTemplate();
109
110
        // inject the class names appropriate to this migration
111
        $classes = array(
112
            '$useClassName' => $this->getConfig()->getMigrationBaseClassName(false),
113
            '$className' => $className,
114
            '$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true),
115
        );
116
        $contents = strtr($contents, $classes);
117
118
        if (false === file_put_contents($filePath, $contents)) {
119
            throw new \RuntimeException(sprintf(
120
                'The file "%s" could not be written to',
121
                $path
122
            ));
123
        }
124
125
        $output->writeln('<info>using migration base class</info> '.$classes['$useClassName']);
126
127
        if (!empty($altTemplate)) {
0 ignored issues
show
Bug introduced by
The variable $altTemplate seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
128
            $output->writeln('<info>using alternative template</info> '.$altTemplate);
129
        } elseif (!empty($creationClassName)) {
0 ignored issues
show
Bug introduced by
The variable $creationClassName does not exist. Did you mean $className?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
130
            $output->writeln('<info>using template creation class</info> '.$creationClassName);
131
        } else {
132
            $output->writeln('<info>using default template</info>');
133
        }
134
135
        $output->writeln('<info>created</info> '.str_replace(getcwd(), '', $filePath));
136
    }
137
138
    public function getTemplate()
139
    {
140
        return '
141
<?php
142
143
use FastD\Model\Migration;
144
use Phinx\Db\Table;
145
146
class $className extends Migration
147
{
148
    /**
149
     * @return Table
150
     */
151
    public function setUp()
152
    {
153
        
154
    }
155
    
156
    /**
157
     * The table preinstall dataset.
158
     *
159
     * @return mixed
160
     */
161
    public function dataSet(Table $table)
162
    {
163
        
164
    }
165
}';
166
    }
167
}
168