BundleGenerator::generate()   F
last analyzed

Complexity

Conditions 13
Paths 960

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 2.2008
c 0
b 0
f 0
cc 13
nc 960
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
namespace Admingenerator\GeneratorBundle\Generator;
4
5
use Sensio\Bundle\GeneratorBundle\Generator\Generator as BaseBundleGenerator;
6
use Sensio\Bundle\GeneratorBundle\Model\Bundle;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * Generates an admin bundle.
11
 *
12
 * @author Cedric LOMBARDOT
13
 */
14
class BundleGenerator extends BaseBundleGenerator
15
{
16
    /**
17
     * @var Filesystem
18
     */
19
    private $filesystem;
20
21
    /**
22
     * @var string
23
     */
24
    private $skeletonDir;
25
26
    /**
27
     * @var string
28
     */
29
    protected $generator;
30
31
    /**
32
     * @var string
33
     */
34
    protected $prefix;
35
36
    /**
37
     * @var array
38
     */
39
    protected $actions = array(
40
        'New'  => array('views' => array(
41
            'index',
42
            'form',
43
        )),
44
        'List' => array('views' => array(
45
            'index',
46
            'results',
47
            'filters',
48
            'row'
49
        )),
50
        'Excel' => array('views' => array()),
51
        'Edit' => array('views' => array(
52
            'index',
53
            'form',
54
        )),
55
        'Show' => array('views' => array('index')),
56
        'Actions' => array('views' => array('index'))
57
    );
58
59
    /**
60
     * @var array
61
     */
62
    protected $forms = array('New', 'Filters', 'Edit');
63
64
    /**
65
     * @param string $skeletonDir
66
     */
67
    public function __construct(Filesystem $filesystem, $skeletonDir)
68
    {
69
        $this->filesystem = $filesystem;
70
        $this->skeletonDir = $skeletonDir;
71
        if (method_exists($this, 'setSkeletonDirs')) {
72
            $this->setSkeletonDirs($this->skeletonDir);
0 ignored issues
show
Documentation introduced by
$this->skeletonDir is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        }
74
    }
75
76
    /**
77
     * @param string $generator
78
     */
79
    public function setGenerator($generator)
80
    {
81
        $this->generator = $generator;
82
    }
83
84
    /**
85
     * @param string $prefix
86
     */
87
    public function setPrefix($prefix)
88
    {
89
        $this->prefix = $prefix;
90
    }
91
92
    /**
93
     * @param Bundle $bundle
94
     * @param string $modelName
95
     */
96
    public function generate(Bundle $bundle, $modelName)
97
    {
98
        $dir = $bundle->getTargetDirectory();
99
100
        // Retrieves model folder depending on chosen Model Manager
101
        $modelFolder = '';
102
        switch ($this->generator) {
103
            case 'propel':
104
                $modelFolder = 'Model';
105
                break;
106
            case 'doctrine':
107
                $modelFolder = 'Entity';
108
                break;
109
            case 'doctrine_orm':
110
                $modelFolder = 'Document';
111
                break;
112
        }
113
114
        if (false === strpos($bundle->getNamespace(), '\\')) {
115
            $bundleName = $bundle->getNamespace();
116
            $namespacePrefix = null;
117
        } else {
118
            list( $namespacePrefix, $bundleName) = explode('\\', $bundle->getNamespace(), 2);
119
        }
120
121
        $parameters = array(
122
            'namespace'        => $bundle->getNamespace(),
123
            'bundle'           => $bundle->getName(),
124
            'generator'        => 'admingenerator.generator.'.$this->generator,
125
            'namespace_prefix' => $namespacePrefix,
126
            'bundle_name'      => $bundleName,
127
            'model_folder'     => $modelFolder,
128
            'model_name'       => $modelName,
129
            'prefix'           => ucfirst($this->prefix),
130
        );
131
132
        if (!file_exists($dir.'/'.$bundle->getName().'.php')) {
133
            $this->renderGeneratedFile('Bundle.php.twig', $dir.'/'.$bundle->getName().'.php', $parameters);
134
        }
135
136
        foreach ($this->actions as $action => $actionProperties) {
137
            $parameters['action'] = $action;
138
139
            $controllerFile = $dir.'/Controller/'
140
                .($this->prefix ? ucfirst($this->prefix).'/' : '').$action.'Controller.php';
141
            $this->copyPreviousFile($controllerFile);
142
            $this->renderGeneratedFile(
143
                'DefaultController.php.twig',
144
                $controllerFile,
145
                $parameters
146
            );
147
148
            foreach ($actionProperties['views'] as $templateName) {
149
                $templateFile = $dir.'/Resources/views/'.ucfirst($this->prefix).$action.'/'.$templateName.'.html.twig';
150
                $this->copyPreviousFile($templateFile);
151
                $this->renderGeneratedFile(
152
                    'default_view.html.twig',
153
                    $templateFile,
154
                    $parameters + array('view' => $templateName)
155
                );
156
            }
157
        }
158
159
        foreach ($this->forms as $form) {
160
            $parameters['form'] = $form;
161
162
            $formFile = $dir.'/Form/Type/'.($this->prefix ? ucfirst($this->prefix).'/' : '').$form.'Type.php';
163
            $this->copyPreviousFile($formFile);
164
            $this->renderGeneratedFile(
165
                'DefaultType.php.twig',
166
                $formFile,
167
                $parameters
168
            );
169
        }
170
171
        $optionsFile = $dir.'/Form/Type/'.($this->prefix ? ucfirst($this->prefix).'/' : '').'Options.php';
172
        $this->copyPreviousFile($optionsFile);
173
        $this->renderGeneratedFile(
174
            'DefaultOptions.php.twig',
175
            $optionsFile,
176
            $parameters
177
        );
178
179
        $generatorFile = $dir.'/Resources/config/'.($this->prefix ? ucfirst($this->prefix).'-' : '').'generator.yml';
180
        $this->copyPreviousFile($generatorFile);
181
        $this->renderGeneratedFile(
182
            'generator.yml.twig',
183
            $generatorFile,
184
            $parameters
185
        );
186
    }
187
188
    /**
189
     * @param string $template
190
     * @param string $target
191
     */
192
    protected function renderGeneratedFile($template, $target, array $parameters)
193
    {
194
        $this->renderFile($template, $target, $parameters);
195
    }
196
197
    /**
198
     * @param string $oldname
199
     */
200
    protected function copyPreviousFile($oldname)
201
    {
202
        if (file_exists($oldname)) {
203
            $newname = $oldname.'~';
204
205
            // Find unused copy name
206
            if (file_exists($newname)) {
207
                $key = 0;
208
                do {
209
                    $key++;
210
                } while (file_exists($oldname.'~'.$key));
211
212
                $newname = $oldname.'~'.$key;
213
            }
214
215
            // Create new copy
216
            rename($oldname, $newname);
217
        }
218
    }
219
}
220