Completed
Pull Request — master (#1241)
by
unknown
02:14
created

SeedRun::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 9.0856
c 1
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Console
28
 */
29
namespace Phinx\Console\Command;
30
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class SeedRun extends AbstractCommand
36
{
37
    /**
38
     * {@inheritdoc}
39
     */
40 35
    protected function configure()
41
    {
42 35
        parent::configure();
43
44 35
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
45
46 35
        $this->setName('seed:run')
47 35
             ->setDescription('Run database seeders')
48 35
             ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?')
49 35
             ->addOption('--database', '-d', InputOption::VALUE_REQUIRED, 'What is the db reference required?')
50
             ->setHelp(
51
                 <<<EOT
52
The <info>seed:run</info> command runs all available or individual seeders
53
54
<info>phinx seed:run -e development</info>
55
<info>phinx seed:run -e development -s UserSeeder</info>
56
<info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info>
57
<info>phinx seed:run -e development -s UserSeeder -d databaseReference
58
<info>phinx seed:run -e development -v</info>
59 35
60 35
EOT
61
             );
62
    }
63
64
    /**
65
     * Run database seeders.
66
     *
67
     * @param \Symfony\Component\Console\Input\InputInterface $input
68
     * @param \Symfony\Component\Console\Output\OutputInterface $output
69 4
     * @return void
70
     */
71 4
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73 4
        $this->bootstrap($input, $output);
74 4
75
        $seedSet = $input->getOption('seed');
76 4
        $environment = $input->getOption('environment');
77 3
        $dbRef = $input->getOption('database');
78 3
79 3 View Code Duplication
        if ($environment === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80 1
            $environment = $this->getConfig()->getDefaultEnvironment();
81
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
82
        } else {
83 4
            $output->writeln('<info>using environment</info> ' . $environment);
84 4
        }
85 3
86 3
        $envOptions = $this->getConfig()->getEnvironment($environment);
87
        $databases = $this->getConfig()->getStorageConfigs($envOptions);
0 ignored issues
show
Bug introduced by
It seems like $envOptions defined by $this->getConfig()->getEnvironment($environment) on line 86 can also be of type null; however, Phinx\Config\ConfigInterface::getStorageConfigs() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88 4
        $start = microtime(true);
89
90
        if (!is_null($dbRef)) {
91
            $this->getManager()->setDbRef($dbRef);
92 4
            $this->outputEnvironmentInfo($databases[$dbRef], $output);
93 3
            if (empty($seedSet)) {
94 3
                // run all the seed(ers)
95 1
                $this->getManager()->seed($environment);
96 1
            } else {
97
                // run seed(ers) specified in a comma-separated list of classes
98
                foreach ($seedSet as $seed) {
99 3
                    $this->getManager()->seed($environment, trim($seed));
100
                }
101
            }
102 3
        } else {
103 View Code Duplication
            foreach ($databases as $adapterOptions) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
104
                $this->getManager()->setSeeds(null);
105
106 3
                if (isset($adapterOptions['dbRef'])) {
107
                    $this->getManager()->setDbRef($adapterOptions['dbRef']);
108 3
                }
109
110 2
                $this->outputEnvironmentInfo($adapterOptions, $output);
111 2
112
                if (empty($seedSet)) {
113 1
                    // run all the seed(ers)
114 1
                    $this->getManager()->seed($environment);
115 1
                } else {
116
                    // run seed(ers) specified in a comma-separated list of classes
117
                    foreach ($seedSet as $seed) {
118 3
                        $this->getManager()->seed($environment, trim($seed));
119
                    }
120 3
                }
121 3
            }
122 3
        }
123
124
        $end = microtime(true);
125
126
        $output->writeln('');
127
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
128
    }
129
}
130