Completed
Push — master ( 4714ce...2b7987 )
by Rougin
04:26
created

CreateModelCommand::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
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\File;
11
use Rougin\Combustor\Common\Tools;
12
use Rougin\Combustor\Validator\ModelValidator;
13
use Rougin\Combustor\Generator\ModelGenerator;
14
15
/**
16
 * Create Model Command
17
 *
18
 * Generates a Wildfire or Doctrine-based model for CodeIgniter
19
 * 
20
 * @package Combustor
21
 * @author  Rougin Royce Gutib <[email protected]>
22
 */
23
class CreateModelCommand extends AbstractCommand
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $command = 'model';
29
30
    /**
31
     * Sets the configurations of the specified command.
32
     *
33
     * @return void
34
     */
35 15
    protected function configure()
36
    {
37 15
        $this
38 15
            ->setName('create:model')
39 15
            ->setDescription('Creates a new model')
40 15
            ->addArgument(
41 15
                'name',
42 15
                InputArgument::REQUIRED,
43
                'Name of the table'
44 15
            )->addOption(
45 15
                'camel',
46 15
                NULL,
47 15
                InputOption::VALUE_NONE,
48
                'Uses the camel case naming convention'
49 15
            )->addOption(
50 15
                'lowercase',
51 15
                NULL,
52 15
                InputOption::VALUE_NONE,
53
                'Keeps the first character of the name to lowercase'
54 15
            );
55 15
    }
56
57
    /**
58
     * Executes the command.
59
     * 
60
     * @param \Symfony\Component\Console\Input\InputInterface   $input
61
     * @param \Symfony\Component\Console\Output\OutputInterface $output
62
     * @return object|\Symfony\Component\Console\Output\OutputInterface
63
     */
64 12
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66 12
        $fileName = ucfirst(singular($input->getArgument('name')));
67
68 12
        $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php';
69
70
        $info = [
71 12
            'name' => $fileName,
72 12
            'type' => 'model',
73
            'path' => $path
74 12
        ];
75
76 12
        $validator = new ModelValidator($input->getOption('camel'), $info);
77
78 12
        if ($validator->fails()) {
79 3
            $message = $validator->getMessage();
80
81 3
            return $output->writeln('<error>' . $message . '</error>');
82
        }
83
84
        $data = [
85 9
            'file' => $info,
86 9
            'isCamel' => $input->getOption('camel'),
87 9
            'name' => $input->getArgument('name'),
88 9
            'type' => $validator->getLibrary()
89 9
        ];
90
91 9
        $generator = new ModelGenerator($this->describe, $data);
92
93 9
        $result = $generator->generate();
94 9
        $model = $this->renderer->render('Model.tpl', $result);
95 9
        $message = 'The model "' . $fileName . '" has been created successfully!';
96
97 9
        $file = new File($path);
98
99 9
        $file->putContents($model);
100 9
        $file->close();
101
102 9
        return $output->writeln('<info>' . $message . '</info>');
103
    }
104
}
105