Completed
Push — master ( c45fe6...da9ea2 )
by Rougin
06:15
created

CreateViewCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 5
Bugs 3 Features 0
Metric Value
c 5
b 3
f 0
dl 0
loc 55
ccs 0
cts 36
cp 0
rs 9.7692
cc 3
eloc 34
nc 4
nop 2
crap 12

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 Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
use Rougin\Combustor\Common\Tools;
11
use Rougin\Combustor\Common\Inflector;
12
use Rougin\Combustor\Generator\ViewGenerator;
13
use Rougin\Combustor\Validator\ViewValidator;
14
15
/**
16
 * Create View Command
17
 *
18
 * Creates a list of views for CodeIgniter
19
 * 
20
 * @package Combustor
21
 * @author  Rougin Royce Gutib <[email protected]>
22
 */
23
class CreateViewCommand extends AbstractCommand
24
{
25
    /**
26
     * Checks whether the command is enabled or not in the current environment.
27
     *
28
     * Override this to check for x or y and return false if the command can not
29
     * run properly under the current conditions.
30
     *
31
     * @return bool
32
     */
33
    public function isEnabled()
34
    {
35
        return Tools::isCommandEnabled() && Tools::hasLayout();
36
    }
37
38
    /**
39
     * Set the configurations of the specified command
40
     *
41
     * @return void
42
     */
43
    protected function configure()
44
    {
45
        $this->setName('create:view')
46
            ->setDescription('Create a new view')
47
            ->addArgument(
48
                'name',
49
                InputArgument::REQUIRED,
50
                'Name of the view folder'
51
            )->addOption(
52
                'bootstrap',
53
                NULL,
54
                InputOption::VALUE_NONE,
55
                'Includes the Bootstrap CSS/JS Framework tags'
56
            )->addOption(
57
                'camel',
58
                NULL,
59
                InputOption::VALUE_NONE,
60
                'Uses the camel case naming convention'
61
            )->addOption(
62
                'keep',
63
                NULL,
64
                InputOption::VALUE_NONE,
65
                'Keeps the name to be used'
66
            );
67
    }
68
69
    /**
70
     * Executes the command.
71
     * 
72
     * @param \Symfony\Component\Console\Input\InputInterface   $input
73
     * @param \Symfony\Component\Console\Output\OutputInterface $output
74
     * @return object|\Symfony\Component\Console\Output\OutputInterface
75
     */
76
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78
        $name = Tools::stripTableSchema($input->getArgument('name'));
79
80
        if ( ! $input->getOption('keep')) {
81
            $name = Tools::stripTableSchema(Inflector::plural($input->getArgument('name')));
82
        }
83
84
        $validator = new ViewValidator($name);
85
86
        if ($validator->fails()) {
87
            $message = $validator->getMessage();
88
89
            return $output->writeln('<error>' . $message . '</error>');
90
        }
91
92
        $data = [
93
            'isBootstrap' => $input->getOption('bootstrap'),
94
            'isCamel' => $input->getOption('camel'),
95
            'name' => $name
96
        ];
97
98
        $generator = new ViewGenerator($this->describe, $data);
99
100
        $result = $generator->generate();
101
102
        $results = [
103
            'create' => $this->renderer->render('Views/create.template', $result),
104
            'edit' => $this->renderer->render('Views/edit.template', $result),
105
            'index' => $this->renderer->render('Views/index.template', $result),
106
            'show' => $this->renderer->render('Views/show.template', $result)
107
        ];
108
109
        $filePath = APPPATH . 'views/' . $name;
110
111
        $file = fopen($filePath . '/create.php', 'wb');
112
        file_put_contents($filePath . '/create.php', $results['create']);
113
        fclose($file);
114
115
        $file = fopen($filePath . '/edit.php', 'wb');
116
        file_put_contents($filePath . '/edit.php', $results['edit']);
117
        fclose($file);
118
119
        $file = fopen($filePath . '/index.php', 'wb');
120
        file_put_contents($filePath . '/index.php', $results['index']);
121
        fclose($file);
122
123
        $file = fopen($filePath . '/show.php', 'wb');
124
        file_put_contents($filePath . '/show.php', $results['show']);
125
        fclose($file);
126
127
        $message = 'The views folder "' . $name . '" has been created successfully!';
128
129
        return $output->writeln('<info>' . $message . '</info>');
130
    }
131
}
132