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

SeedRun::getConfig()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 22

Duplication

Lines 29
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 0
dl 29
loc 29
ccs 0
cts 24
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
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\Migrate;
14
use Phinx\Migration\AbstractMigration;
15
use Phinx\Migration\Manager;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class SeedRun.
21
 */
22
class SeedRun extends Migrate
23
{
24
    protected $env = 'default';
25
26
    /**
27
     * @var bool
28
     */
29
    protected $booted = false;
30
31
    /**
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     */
35
    protected function loadManager(InputInterface $input, OutputInterface $output)
36
    {
37
        if (null === $this->getManager()) {
38
            $manager = new Migration($this->getConfig(), $input, $output);
39
            $this->setManager($manager);
40
        }
41
    }
42
43 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...
44
    {
45
        $path = app()->getPath().'/database/schema';
46
        $database = config()->get('database');
47
        $env = [];
48
        $keys = array_keys($database);
49
        $default = $keys[0];
50
        foreach ($database as $name => $config) {
51
            $env[$name] = [
52
                'adapter' => 'mysql',
53
                'host' => config()->get('database.'.$name.'.host'),
54
                'name' => config()->get('database.'.$name.'.name'),
55
                'user' => config()->get('database.'.$name.'.user'),
56
                'pass' => config()->get('database.'.$name.'.pass'),
57
                'port' => config()->get('database.'.$name.'.port'),
58
                'charset' => config()->get('database.'.$name.'.charset', 'utf8'),
59
            ];
60
        }
61
62
        return new MConfig(array(
63
            'paths' => array(
64
                'migrations' => $path,
65
                'seeds' => $path,
66
            ),
67
            'environments' => array_merge([
68
                'default_database' => $default,
69
            ], $env),
70
        ));
71
    }
72
73
    public function configure()
74
    {
75
        parent::configure();
76
77
        $this->setName('seed:run');
78
79
        $this->setConfig($this->getConfig());
80
    }
81
82
    /**
83
     * @param InputInterface  $input
84
     * @param OutputInterface $output
85
     */
86
    public function bootstrap(InputInterface $input, OutputInterface $output)
87
    {
88
        if (false === $this->booted) {
89
            parent::bootstrap($input, $output);
90
            $this->booted = true;
91
        }
92
    }
93
94
    /**
95
     * @param InputInterface  $input
96
     * @param OutputInterface $output
97
     *
98
     * @return int
99
     */
100
    public function execute(InputInterface $input, OutputInterface $output)
101
    {
102
        $this->bootstrap($input, $output);
103
104
        $config = $this->getConfig()->getEnvironment($this->env);
105
        $adapter = $this->getManager()->getEnvironment($this->env)->getAdapter();
106
        if (!$adapter->hasDatabase($config['name'])) {
107
            $adapter->createDatabase($config['name']);
108
        }
109
110
        $code = parent::execute($input, $output);
111
112
        return $code;
113
    }
114
}
115
116
/**
117
 * Class Migration.
118
 */
119
class Migration extends Manager
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
120
{
121
    /**
122
     * Gets an array of the database migrations, indexed by migration name (aka creation time) and sorted in ascending
123
     * order.
124
     *
125
     * @throws \InvalidArgumentException
126
     *
127
     * @return AbstractMigration[]
128
     */
129
    public function getMigrations()
130
    {
131
        if (null === $this->migrations) {
132
            $phpFiles = $this->getMigrationFiles();
133
134
            // filter the files to only get the ones that match our naming scheme
135
            $fileNames = array();
136
            /** @var AbstractMigration[] $migrations */
137
            $migrations = array();
138
139
            $version = date('Ymd');
140
            $versionIncrementSeed = mt_rand(1000, 9999 - count($phpFiles));
141
142
            foreach ($phpFiles as $filePath) {
143
                $class = pathinfo($filePath, PATHINFO_FILENAME);
144
                $fileNames[$class] = basename($filePath);
145
                require_once $filePath;
146
                $migration = new $class($version.(++$versionIncrementSeed), $this->getInput(), $this->getOutput());
147
148
                if (!($migration instanceof AbstractMigration)) {
149
                    throw new \InvalidArgumentException(sprintf(
150
                        'The class "%s" in file "%s" must extend \Phinx\Migration\AbstractMigration',
151
                        $class,
152
                        $filePath
153
                    ));
154
                }
155
156
                $migrations[$migration->getName()] = $migration;
157
            }
158
159
            ksort($migrations);
160
            $this->setMigrations($migrations);
161
        }
162
163
        return $this->migrations;
164
    }
165
}
166