Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

CreateViewCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96.43%

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 0
loc 109
ccs 54
cts 56
cp 0.9643
rs 10
c 8
b 3
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 2
B configure() 0 25 1
A execute() 0 55 3
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 3
    public function isEnabled()
34
    {
35 3
        return Tools::isCommandEnabled() && Tools::hasLayout();
36
    }
37
38
    /**
39
     * Set the configurations of the specified command
40
     *
41
     * @return void
42
     */
43 6
    protected function configure()
44
    {
45 6
        $this->setName('create:view')
46 6
            ->setDescription('Create a new view')
47 6
            ->addArgument(
48 6
                'name',
49 6
                InputArgument::REQUIRED,
50
                'Name of the view folder'
51 6
            )->addOption(
52 6
                'bootstrap',
53 6
                NULL,
54 6
                InputOption::VALUE_NONE,
55
                'Includes the Bootstrap CSS/JS Framework tags'
56 6
            )->addOption(
57 6
                'camel',
58 6
                NULL,
59 6
                InputOption::VALUE_NONE,
60
                'Uses the camel case naming convention'
61 6
            )->addOption(
62 6
                'keep',
63 6
                NULL,
64 6
                InputOption::VALUE_NONE,
65
                'Keeps the name to be used'
66 6
            );
67 6
    }
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 3
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78 3
        $name = Tools::stripTableSchema($input->getArgument('name'));
79
80 3
        if ( ! $input->getOption('keep')) {
81 3
            $name = Tools::stripTableSchema(Inflector::plural($input->getArgument('name')));
82 3
        }
83
84 3
        $validator = new ViewValidator($name);
85
86 3
        if ($validator->fails()) {
87
            $message = $validator->getMessage();
88
89
            return $output->writeln('<error>' . $message . '</error>');
90
        }
91
92
        $data = [
93 3
            'isBootstrap' => $input->getOption('bootstrap'),
94 3
            'isCamel' => $input->getOption('camel'),
95
            'name' => $name
96 3
        ];
97
98 3
        $generator = new ViewGenerator($this->describe, $data);
99
100 3
        $result = $generator->generate();
101
102
        $results = [
103 3
            'create' => $this->renderer->render('Views/create.template', $result),
104 3
            'edit' => $this->renderer->render('Views/edit.template', $result),
105 3
            'index' => $this->renderer->render('Views/index.template', $result),
106 3
            'show' => $this->renderer->render('Views/show.template', $result)
107 3
        ];
108
109 3
        $filePath = APPPATH . 'views/' . $name;
110
111 3
        $file = fopen($filePath . '/create.php', 'wb');
112 3
        file_put_contents($filePath . '/create.php', $results['create']);
113 3
        fclose($file);
114
115 3
        $file = fopen($filePath . '/edit.php', 'wb');
116 3
        file_put_contents($filePath . '/edit.php', $results['edit']);
117 3
        fclose($file);
118
119 3
        $file = fopen($filePath . '/index.php', 'wb');
120 3
        file_put_contents($filePath . '/index.php', $results['index']);
121 3
        fclose($file);
122
123 3
        $file = fopen($filePath . '/show.php', 'wb');
124 3
        file_put_contents($filePath . '/show.php', $results['show']);
125 3
        fclose($file);
126
127 3
        $message = 'The views folder "' . $name . '" has been created successfully!';
128
129 3
        return $output->writeln('<info>' . $message . '</info>');
130
    }
131
}
132