Completed
Push — master ( 0f0d6a...e070b7 )
by Rougin
05:03
created

CreateViewCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 3

Importance

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