Completed
Push — 2.x ( dc834d...91b37b )
by
unknown
02:07
created

GenerateCommand::generate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 51
rs 8.6588
cc 5
eloc 30
nc 5
nop 3

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
/*
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
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $destOption = $input->getOption('dest');
55
        if ($destOption) {
56
            $dest = realpath($destOption);
57
            if (false === $dest) {
58
                $output->writeln('');
59
                $output->writeln(sprintf('<error>The provided destination folder \'%s\' does not exist!</error>', $destOption));
60
61
                return 0;
62
            }
63
        } else {
64
            $dest = $this->getContainer()->get('kernel')->getRootDir();
65
        }
66
67
        $configuration = array(
68
            'application_dir' => sprintf('%s/Application', $dest),
69
        );
70
71
        $bundleNames = $input->getArgument('bundle');
72
73
        if (empty($bundleNames)) {
74
            $output->writeln('');
75
            $output->writeln('<error>You must provide a bundle name!</error>');
76
            $output->writeln('');
77
            $output->writeln('  Bundles availables :');
78
            /** @var BundleInterface $bundle */
79
            foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
80
                $bundleMetadata = new BundleMetadata($bundle, $configuration);
81
82
                if (!$bundleMetadata->isExtendable()) {
83
                    continue;
84
                }
85
86
                $output->writeln(sprintf('     - %s', $bundle->getName()));
87
            }
88
89
            $output->writeln('');
90
91
            return 0;
92
        }
93
94
        foreach ($bundleNames as $bundleName) {
95
            $processed = $this->generate($bundleName, $configuration, $output);
96
97
            if (!$processed) {
98
                $output->writeln(sprintf('<error>The bundle \'%s\' does not exist or not defined in the kernel file!</error>', $bundleName));
99
100
                return -1;
101
            }
102
        }
103
104
        $output->writeln('done!');
105
106
        return 0;
107
    }
108
109
    /**
110
     * Generates a bundle entities from a bundle name.
111
     *
112
     * @param string          $bundleName
113
     * @param array           $configuration
114
     * @param OutputInterface $output
115
     *
116
     * @return bool
117
     */
118
    protected function generate($bundleName, array $configuration, $output)
119
    {
120
        $processed = false;
121
122
        /** @var BundleInterface $bundle */
123
        foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
124
            if ($bundle->getName() != $bundleName) {
125
                continue;
126
            }
127
128
            $processed = true;
129
            $bundleMetadata = new BundleMetadata($bundle, $configuration);
130
131
            // generate the bundle file
132
            if (!$bundleMetadata->isExtendable()) {
133
                $output->writeln(sprintf('Ignoring bundle : "<comment>%s</comment>"', $bundleMetadata->getClass()));
134
                continue;
135
            }
136
137
            // generate the bundle file
138
            if (!$bundleMetadata->isValid()) {
139
                $output->writeln(sprintf('%s : <comment>wrong folder structure</comment>', $bundleMetadata->getClass()));
140
                continue;
141
            }
142
143
            $output->writeln(sprintf('Processing bundle : "<info>%s</info>"', $bundleMetadata->getName()));
144
145
            $this->getContainer()->get('sonata.easy_extends.generator.bundle')
146
                ->generate($output, $bundleMetadata);
147
148
            $output->writeln(sprintf('Processing Doctrine ORM : "<info>%s</info>"', $bundleMetadata->getName()));
149
            $this->getContainer()->get('sonata.easy_extends.generator.orm')
150
                ->generate($output, $bundleMetadata);
151
152
            $output->writeln(sprintf('Processing Doctrine ODM : "<info>%s</info>"', $bundleMetadata->getName()));
153
            $this->getContainer()->get('sonata.easy_extends.generator.odm')
154
                ->generate($output, $bundleMetadata);
155
156
            $output->writeln(sprintf('Processing Doctrine PHPCR : "<info>%s</info>"', $bundleMetadata->getName()));
157
            $this->getContainer()->get('sonata.easy_extends.generator.phpcr')
158
                ->generate($output, $bundleMetadata);
159
160
            $output->writeln(sprintf('Processing Serializer config : "<info>%s</info>"', $bundleMetadata->getName()));
161
            $this->getContainer()->get('sonata.easy_extends.generator.serializer')
162
                ->generate($output, $bundleMetadata);
163
164
            $output->writeln('');
165
        }
166
167
        return $processed;
168
    }
169
}
170