Completed
Pull Request — master (#1622)
by
unknown
01:27
created

SeedRun::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.6
c 0
b 0
f 0
cc 1
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
    protected static $defaultName = 'seed:run';
38
39
    /**
40 35
     * {@inheritdoc}
41
     */
42 35
    protected function configure()
43
    {
44 35
        parent::configure();
45
46 35
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
47 35
48 35
        $this->setDescription('Run database seeders')
49 35
            ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?')
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 -v</info>
58
59 35
EOT
60 35
            );
61
    }
62
63
    /**
64
     * Run database seeders.
65
     *
66
     * @param \Symfony\Component\Console\Input\InputInterface $input
67
     * @param \Symfony\Component\Console\Output\OutputInterface $output
68
     * @return int integer 0 on success, or an error code.
69 4
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71 4
    {
72
        $this->bootstrap($input, $output);
73 4
74 4
        $seedSet = $input->getOption('seed');
75
        $environment = $input->getOption('environment');
76 4
77 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...
78 3
            $environment = $this->getConfig()->getDefaultEnvironment();
79 3
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
80 1
        } else {
81
            $output->writeln('<info>using environment</info> ' . $environment);
82
        }
83 4
84 4
        if (!$this->getConfig()->hasEnvironment($environment)) {
85 3
            $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
86 3
87
            return 1;
88 4
        }
89
90
        $envOptions = $this->getConfig()->getEnvironment($environment);
91
        if (isset($envOptions['adapter'])) {
92 4
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
93 3
        }
94 3
95 1
        if (isset($envOptions['wrapper'])) {
96 1
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
97
        }
98
99 3 View Code Duplication
        if (isset($envOptions['name'])) {
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...
100
            $output->writeln('<info>using database</info> ' . $envOptions['name']);
101
        } else {
102 3
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
103
104
            return 1;
105
        }
106 3
107
        if (isset($envOptions['table_prefix'])) {
108 3
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
109
        }
110 2
        if (isset($envOptions['table_suffix'])) {
111 2
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
112
        }
113 1
114 1
        $start = microtime(true);
115 1
116
        if (empty($seedSet)) {
117
            // run all the seed(ers)
118 3
            $this->getManager()->seed($environment);
119
        } else {
120 3
            // run seed(ers) specified in a comma-separated list of classes
121 3
            foreach ($seedSet as $seed) {
0 ignored issues
show
Bug introduced by
The expression $seedSet of type string|array<integer,string>|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
122 3
                $this->getManager()->seed($environment, trim($seed));
123
            }
124
        }
125
126
        $end = microtime(true);
127
128
        $output->writeln('');
129
        $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
130
131
        return 0;
132
    }
133
}
134