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

CreateControllerCommand::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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