CreateModelCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 23
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 39
ccs 24
cts 24
cp 1
crap 2
rs 9.552
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 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 10
        $this
38 15
            ->setName('create:model')
39 15
            ->setDescription('Creates a new model')
40 15
            ->addArgument(
41 15
                'name',
42 15
                InputArgument::REQUIRED,
43 5
                'Name of the table'
44 15
            )->addOption(
45 15
                'camel',
46 15
                null,
47 15
                InputOption::VALUE_NONE,
48 5
                'Uses the camel case naming convention'
49 15
            )->addOption(
50 15
                'lowercase',
51 15
                null,
52 15
                InputOption::VALUE_NONE,
53 5
                'Keeps the first character of the name to lowercase'
54 10
            );
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')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('name') can also be of type string[]; however, parameter $str of singular() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $fileName = ucfirst(singular(/** @scrutinizer ignore-type */ $input->getArgument('name')));
Loading history...
67
68 12
        $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php';
0 ignored issues
show
Bug introduced by
The constant Rougin\Combustor\Commands\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
70
        $info = [
71 12
            'name' => $fileName,
72 12
            'type' => 'model',
73 4
            'path' => $path
74 8
        ];
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 6
        ];
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