Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

GenerateBundleCommand::updateKernel()   C

Complexity

Conditions 7
Paths 23

Size

Total Lines 70
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 0
cts 44
cp 0
rs 6.8519
c 0
b 0
f 0
cc 7
eloc 47
nc 23
nop 6
crap 56

How to fix   Long Method   

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
 * generator command
4
 */
5
6
namespace Graviton\GeneratorBundle\Command;
7
8
use Graviton\GeneratorBundle\Generator\BundleGenerator;
9
use Graviton\GeneratorBundle\Manipulator\BundleBundleManipulator;
10
use Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand as SymfonyGenerateBundleCommand;
11
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\HttpKernel\KernelInterface;
16
17
/**
18
 * generator command
19
 *
20
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
21
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
22
 * @link     http://swisscom.ch
23
 */
24
class GenerateBundleCommand extends SymfonyGenerateBundleCommand
25
{
26
27
    /**
28
     * {@inheritDoc}
29
     *
30
     * @return void
31
     */
32 14
    protected function configure()
33
    {
34 14
        parent::configure();
35
36 14
        $this->addOption(
37 14
            'loaderBundleName',
38 14
            'lbn',
39 14
            InputOption::VALUE_OPTIONAL,
40 14
            'Name of the bundle to manipulate, defaults to GravitonCoreBundle',
41 7
            'GravitonCoreBundle'
42 7
        )
43 14
             ->addOption(
44 14
                 'doUpdateKernel',
45 14
                 'dak',
46 14
                 InputOption::VALUE_OPTIONAL,
47 14
                 'If "true", update the kernel, "false" if we should skip that.',
48 7
                 'true'
49 7
             )
50 14
             ->setName('graviton:generate:bundle')
51 14
             ->setDescription('Generates a graviton bundle');
52 14
    }
53
54
    /**
55
     * {@inheritDoc}
56
     *
57
     * @param InputInterface  $input  input
58
     * @param OutputInterface $output output
59
     *
60
     * @return void
61
     */
62 12
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64 12
        parent::execute(
65 6
            $input,
66
            $output
67 6
        );
68
69 12
        $output->writeln(
70 6
            'Please review Resource/config/config.xml before commiting'
71 6
        );
72 12
    }
73
74
    /**
75
     * {@inheritDoc}
76
     * Add the new bundle to the BundleBundle loader infrastructure instead of main kernel
77
     *
78
     * @param QuestionHelper  $questionHelper dialog
79
     * @param InputInterface  $input          input
80
     * @param OutputInterface $output         output
81
     * @param KernelInterface $kernel         kernel
82
     * @param string          $namespace      namespace
83
     * @param string          $bundle         bundle
84
     *
85
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
86
     */
87
    protected function updateKernel(
88
        QuestionHelper $questionHelper,
89
        InputInterface $input,
90
        OutputInterface $output,
91
        KernelInterface $kernel,
92
        $namespace,
93
        $bundle
94
    ) {
95
96
        // skip if kernel manipulation disabled by options (defaults to true)
97
        $doUpdate = $input->getOption('doUpdateKernel');
98
        if ($doUpdate == 'false') {
99
            return;
100
        }
101
102
        $auto = true;
103
        if ($input->isInteractive()) {
104
            $auto = $questionHelper->doAsk(
105
                $output,
106
                $questionHelper->getQuestion(
107
                    'Confirm automatic update of your core bundle',
108
                    'yes',
109
                    '?'
110
                )
111
            );
112
        }
113
114
        $output->write('Enabling the bundle inside the core bundle: ');
115
        $coreBundle = $kernel->getBundle($input->getOption('loaderBundleName'));
116
        if (!is_a(
117
            $coreBundle,
118
            '\Graviton\BundleBundle\GravitonBundleInterface'
119
        )
120
        ) {
121
            throw new \LogicException(
122
                'GravitonCoreBundle does not implement GravitonBundleInterface'
123
            );
124
        }
125
        $manip = new BundleBundleManipulator($coreBundle);
126
        try {
127
            $ret = $auto ? $manip->addBundle($namespace . '\\' . $bundle) : false;
128
129
            if (!$ret) {
130
                $reflected = new \ReflectionObject($kernel);
131
132
                return array(
133
                    sprintf(
134
                        '- Edit <comment>%s</comment>',
135
                        $reflected->getFilename()
136
                    ),
137
                    '  and add the following bundle in the <comment>GravitonCoreBundle::getBundles()</comment> method:',
138
                    '',
139
                    sprintf(
140
                        '    <comment>new %s(),</comment>',
141
                        $namespace . '\\' . $bundle
142
                    ),
143
                    ''
144
                );
145
            }
146
        } catch (\RuntimeException $e) {
147
            return array(
148
                sprintf(
149
                    'Bundle <comment>%s</comment> is already defined in <comment>%s)</comment>.',
150
                    $namespace . '\\' . $bundle,
151
                    'sGravitonCoreBundle::getBundles()'
152
                ),
153
                ''
154
            );
155
        }
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     * Don't check routing since graviton bundles usually get routed explicitly based on their naming.
161
     *
162
     * @param QuestionHelper  $questionHelper dialog
163
     * @param InputInterface  $input          input
164
     * @param OutputInterface $output         output
165
     * @param object          $bundle         bundle
166
     * @param object          $format         format
167
     *
168
     * @return string[]
169
     */
170 12
    protected function updateRouting(
171
        QuestionHelper $questionHelper,
172
        InputInterface $input,
173
        OutputInterface $output,
174
        $bundle,
175
        $format
176
    ) {
177 12
        return array();
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     * Use an overridden generator to make nicer code
183
     *
184
     * @return BundleGenerator
185
     */
186
    protected function createGenerator()
187
    {
188
        return new BundleGenerator();
189
    }
190
}
191