GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 1.x ( 67d7cc...23c8ab )
by Théo
476:36 queued 441:37
created

LoadDataFixturesCommand::execute()   C

Complexity

Conditions 12
Paths 99

Size

Total Lines 71
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 14.8596

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 71
ccs 35
cts 48
cp 0.7292
rs 5.5809
cc 12
eloc 42
nc 99
nop 2
crap 14.8596

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
/*
4
 * This file is part of the Hautelook\AliceBundle package.
5
 *
6
 * (c) Baldur Rensch <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Hautelook\AliceBundle\Doctrine\Command;
13
14
use Doctrine\Bundle\DoctrineBundle\Registry;
15
use Doctrine\Common\Persistence\ManagerRegistry;
16
use Doctrine\DBAL\Sharding\PoolingShardConnection;
17
use Hautelook\AliceBundle\Alice\DataFixtures\Fixtures\LoaderInterface as FixturesLoaderInterface;
18
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface;
19
use Hautelook\AliceBundle\Doctrine\DataFixtures\Executor\FixturesExecutorInterface;
20
use Hautelook\AliceBundle\Doctrine\Finder\FixturesFinder;
21
use Hautelook\AliceBundle\Doctrine\Generator\LoaderGeneratorInterface;
22
use Hautelook\AliceBundle\Finder\FixturesFinderInterface;
23
use Hautelook\AliceBundle\Resolver\BundlesResolverInterface;
24
use Symfony\Bundle\FrameworkBundle\Console\Application;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Helper\DialogHelper;
27
use Symfony\Component\Console\Helper\QuestionHelper;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\Console\Question\ConfirmationQuestion;
32
33
/**
34
 * Command used to load the fixtures.
35
 *
36
 * @author Théo FIDRY <[email protected]>
37
 */
38
class LoadDataFixturesCommand extends Command
39
{
40
    /**
41
     * @var BundlesResolverInterface
42
     */
43
    private $bundlesResolver;
44
45
    /**
46
     * @var ManagerRegistry
47
     */
48
    private $doctrine;
49
50
    /**
51
     * @var FixturesExecutorInterface
52
     */
53
    private $fixturesExecutor;
54
55
    /**
56
     * @var FixturesFinderInterface|FixturesFinder
57
     */
58
    private $fixturesFinder;
59
60
    /**
61
     * @var FixturesLoaderInterface
62
     */
63
    private $fixturesLoader;
64
65
    /**
66
     * @var LoaderInterface
67
     */
68
    private $loader;
69
70
    /**
71
     * @var LoaderGeneratorInterface
72
     */
73
    private $loaderGenerator;
74
75
    /**
76
     * @param string                    $name             Command name
77
     * @param ManagerRegistry           $doctrine
78
     * @param LoaderInterface           $loader
79
     * @param FixturesLoaderInterface   $fixturesLoader
80
     * @param FixturesFinderInterface   $fixturesFinder
81
     * @param BundlesResolverInterface  $bundlesResolver
82
     * @param LoaderGeneratorInterface  $loaderGenerator
83
     * @param FixturesExecutorInterface $fixturesExecutor
84
     */
85 120
    public function __construct(
86
        $name,
87
        ManagerRegistry $doctrine,
88
        LoaderInterface $loader,
89
        FixturesLoaderInterface $fixturesLoader,
90
        FixturesFinderInterface $fixturesFinder,
91
        BundlesResolverInterface $bundlesResolver,
92
        LoaderGeneratorInterface $loaderGenerator,
93
        FixturesExecutorInterface $fixturesExecutor
94
    ) {
95 120
        $this->doctrine = $doctrine;
96 120
        $this->loader = $loader;
97 120
        $this->fixturesLoader = $fixturesLoader;
98 120
        $this->fixturesFinder = $fixturesFinder;
99 120
        $this->bundlesResolver = $bundlesResolver;
100 120
        $this->loaderGenerator = $loaderGenerator;
101 120
        $this->fixturesExecutor = $fixturesExecutor;
102
103 120
        parent::__construct($name);
104 120
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 120
    protected function configure()
110
    {
111 120
        $this
112 120
            ->setAliases(['fixtures:load', 'hautelook_alice:fixtures:load'])
113 120
            ->setDescription('Load data fixtures to your database.')
114 120
            ->addOption(
115 120
                'bundle',
116 120
                'b',
117 120
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
118
                'Bundles where fixtures should be loaded.'
119 120
            )
120 120
            ->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory to load data fixtures from.')
121 120
            ->addOption(
122 120
                'manager',
123 120
                'em',
124
                InputOption::VALUE_REQUIRED,
125 120
                'The entity manager to use for this command.'
126 120
            )
127 120
            ->addOption(
128 120
                'append',
129 120
                null,
130
                InputOption::VALUE_NONE,
131 120
                'Append the data fixtures instead of deleting all data from the database first.'
132 120
            )
133 120
            ->addOption(
134 120
                'shard',
135 120
                null,
136
                InputOption::VALUE_REQUIRED,
137 120
                'The shard database id to use for this command.'
138
            )
139
        ;
140 120
141 120
        if ($this->doctrine instanceof Registry) {
142 120
            $this->addOption('purge-with-truncate',
143 120
                null,
144
                InputOption::VALUE_NONE,
145 120
                'Purge data by using a database-level TRUNCATE statement when using Doctrine ORM.'
146 120
            );
147 120
        }
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     *
153
     * \RuntimeException Unsupported Application type
154 114
     */
155
    protected function execute(InputInterface $input, OutputInterface $output)
156 114
    {
157 114
        // Warn the user that the database will be purged
158 114
        // Ask him to confirm his choice
159
        if ($input->isInteractive() && !$input->getOption('append')) {
160
            if (false === $this->askConfirmation(
161
                    $input,
162
                    $output,
163
                    '<question>Careful, database will be purged. Do you want to continue y/N ?</question>',
164
                    false
165 114
                )
166
            ) {
167
                return;
168
            }
169
        }
170
171
        $manager = $this->doctrine->getManager($input->getOption('manager'));
172
        $environment = $input->getOption('env');
173
        $bundles = $input->getOption('bundle');
174
175
        /** @var Application $application */
176
        $application = $this->getApplication();
177 114
        if (false === $application instanceof Application) {
178 78
            throw new \RuntimeException('Expected Symfony\Bundle\FrameworkBundle\Console\Application application.');
179 78
        }
180
181
        // Get bundles
182 78
        if (true === empty($bundles)) {
183 78
            $dirOrFile = $input->getOption('fixtures');
184
            if ($dirOrFile) {
185
                $bundles = is_array($dirOrFile) ? $dirOrFile : [$dirOrFile];
186
            } else {
187
                $bundles = $application->getKernel()->getBundles();
188 78
            }
189 30
        } else {
190 30
            $bundles = $this->bundlesResolver->resolveBundles($application, $bundles);
191 48
        }
192
193
        $fixtures = $this->fixturesFinder->getFixtures($application->getKernel(), $bundles, $environment);
194 78
195
        $output->writeln(sprintf('  <comment>></comment> <info>%s</info>', 'fixtures found:'));
196 78
        foreach ($fixtures as $fixture) {
197 78
            $output->writeln(sprintf('      <comment>-</comment> <info>%s</info>', $fixture));
198 78
        }
199 78
200
        $truncate = $input->hasOption('purge-with-truncate') ? $input->getOption('purge-with-truncate') : false;
201 78
202
        // Shard database
203
        $shard = $input->getOption('shard');
204 78
        if (!empty($shard)) {
205 78
            $connection = $manager->getConnection();
206 6
            if (!$connection instanceof PoolingShardConnection) {
207 6
                throw new \RuntimeException('Expected Doctrine\DBAL\Sharding\PoolingShardConnection connection when using shard option.');
208
            }
209
210
            // Switch to shard database
211
            $connection->connect($shard);
212 6
        }
213 6
214
        $this->fixturesExecutor->execute(
215 78
            $manager,
216 78
            $this->loaderGenerator->generate($this->loader, $this->fixturesLoader, $bundles, $environment),
217 78
            $fixtures,
218 78
            $input->getOption('append'),
219 78
            function ($message) use ($output) {
220 78
                $output->writeln(sprintf('  <comment>></comment> <info>%s</info>', $message));
221 78
            },
222 78
            $truncate
223
        );
224 78
        $output->writeln(sprintf('  <comment>></comment> <info>%s</info>', 'fixtures loaded'));
225 78
    }
226 78
227
    /**
228
     * Prompt to the user a message to ask him a confirmation.
229
     *
230
     * @param InputInterface  $input
231
     * @param OutputInterface $output
232
     * @param string          $question
233
     * @param bool            $default
234
     *
235
     * @return bool User choice.
236
     */
237
    private function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default)
238
    {
239
        if (false === class_exists('Symfony\Component\Console\Question\ConfirmationQuestion')) {
240
            /** @var DialogHelper $dialogHelper */
241
            $dialogHelper = $this->getHelperSet()->get('dialog');
242
243
            return $dialogHelper->askConfirmation($output, $question, $default);
244
        }
245
246
        /** @var QuestionHelper $questionHelper */
247
        $questionHelper = $this->getHelperSet()->get('question');
248
        $question = new ConfirmationQuestion($question, $default);
249
250
        return (bool) $questionHelper->ask($input, $output, $question);
251
    }
252
}
253