GenerateRestControllerCommand::execute()   C
last analyzed

Complexity

Conditions 10
Paths 54

Size

Total Lines 63
Code Lines 39

Duplication

Lines 9
Ratio 14.29 %

Code Coverage

Tests 22
CRAP Score 19.1124

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 9
loc 63
ccs 22
cts 40
cp 0.55
rs 6.3636
cc 10
eloc 39
nc 54
nop 2
crap 19.1124

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
namespace Tpg\ExtjsBundle\Command;
3
4
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
5
use Sensio\Bundle\GeneratorBundle\Command\Validators;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\HttpKernel\Bundle\Bundle;
10
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
11
use Tpg\ExtjsBundle\Generator\RestControllerGenerator;
12
13
class GenerateRestControllerCommand extends GeneratorCommand {
14
15
    /** @var  InputInterface */
16
    protected $input;
17
    /**
18
     * @var OutputInterface
19
     */
20
    protected $output;
21
22 2
    public function configure() {
23 2
        $this
24 2
            ->setDefinition(array(
25 2
                    new InputOption(
26 2
                        'controller',
27 2
                        '',
28 2
                        InputOption::VALUE_REQUIRED,
29
                        'The name of the controller to create'
30 2
                    ),
31 2
                    new InputOption(
32 2
                        'entity',
33 2
                        '',
34 2
                        InputOption::VALUE_REQUIRED,
35
                        "Entity this rest controller will manage"
36 2
                    ),
37 2
                    new InputOption(
38 2
                        'trait',
39 2
                        't',
40 2
                        InputOption::VALUE_NONE,
41
                        "Generate Trait and Rest Controller"
42 2
                    ),
43 2
                    new InputOption(
44 2
                        'mongo',
45 2
                        'm',
46 2
                        InputOption::VALUE_NONE,
47
                        "Generate Rest Controller for Mongo Document"
48 2
                    )
49 2
                ))
50 2
            ->setDescription('Generates a controller')
51 2
            ->setHelp(<<<EOT
52
The <info>generate:rest:controller</info> command helps you generates new FOSRest based controllers inside bundles.
53
54
If you want to specify the controller and entity to generate the controller for,
55
<info>php app/console generate:controller --controller=AcmeBlogBundle:Post --entity=AcmeBlogBundle:Post</info>
56
57
Every generated file is based on a template. There are default templates but they can be overriden by placing custom
58
templates in one of the following locations, by order of priority:
59
60
<info>BUNDLE_PATH/Resources/SensioGeneratorBundle/skeleton/controller
61
APP_PATH/Resources/SensioGeneratorBundle/skeleton/controller</info>
62
63
EOT
64 2
            )
65 2
            ->setName('generate:rest:controller');
66 2
    }
67
68 2
    public function execute(InputInterface $input, OutputInterface $output)
69
    {
70 2
        $this->input = $input;
71 2
        $this->output = $output;
72
73 2
        Validators::validateEntityName($input->getOption('entity'));
74
75
        // in SensioGeneratorBundle:v2.5.0 was BC break, changed method name. commit 59eebd0
76 2
        if (method_exists($this, 'getDialogHelper')) {
77
            $dialog = $this->getDialogHelper();
0 ignored issues
show
Bug introduced by
The method getDialogHelper() does not seem to exist on object<Tpg\ExtjsBundle\C...eRestControllerCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        } else {
79 2
            $dialog = $this->getQuestionHelper();
80
        }
81
82 2
        if ($input->getOption('trait')) {
83
            if (PHP_MAJOR_VERSION < 5 || PHP_MINOR_VERSION < 4) {
84
                throw new \RuntimeException('You need PHP > 5.4 to use trait feature');
85
            }
86
        }
87
88 2
        if (null === $input->getOption('controller')) {
89
            throw new \RuntimeException('The controller option must be provided.');
90
        }
91
92 2
        list($bundle, $controller) = $this->parseShortcutNotation($input->getOption('controller'));
93
        /** @var Bundle $bundle */
94 2 View Code Duplication
        if (is_string($bundle)) {
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...
95 2
            $bundle = Validators::validateBundleName($bundle);
96
97
            try {
98 2
                $bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
99 2
            } catch (\Exception $e) {
100
                $output->writeln(sprintf('<bg=red>Bundle "%s" does not exists.</>', $bundle));
101
            }
102 2
        }
103
104 2
        $dialog->writeSection($output, 'Controller generation: ' . $controller . 'Controller (' . $bundle->getName() . ')');
105
106
        /** @var RestControllerGenerator $generator */
107 2
        $generator = $this->getGenerator($bundle);
108 2
        if ($input->getOption('trait')) {
109
            $output->writeln("<info>Generating Controller with Traits</info>");
110
            $generator->setUseTrait(true);
111
            $generator->generate($bundle,$controller,'','');
112
            $output->writeln("<info>Trait Controller Generated</info>");
113
            $generator->setUseTrait(false);
114
            $generator->setTemplateFile('UseTraitController.php');
115
            try {
116
                $generator->generate($bundle,$controller,'','');
117
                $output->writeln("<info>Controller Generated</info>");
118
            } catch (\RuntimeException $e) {
119
                $output->writeln("<info>Controller Skipped</info>");
120
            }
121
        } else {
122 2
            $generator->generate($bundle,$controller,'','');
123 2
            $output->writeln("<info>Controller Generated</info>");
124
        }
125
126 2
        $output->writeln('Generating the bundle code: <info>OK</info>');
127
128 2
        $dialog->writeGeneratorSummary($output, array());
129
130 2
    }
131
132 2
    protected function createGenerator()
133
    {
134 2
        $generator = new RestControllerGenerator($this->getContainer()->get('filesystem'));
135 2
        list($bundle, $entity) = $this->parseShortcutNotation($this->input->getOption('entity'));
136 2
        $generator->setEntityName($entity);
137 2 View Code Duplication
        if (is_string($bundle)) {
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...
138 2
            $bundle = Validators::validateBundleName($bundle);
139
            try {
140 2
                $bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
141 2
            } catch (\Exception $e) {
142
                $this->output->writeln(sprintf('<bg=red>Bundle "%s" does not exists.</>', $bundle));
143
            }
144 2
        }
145
        /** @todo Check entity is a valid entity */
146 2
        if ($this->input->getOption("mongo")) {
147 1
            $generator->setMongo(true);
148 1
        }
149 2
        $generator->setEntityBundle($bundle);
150 2
        return $generator;
151
    }
152
153 2
    protected function getSkeletonDirs(BundleInterface $bundle = null)
154
    {
155 2
        $dirs = parent::getSkeletonDirs($bundle);
156 2
        $dirs[] = __DIR__.'/../Resources/skeleton';
157 2
        return $dirs;
158
    }
159
160
161 2
    public function parseShortcutNotation($shortcut)
162
    {
163 2
        $entity = str_replace('/', '\\', $shortcut);
164
165 2
        if (false === $pos = strpos($entity, ':')) {
166
            throw new \InvalidArgumentException(sprintf('The controller name must contain a : ("%s" given, expecting something like AcmeBlogBundle:Post)', $entity));
167
        }
168
169 2
        return array(substr($entity, 0, $pos), substr($entity, $pos + 1));
170
    }
171
}
172