Completed
Push — master ( db44e7...3e1fbd )
by Tom
04:39
created

CreateCommand::createModuleDirectories()   C

Complexity

Conditions 8
Paths 35

Size

Total Lines 67
Code Lines 42

Duplication

Lines 12
Ratio 17.91 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 12
loc 67
rs 6.6523
cc 8
eloc 42
nc 35
nop 2

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
namespace N98\Magento\Command\Developer\Module;
4
5
use InvalidArgumentException;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use N98\Util\Console\Helper\TwigHelper;
8
use RuntimeException;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Create a magento module skeleton
16
 */
17
class CreateCommand extends AbstractMagentoCommand
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $twigVars = array();
23
24
    /**
25
     * @var string
26
     */
27
    protected $baseFolder;
28
29
    /**
30
     * @var string
31
     */
32
    protected $moduleDirectory;
33
34
    /**
35
     * @var string
36
     */
37
    protected $vendorNamespace;
38
39
    /**
40
     * @var string
41
     */
42
    protected $moduleName;
43
44
    /**
45
     * @var string
46
     */
47
    protected $codePool;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $modmanMode = false;
53
54
    protected function configure()
55
    {
56
        $this
57
            ->setName('dev:module:create')
58
            ->addArgument('vendorNamespace', InputArgument::REQUIRED, 'Namespace (your company prefix)')
59
            ->addArgument('moduleName', InputArgument::REQUIRED, 'Name of your module.')
60
            ->addArgument('codePool', InputArgument::OPTIONAL, 'Codepool (local,community)', 'local')
61
            ->addOption('add-blocks', null, InputOption::VALUE_NONE, 'Adds blocks')
62
            ->addOption('add-helpers', null, InputOption::VALUE_NONE, 'Adds helpers')
63
            ->addOption('add-models', null, InputOption::VALUE_NONE, 'Adds models')
64
            ->addOption('add-setup', null, InputOption::VALUE_NONE, 'Adds SQL setup')
65
            ->addOption('add-all', null, InputOption::VALUE_NONE, 'Adds blocks, helpers and models')
66
            ->addOption('modman', null, InputOption::VALUE_NONE, 'Create all files in folder with a modman file.')
67
            ->addOption('add-readme', null, InputOption::VALUE_NONE, 'Adds a readme.md file to generated module')
68
            ->addOption('add-composer', null, InputOption::VALUE_NONE, 'Adds a composer.json file to generated module')
69
            ->addOption('author-name', null, InputOption::VALUE_OPTIONAL, 'Author for readme.md or composer.json')
70
            ->addOption('author-email', null, InputOption::VALUE_OPTIONAL, 'Author for readme.md or composer.json')
71
            ->addOption('description', null, InputOption::VALUE_OPTIONAL, 'Description for readme.md or composer.json')
72
            ->setDescription('Create and register a new magento module.');
73
    }
74
75
    /**
76
     * @param InputInterface  $input
77
     * @param OutputInterface $output
78
     *
79
     * @return int|void
80
     * @throws InvalidArgumentException
81
     */
82
    protected function execute(InputInterface $input, OutputInterface $output)
83
    {
84
        $this->modmanMode = $input->getOption('modman');
85
        if ($input->getOption('add-all')) {
86
            $input->setOption('add-blocks', true);
87
            $input->setOption('add-helpers', true);
88
            $input->setOption('add-models', true);
89
            $input->setOption('add-setup', true);
90
            $input->setOption('add-readme', true);
91
            $input->setOption('add-composer', true);
92
        }
93
        if (!$this->modmanMode) {
94
            $this->detectMagento($output);
95
        }
96
        $this->baseFolder = __DIR__ . '/../../../../../../res/module/create';
97
98
        $this->vendorNamespace = ucfirst($input->getArgument('vendorNamespace'));
99
        $this->moduleName = ucfirst($input->getArgument('moduleName'));
100
        $this->codePool = $input->getArgument('codePool');
101
        if (!in_array($this->codePool, array('local', 'community'))) {
102
            throw new InvalidArgumentException('Code pool must "community" or "local"');
103
        }
104
        $this->initView($input);
105
        $this->createModuleDirectories($input, $output);
106
        $this->writeEtcModules($output);
107
        $this->writeModuleConfig($output);
108
        $this->writeReadme($input, $output);
109
        if ($this->modmanMode) {
110
            $this->writeModmanFile($output);
111
        }
112
        $this->writeComposerConfig($input, $output);
113
        $this->addAdditionalFiles($output);
114
    }
115
116
    protected function initView(InputInterface $input)
117
    {
118
        $this->twigVars = array(
119
            'vendorNamespace' => $this->vendorNamespace,
120
            'moduleName'      => $this->moduleName,
121
            'codePool'        => $this->codePool,
122
            'createBlocks'    => $input->getOption('add-blocks'),
123
            'createModels'    => $input->getOption('add-models'),
124
            'createHelpers'   => $input->getOption('add-helpers'),
125
            'createSetup'     => $input->getOption('add-setup'),
126
            'authorName'      => $input->getOption('author-name'),
127
            'authorEmail'     => $input->getOption('author-email'),
128
            'description'     => $input->getOption('description'),
129
        );
130
    }
131
132
    /**
133
     * @param InputInterface  $input
134
     * @param OutputInterface $output
135
     */
136
    protected function createModuleDirectories(InputInterface $input, OutputInterface $output)
137
    {
138
        if ($this->modmanMode) {
139
            $modManDir = $this->vendorNamespace . '_' . $this->moduleName . '/src';
140
            if (file_exists($modManDir)) {
141
                throw new RuntimeException('Module already exists. Stop.');
142
            }
143
            mkdir($modManDir, 0777, true);
144
            $this->_magentoRootFolder = './' . $modManDir;
145
            mkdir($this->_magentoRootFolder . '/app/etc/modules', 0777, true);
146
        }
147
        $moduleDir = sprintf(
148
            '%s/app/code/%s/%s/%s',
149
            $this->_magentoRootFolder,
150
            $this->codePool,
151
            $this->vendorNamespace,
152
            $this->moduleName
153
        );
154
155
        if (file_exists($moduleDir)) {
156
            throw new RuntimeException('Module already exists. Stop.');
157
        }
158
        $this->moduleDirectory = $moduleDir;
159
        mkdir($this->moduleDirectory, 0777, true);
160
        $output->writeln('<info>Created directory: <comment>' . $this->moduleDirectory . '<comment></info>');
161
162
        // Add etc folder
163
        mkdir($this->moduleDirectory . '/etc');
164
        $output->writeln('<info>Created directory: <comment>' . $this->moduleDirectory . '/etc<comment></info>');
165
166
        // Add blocks folder
167 View Code Duplication
        if ($input->getOption('add-blocks')) {
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...
168
            mkdir($this->moduleDirectory . '/Block');
169
            $output->writeln(
170
                '<info>Created directory: <comment>' . $this->moduleDirectory . '/Block' . '<comment></info>'
171
            );
172
        }
173
174
        // Add helpers folder
175 View Code Duplication
        if ($input->getOption('add-helpers')) {
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...
176
            mkdir($this->moduleDirectory . '/Helper');
177
            $output->writeln(
178
                '<info>Created directory: <comment>' . $this->moduleDirectory . '/Helper' . '<comment></info>'
179
            );
180
        }
181
182
        // Add models folder
183 View Code Duplication
        if ($input->getOption('add-models')) {
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...
184
            mkdir($this->moduleDirectory . '/Model');
185
            $output->writeln(
186
                '<info>Created directory: <comment>' . $this->moduleDirectory . '/Model' . '<comment></info>'
187
            );
188
        }
189
190
        // Create SQL and Data folder
191
        if ($input->getOption('add-setup')) {
192
            $sqlSetupFolder = $this->moduleDirectory . '/sql/' . strtolower($this->vendorNamespace) . '_' .
193
                strtolower($this->moduleName) . '_setup';
194
            mkdir($sqlSetupFolder, 0777, true);
195
            $output->writeln('<info>Created directory: <comment>' . $sqlSetupFolder . '<comment></info>');
196
197
            $dataSetupFolder = $this->moduleDirectory . '/data/' . strtolower($this->vendorNamespace) . '_' .
198
                strtolower($this->moduleName) . '_setup';
199
            mkdir($dataSetupFolder, 0777, true);
200
            $output->writeln('<info>Created directory: <comment>' . $dataSetupFolder . '<comment></info>');
201
        }
202
    }
203
204
    protected function writeEtcModules(OutputInterface $output)
205
    {
206
        $outFile = sprintf(
207
            '%s/app/etc/modules/%s_%s.xml',
208
            $this->_magentoRootFolder,
209
            $this->vendorNamespace,
210
            $this->moduleName
211
        );
212
213
        /** @var $helper TwigHelper */
214
        $helper = $this->getHelper('twig');
215
        $buffer = $helper->render('dev/module/create/app/etc/modules/definition.twig', $this->twigVars);
216
        $size   = file_put_contents($outFile, $buffer);
217
218
        $output->writeln('<info>Created file: <comment>' . $outFile . '<comment> (' . $size . ' bytes)</info>');
219
    }
220
221 View Code Duplication
    protected function writeModuleConfig(OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
222
    {
223
        $outFile = $this->moduleDirectory . '/etc/config.xml';
224
        file_put_contents(
225
            $outFile,
226
            $this->getHelper('twig')->render('dev/module/create/app/etc/modules/config.twig', $this->twigVars)
227
        );
228
229
        $output->writeln('<info>Created file: <comment>' . $outFile . '<comment></info>');
230
    }
231
232 View Code Duplication
    protected function writeModmanFile(OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
233
    {
234
        $outFile = $this->_magentoRootFolder . '/../modman';
235
        file_put_contents(
236
            $outFile,
237
            $this->getHelper('twig')->render('dev/module/create/modman.twig', $this->twigVars)
238
        );
239
        $output->writeln('<info>Created file: <comment>' . $outFile . '<comment></info>');
240
    }
241
242
    /**
243
     * Write standard readme
244
     *
245
     * TODO: Make author name / company URL and more configurable
246
     *
247
     * @see https://raw.github.com/sprankhub/Magento-Extension-Sample-Readme/master/readme.markdown
248
     *
249
     * @param InputInterface $input
250
     * @param OutputInterface $output
251
     */
252 View Code Duplication
    protected function writeReadme($input, $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
253
    {
254
        if (!$input->getOption('add-readme')) {
255
            return;
256
        }
257
        if ($this->modmanMode) {
258
            $outFile = $this->_magentoRootFolder . '/../readme.md';
259
        } else {
260
            $outFile = $this->moduleDirectory . '/etc/readme.md';
261
        }
262
        file_put_contents(
263
            $outFile,
264
            $this->getHelper('twig')->render('dev/module/create/app/etc/modules/readme.twig', $this->twigVars)
265
        );
266
        $output->writeln('<info>Created file: <comment>' . $outFile . '<comment></info>');
267
    }
268
269
    /**
270
     * Write composer.json
271
     *
272
     * @param InputInterface $input
273
     * @param OutputInterface $output
274
     */
275 View Code Duplication
    protected function writeComposerConfig(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
276
    {
277
        if (!$input->getOption('add-composer')) {
278
            return;
279
        }
280
        if ($this->modmanMode) {
281
            $outFile = $this->_magentoRootFolder . '/../composer.json';
282
        } else {
283
            $outFile = $this->moduleDirectory . '/etc/composer.json';
284
        }
285
        file_put_contents(
286
            $outFile,
287
            $this->getHelper('twig')->render('dev/module/create/composer.twig', $this->twigVars)
288
        );
289
        $output->writeln('<info>Created file: <comment>' . $outFile . '<comment></info>');
290
    }
291
292
    protected function addAdditionalFiles(OutputInterface $output)
293
    {
294
        $config = $this->getCommandConfig();
295
        if (isset($config['additionalFiles']) && is_array($config['additionalFiles'])) {
296
            foreach ($config['additionalFiles'] as $template => $outFileRaw) {
297
                $outFile = $this->_getOutfile($outFileRaw);
298
                if (!is_dir(dirname($outFile))) {
299
                    mkdir(dirname($outFile), 0777, true);
300
                }
301
                file_put_contents(
302
                    $outFile,
303
                    $this->getHelper('twig')->render($template, $this->twigVars)
304
                );
305
                $output->writeln('<info>Created file: <comment>' . $outFile . '<comment></info>');
306
            }
307
        }
308
    }
309
310
    /**
311
     * @param string $filename
312
     * @return string
313
     */
314
    private function _getOutfile($filename)
315
    {
316
        $pathes = array(
317
            'rootDir'   => $this->_magentoRootFolder,
318
            'moduleDir' => $this->moduleDirectory,
319
        );
320
321
        return $this->getHelper('twig')->renderString($filename, array_merge($this->twigVars, $pathes));
322
    }
323
}
324