Completed
Push — master ( 267f53...0f0d6a )
by Rougin
04:34
created

CreateControllerCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 3

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 49
ccs 31
cts 31
cp 1
rs 9.2258
cc 3
eloc 31
nc 4
nop 2
crap 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\Validator\ControllerValidator;
12
use Rougin\Combustor\Generator\ControllerGenerator;
13
14
/**
15
 * Create Controller Command
16
 *
17
 * Generates a Wildfire or Doctrine-based controller for CodeIgniter.
18
 * 
19
 * @package Combustor
20
 * @author  Rougin Royce Gutib <[email protected]>
21
 */
22
class CreateControllerCommand 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();
35
    }
36
37
    /**
38
     * Sets the configurations of the specified command.
39
     *
40
     * @return void
41
     */
42 24
    protected function configure()
43
    {
44 24
        $this
45 24
            ->setName('create:controller')
46 24
            ->setDescription('Creates a new controller')
47 24
            ->addArgument(
48 24
                'name',
49 24
                InputArgument::REQUIRED,
50
                'Name of the table'
51 24
            )->addOption(
52 24
                'camel',
53 24
                NULL,
54 24
                InputOption::VALUE_NONE,
55
                'Uses the camel case naming convention'
56 24
            )->addOption(
57 24
                'doctrine',
58 24
                NULL,
59 24
                InputOption::VALUE_NONE,
60
                'Generates a controller based on Doctrine'
61 24
            )->addOption(
62 24
                'keep',
63 24
                NULL,
64 24
                InputOption::VALUE_NONE,
65
                'Keeps the name to be used'
66 24
            )->addOption(
67 24
                'lowercase',
68 24
                NULL,
69 24
                InputOption::VALUE_NONE,
70
                'Keeps the first character of the name to lowercase'
71 24
            )->addOption(
72 24
                'wildfire',
73 24
                NULL,
74 24
                InputOption::VALUE_NONE,
75
                'Generates a controller based on Wildfire'
76 24
            );
77 24
    }
78
79
    /**
80
     * Executes the command.
81
     * 
82
     * @param \Symfony\Component\Console\Input\InputInterface   $input
83
     * @param \Symfony\Component\Console\Output\OutputInterface $output
84
     * @return object|\Symfony\Component\Console\Output\OutputInterface
85
     */
86 21
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88 21
        $fileName = ucfirst(plural($input->getArgument('name')));
89
90 21
        if ($input->getOption('keep')) {
91 12
            $fileName = ucfirst($input->getArgument('name'));
92 12
        }
93
94 21
        $path = APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $fileName . '.php';
95
96
        $fileInformation = [
97 21
            'name' => $fileName,
98 21
            'type' => 'controller',
99
            'path' => $path
100 21
        ];
101
102 21
        $validator = new ControllerValidator(
103 21
            $input->getOption('doctrine'),
104 21
            $input->getOption('wildfire'),
105 21
            $input->getOption('camel'),
106
            $fileInformation
107 21
        );
108
109 21
        if ($validator->fails()) {
110 12
            $message = $validator->getMessage();
111
112 12
            return $output->writeln('<error>' . $message . '</error>');
113
        }
114
115
        $data = [
116 12
            'file' => $fileInformation,
117 12
            'isCamel' => $input->getOption('camel'),
118 12
            'name' => $input->getArgument('name'),
119 12
            'title' => strtolower($fileName),
120 12
            'type' => $validator->getLibrary()
121 12
        ];
122
123 12
        $generator = new ControllerGenerator($this->describe, $data);
124
125 12
        $result = $generator->generate();
126 12
        $controller = $this->renderer->render('Controller.template', $result);
127 12
        $message = 'The controller "' . $fileName . '" has been created successfully!';
128
129 12
        $file = fopen($path, 'wb');
130 12
        file_put_contents($path, $controller);
131 12
        fclose($file);
132
133 12
        return $output->writeln('<info>' . $message . '</info>');
134
    }
135
}
136