CreateControllerCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 26
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 44
ccs 28
cts 28
cp 1
crap 3
rs 9.504
1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\File;
9
use Rougin\Combustor\Common\Tools;
10
use Rougin\Combustor\Validator\ControllerValidator;
11
use Rougin\Combustor\Generator\ControllerGenerator;
12
13
/**
14
 * Create Controller Command
15
 *
16
 * Generates a Wildfire or Doctrine-based controller for CodeIgniter.
17
 *
18
 * @package Combustor
19
 * @author  Rougin Gutib <[email protected]>
20
 */
21
class CreateControllerCommand extends AbstractCommand
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $command = 'controller';
27
28
    /**
29
     * Executes the command.
30
     *
31
     * @param \Symfony\Component\Console\Input\InputInterface   $input
32
     * @param \Symfony\Component\Console\Output\OutputInterface $output
33
     * @return object|\Symfony\Component\Console\Output\OutputInterface
34
     */
35 21
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 21
        $fileName = ucfirst(plural($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 plural() 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

37
        $fileName = ucfirst(plural(/** @scrutinizer ignore-type */ $input->getArgument('name')));
Loading history...
38
39 21
        if ($input->getOption('keep')) {
40 12
            $fileName = ucfirst($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 ucfirst() 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

40
            $fileName = ucfirst(/** @scrutinizer ignore-type */ $input->getArgument('name'));
Loading history...
41 8
        }
42
43 21
        $path = APPPATH . 'controllers' . 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...
44
45
        $info = [
46 21
            'name' => $fileName,
47 21
            'type' => 'controller',
48 7
            'path' => $path
49 14
        ];
50
51 21
        $validator = new ControllerValidator($input->getOption('camel'), $info);
52
53 21
        if ($validator->fails()) {
54 12
            $message = $validator->getMessage();
55
56 12
            return $output->writeln('<error>' . $message . '</error>');
57
        }
58
59
        $data = [
60 12
            'file' => $info,
61 12
            'isCamel' => $input->getOption('camel'),
62 12
            'name' => $input->getArgument('name'),
63 12
            'title' => strtolower($fileName),
64 12
            'type' => $validator->getLibrary()
65 8
        ];
66
67 12
        $generator = new ControllerGenerator($this->describe, $data);
68
69 12
        $result = $generator->generate();
70 12
        $controller = $this->renderer->render('Controller.tpl', $result);
71 12
        $message = 'The controller "' . $fileName . '" has been created successfully!';
72
73 12
        $file = new File($path);
74
75 12
        $file->putContents($controller);
76 12
        $file->close();
77
78 12
        return $output->writeln('<info>' . $message . '</info>');
79
    }
80
}
81