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

CreateModelCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96.3%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 104
ccs 52
cts 54
cp 0.963
rs 10
c 6
b 2
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
B configure() 0 31 1
B execute() 0 44 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 3
    public function isEnabled()
35
    {
36 3
        return Tools::isCommandEnabled();
37
    }
38
39
    /**
40
     * Sets the configurations of the specified command.
41
     *
42
     * @return void
43
     */
44 9
    protected function configure()
45
    {
46 9
        $this
47 9
            ->setName('create:model')
48 9
            ->setDescription('Creates a new model')
49 9
            ->addArgument(
50 9
                'name',
51 9
                InputArgument::REQUIRED,
52
                'Name of the model'
53 9
            )->addOption(
54 9
                'camel',
55 9
                NULL,
56 9
                InputOption::VALUE_NONE,
57
                'Uses the camel case naming convention'
58 9
            )->addOption(
59 9
                'doctrine',
60 9
                NULL,
61 9
                InputOption::VALUE_NONE,
62
                'Generates a model based on Doctrine'
63 9
            )->addOption(
64 9
                'lowercase',
65 9
                NULL,
66 9
                InputOption::VALUE_NONE,
67
                'Keeps the first character of the name to lowercase'
68 9
            )->addOption(
69 9
                'wildfire',
70 9
                NULL,
71 9
                InputOption::VALUE_NONE,
72
                'Generates a model based on Wildfire'
73 9
            );
74 9
    }
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 6
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85 6
        $fileName = ucfirst($input->getArgument('name'));
86
87 6
        $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php';
88
89
        $fileInformation = [
90 6
            'name' => $fileName,
91 6
            'type' => 'model',
92
            'path' => $path
93 6
        ];
94
95 6
        $validator = new ModelValidator(
96 6
            $input->getOption('doctrine'),
97 6
            $input->getOption('wildfire'),
98 6
            $input->getOption('camel'),
99
            $fileInformation
100 6
        );
101
102 6
        if ($validator->fails()) {
103
            $message = $validator->getMessage();
104
105
            return $output->writeln('<error>' . $message . '</error>');
106
        }
107
108
        $data = [
109 6
            'file' => $fileInformation,
110 6
            'isCamel' => $input->getOption('camel'),
111 6
            'name' => $input->getArgument('name'),
112 6
            'type' => $validator->getLibrary()
113 6
        ];
114
115 6
        $generator = new ModelGenerator($this->describe, $data);
116
117 6
        $result = $generator->generate();
118 6
        $model = $this->renderer->render('Model.template', $result);
119 6
        $message = 'The model "' . $fileName . '" has been created successfully!';
120
121 6
        $file = fopen($path, 'wb');
122 6
        file_put_contents($path, $model);
123 6
        fclose($file);
124
125 6
        return $output->writeln('<info>' . $message . '</info>');
126
    }
127
}
128