Completed
Push — master ( 4714ce...2b7987 )
by Rougin
04:26
created

CreateViewCommand::isEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\File;
9
use Rougin\Combustor\Common\Tools;
10
use Rougin\Combustor\Generator\ViewGenerator;
11
use Rougin\Combustor\Validator\ViewValidator;
12
13
/**
14
 * Create View Command
15
 *
16
 * Creates a list of views for CodeIgniter
17
 * 
18
 * @package Combustor
19
 * @author  Rougin Royce Gutib <[email protected]>
20
 */
21
class CreateViewCommand extends AbstractCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $command = 'view';
27
28
    /**
29
     * Executes the command.
30
     * 
31
     * @param \Symfony\Component\Console\Input\InputInterface   $input
32
     * @param \Symfony\Component\Console\Output\OutputInterface $output
33
     * @return object|\Symfony\Component\Console\Output\OutputInterface
34
     */
35 9
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 9
        $name = Tools::stripTableSchema(plural($input->getArgument('name')));
38
39 9
        if ($input->getOption('keep')) {
40 3
            $name = Tools::stripTableSchema($input->getArgument('name'));
41 3
        }
42
43 9
        $validator = new ViewValidator($name);
44
45 9
        if ($validator->fails()) {
46 3
            $message = $validator->getMessage();
47
48 3
            return $output->writeln('<error>' . $message . '</error>');
49
        }
50
51
        $data = [
52 9
            'isBootstrap' => $input->getOption('bootstrap'),
53 9
            'isCamel' => $input->getOption('camel'),
54 9
            'name' => $input->getArgument('name')
55 9
        ];
56
57 9
        $generator = new ViewGenerator($this->describe, $data);
58
59 9
        $result = $generator->generate();
60
61
        $results = [
62 9
            'create' => $this->renderer->render('Views/create.tpl', $result),
63 9
            'edit' => $this->renderer->render('Views/edit.tpl', $result),
64 9
            'index' => $this->renderer->render('Views/index.tpl', $result),
65 9
            'show' => $this->renderer->render('Views/show.tpl', $result)
66 9
        ];
67
68 9
        $filePath = APPPATH . 'views/' . $name;
69
70 9
        $create = new File($filePath . '/create.php');
71 9
        $edit = new File($filePath . '/edit.php');
72 9
        $index = new File($filePath . '/index.php');
73 9
        $show = new File($filePath . '/show.php');
74
75 9
        $create->putContents($results['create']);
76 9
        $edit->putContents($results['edit']);
77 9
        $index->putContents($results['index']);
78 9
        $show->putContents($results['show']);
79
80 9
        $create->close();
81 9
        $edit->close();
82 9
        $index->close();
83 9
        $show->close();
84
85 9
        $message = 'The views folder "' . $name . '" has been created successfully!';
86
87 9
        return $output->writeln('<info>' . $message . '</info>');
88
    }
89
}
90