Completed
Push — master ( c45fe6...da9ea2 )
by Rougin
06:15
created

CreateModelCommand::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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