Completed
Push — master ( 0f0d6a...e070b7 )
by Rougin
05:03
created

CreateModelCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

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

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\Combustor\Common\Tools;
11
use Rougin\Combustor\Validator\ModelValidator;
12
use Rougin\Combustor\Generator\ModelGenerator;
13
14
/**
15
 * Create Model Command
16
 *
17
 * Generates a Wildfire or Doctrine-based model for CodeIgniter
18
 * 
19
 * @package Combustor
20
 * @author  Rougin Royce Gutib <[email protected]>
21
 */
22
class CreateModelCommand 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 15
    protected function configure()
43
    {
44 15
        $this
45 15
            ->setName('create:model')
46 15
            ->setDescription('Creates a new model')
47 15
            ->addArgument(
48 15
                'name',
49 15
                InputArgument::REQUIRED,
50
                'Name of the table'
51 15
            )->addOption(
52 15
                'camel',
53 15
                NULL,
54 15
                InputOption::VALUE_NONE,
55
                'Uses the camel case naming convention'
56 15
            )->addOption(
57 15
                'doctrine',
58 15
                NULL,
59 15
                InputOption::VALUE_NONE,
60
                'Generates a model based on Doctrine'
61 15
            )->addOption(
62 15
                'lowercase',
63 15
                NULL,
64 15
                InputOption::VALUE_NONE,
65
                'Keeps the first character of the name to lowercase'
66 15
            )->addOption(
67 15
                'wildfire',
68 15
                NULL,
69 15
                InputOption::VALUE_NONE,
70
                'Generates a model based on Wildfire'
71 15
            );
72 15
    }
73
74
    /**
75
     * Executes the command.
76
     * 
77
     * @param \Symfony\Component\Console\Input\InputInterface   $input
78
     * @param \Symfony\Component\Console\Output\OutputInterface $output
79
     * @return object|\Symfony\Component\Console\Output\OutputInterface
80
     */
81 12
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83 12
        $fileName = ucfirst(singular($input->getArgument('name')));
84
85 12
        $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php';
86
87
        $fileInformation = [
88 12
            'name' => $fileName,
89 12
            'type' => 'model',
90
            'path' => $path
91 12
        ];
92
93 12
        $validator = new ModelValidator(
94 12
            $input->getOption('doctrine'),
95 12
            $input->getOption('wildfire'),
96 12
            $input->getOption('camel'),
97
            $fileInformation
98 12
        );
99
100 12
        if ($validator->fails()) {
101 3
            $message = $validator->getMessage();
102
103 3
            return $output->writeln('<error>' . $message . '</error>');
104
        }
105
106
        $data = [
107 9
            'file' => $fileInformation,
108 9
            'isCamel' => $input->getOption('camel'),
109 9
            'name' => $input->getArgument('name'),
110 9
            'type' => $validator->getLibrary()
111 9
        ];
112
113 9
        $generator = new ModelGenerator($this->describe, $data);
114
115 9
        $result = $generator->generate();
116 9
        $model = $this->renderer->render('Model.template', $result);
117 9
        $message = 'The model "' . $fileName . '" has been created successfully!';
118
119 9
        $file = fopen($path, 'wb');
120 9
        file_put_contents($path, $model);
121 9
        fclose($file);
122
123 9
        return $output->writeln('<info>' . $message . '</info>');
124
    }
125
}
126