Passed
Push — master ( 53bb62...283d0c )
by Rougin
08:28
created

CreateControllerCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 58
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 44 3
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 9
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 9
        $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 9
        if ($input->getOption('keep')) {
40 3
            $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 2
        }
42
43 9
        $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 9
            'name' => $fileName,
47 9
            'type' => 'controller',
48 3
            'path' => $path
49 6
        ];
50
51 9
        $validator = new ControllerValidator($input->getOption('camel'), $info);
52
53 9
        if ($validator->fails()) {
54 6
            $message = $validator->getMessage();
55
56 6
            return $output->writeln('<error>' . $message . '</error>');
57
        }
58
59
        $data = [
60 3
            'file' => $info,
61 3
            'isCamel' => $input->getOption('camel'),
62 3
            'name' => $input->getArgument('name'),
63 3
            'title' => strtolower($fileName),
64 3
            'type' => $validator->getLibrary()
65 2
        ];
66
67 3
        $generator = new ControllerGenerator($this->describe, $data);
68
69 3
        $result = $generator->generate();
70 3
        $controller = $this->renderer->render('Controller.tpl', $result);
71
        $message = 'The controller "' . $fileName . '" has been created successfully!';
72
73
        $file = new File($path);
74
75
        $file->putContents($controller);
76
        $file->close();
77
78
        return $output->writeln('<info>' . $message . '</info>');
79
    }
80
}
81