Completed
Push — 2.x ( e45de2...5989f2 )
by Grégoire
02:40
created

GenerateCommand::execute()   C

Complexity

Conditions 10
Paths 19

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 16
Bugs 1 Features 6
Metric Value
c 16
b 1
f 6
dl 0
loc 57
rs 6.7123
cc 10
eloc 35
nc 19
nop 2

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 Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\EasyExtendsBundle\Command;
13
14
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
21
22
/**
23
 * Generate Application entities from bundle entities.
24
 *
25
 * @author Thomas Rabaix <[email protected]>
26
 */
27
class GenerateCommand extends ContainerAwareCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('sonata:easy-extends:generate')
36
            ->setHelp(<<<'EOT'
37
The <info>easy-extends:generate:entities</info> command generating a valid bundle structure from a Vendor Bundle.
38
39
  <info>ie: ./app/console sonata:easy-extends:generate SonataUserBundle</info>
40
EOT
41
            );
42
43
        $this->setDescription('Create entities used by Sonata\'s bundles');
44
45
        $this->addArgument('bundle', InputArgument::IS_ARRAY, 'The bundle name to "easy-extends"');
46
        $this->addOption('dest', 'd', InputOption::VALUE_OPTIONAL, 'The base folder where the Application will be created', false);
47
        $this->addOption('namespace', 'ns', InputOption::VALUE_OPTIONAL, 'The namespace for the classes', false);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $destOption = $input->getOption('dest');
56
        if ($destOption) {
57
            $dest = realpath($destOption);
58
            if (false === $dest) {
59
                throw new \RuntimeException(sprintf('The provided destination folder \'%s\' does not exist!', $destOption));
60
            }
61
        } else {
62
            $dest = $this->getContainer()->get('kernel')->getRootDir();
63
        }
64
65
        $namespace = $input->getOption('namespace');
66
        if ($namespace) {
67
            if (!preg_match('/^(?:(?:[[:alnum:]]+|:vendor)\\\\?)+$/', $namespace)) {
68
                throw new \InvalidArgumentException('The provided namespace \'%s\' is not a valid namespace!', $namespace);
69
            }
70
        } else {
71
            $namespace = 'Application\:vendor';
72
        }
73
74
        $configuration = array(
75
            'application_dir' => sprintf('%s%s%s', $dest, DIRECTORY_SEPARATOR, str_replace('\\', DIRECTORY_SEPARATOR, $namespace)),
76
            'namespace' => $namespace,
77
        );
78
79
        $bundleNames = $input->getArgument('bundle');
80
81
        if (empty($bundleNames)) {
82
            $output->writeln('');
83
            $output->writeln('<error>You must provide a bundle name!</error>');
84
            $output->writeln('');
85
            $output->writeln('  Bundles availables :');
86
            /** @var BundleInterface $bundle */
87
            foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
88
                $bundleMetadata = new BundleMetadata($bundle, $configuration);
89
90
                if (!$bundleMetadata->isExtendable()) {
91
                    continue;
92
                }
93
94
                $output->writeln(sprintf('     - %s', $bundle->getName()));
95
            }
96
97
            $output->writeln('');
98
        } else {
99
            foreach ($bundleNames as $bundleName) {
100
                $processed = $this->generate($bundleName, $configuration, $output);
101
102
                if (!$processed) {
103
                    throw new \RuntimeException(sprintf('<error>The bundle \'%s\' does not exist or not defined in the kernel file!</error>', $bundleName));
104
                }
105
            }
106
        }
107
108
        $output->writeln('done!');
109
    }
110
111
    /**
112
     * Generates a bundle entities from a bundle name.
113
     *
114
     * @param string          $bundleName
115
     * @param array           $configuration
116
     * @param OutputInterface $output
117
     *
118
     * @return bool
119
     */
120
    protected function generate($bundleName, array $configuration, $output)
121
    {
122
        $processed = false;
123
124
        /** @var BundleInterface $bundle */
125
        foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
126
            if ($bundle->getName() != $bundleName) {
127
                continue;
128
            }
129
130
            $processed = true;
131
            $bundleMetadata = new BundleMetadata($bundle, $configuration);
132
133
            // generate the bundle file
134
            if (!$bundleMetadata->isExtendable()) {
135
                $output->writeln(sprintf('Ignoring bundle : "<comment>%s</comment>"', $bundleMetadata->getClass()));
136
                continue;
137
            }
138
139
            // generate the bundle file
140
            if (!$bundleMetadata->isValid()) {
141
                $output->writeln(sprintf('%s : <comment>wrong folder structure</comment>', $bundleMetadata->getClass()));
142
                continue;
143
            }
144
145
            $output->writeln(sprintf('Processing bundle : "<info>%s</info>"', $bundleMetadata->getName()));
146
147
            $this->getContainer()->get('sonata.easy_extends.generator.bundle')
148
                ->generate($output, $bundleMetadata);
149
150
            $output->writeln(sprintf('Processing Doctrine ORM : "<info>%s</info>"', $bundleMetadata->getName()));
151
            $this->getContainer()->get('sonata.easy_extends.generator.orm')
152
                ->generate($output, $bundleMetadata);
153
154
            $output->writeln(sprintf('Processing Doctrine ODM : "<info>%s</info>"', $bundleMetadata->getName()));
155
            $this->getContainer()->get('sonata.easy_extends.generator.odm')
156
                ->generate($output, $bundleMetadata);
157
158
            $output->writeln(sprintf('Processing Doctrine PHPCR : "<info>%s</info>"', $bundleMetadata->getName()));
159
            $this->getContainer()->get('sonata.easy_extends.generator.phpcr')
160
                ->generate($output, $bundleMetadata);
161
162
            $output->writeln(sprintf('Processing Serializer config : "<info>%s</info>"', $bundleMetadata->getName()));
163
            $this->getContainer()->get('sonata.easy_extends.generator.serializer')
164
                ->generate($output, $bundleMetadata);
165
166
            $output->writeln('');
167
        }
168
169
        return $processed;
170
    }
171
}
172