Completed
Pull Request — master (#1281)
by
unknown
02:22
created

SeedRun::execute()   C

Complexity

Conditions 10
Paths 74

Size

Total Lines 60
Code Lines 35

Duplication

Lines 12
Ratio 20 %

Code Coverage

Tests 32
CRAP Score 10.3936

Importance

Changes 0
Metric Value
dl 12
loc 60
ccs 32
cts 38
cp 0.8421
rs 6.5333
c 0
b 0
f 0
cc 10
eloc 35
nc 74
nop 2
crap 10.3936

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
    protected function configure()
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...
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
            ->setHelp(
50
                <<<EOT
51
The <info>seed:run</info> command runs all available or individual seeders
52
53
<info>phinx seed:run -e development</info>
54
<info>phinx seed:run -e development -s UserSeeder</info>
55
<info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info>
56
<info>phinx seed:run -e development -v</info>
57
58
EOT
59 35
            );
60 35
    }
61
62
    /**
63
     * Run database seeders.
64
     *
65
     * @param \Symfony\Component\Console\Input\InputInterface $input
66
     * @param \Symfony\Component\Console\Output\OutputInterface $output
67
     * @return void
68
     */
69 4
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71 4
        $this->bootstrap($input, $output);
72
73 4
        $seedSet = $input->getOption('seed');
74 4
        $environment = $input->getOption('environment');
75
76 4 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...
77 3
            $environment = $this->getConfig()->getDefaultEnvironment();
78 3
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
79 3
        } else {
80 1
            $output->writeln('<info>using environment</info> ' . $environment);
81
        }
82
83 4
        if (!$this->getConfig()->hasEnvironment($environment)) {
84 4
            $output->writeln('<error>Invalid environment name! Please specify an environment name from your config file.</error>');
85 3
            return;
86 3
        }
87
88 4
        $envOptions = $this->getConfig()->getEnvironment($environment);
89
        if (isset($envOptions['adapter'])) {
90
            $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
91
        }
92 4
93 3
        if (isset($envOptions['wrapper'])) {
94 3
            $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
95 1
        }
96 1
97 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...
98
            $output->writeln('<info>using database</info> ' . $envOptions['name']);
99 3
        } else {
100
            $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
101
102 3
            return;
103
        }
104
105
        if (isset($envOptions['table_prefix'])) {
106 3
            $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
107
        }
108 3
        if (isset($envOptions['table_suffix'])) {
109
            $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
110 2
        }
111 2
112
        $start = microtime(true);
113 1
114 1
        if (empty($seedSet)) {
115 1
            // run all the seed(ers)
116
            $this->getManager()->seed($environment);
117
        } else {
118 3
            // run seed(ers) specified in a comma-separated list of classes
119
            foreach ($seedSet as $seed) {
120 3
                $this->getManager()->seed($environment, trim($seed));
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