Completed
Push — master ( a4b34c...4714ce )
by Rougin
02:31
created

CreateControllerCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 26.8 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 2 Features 1
Metric Value
wmc 5
c 9
b 2
f 1
lcom 1
cbo 9
dl 26
loc 97
ccs 51
cts 51
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
B configure() 26 26 1
B execute() 0 45 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ControllerValidator;
13
use Rougin\Combustor\Generator\ControllerGenerator;
14
15
/**
16
 * Create Controller Command
17
 *
18
 * Generates a Wildfire or Doctrine-based controller for CodeIgniter.
19
 * 
20
 * @package Combustor
21
 * @author  Rougin Royce Gutib <[email protected]>
22
 */
23
class CreateControllerCommand extends AbstractCommand
24
{
25
    /**
26
     * Checks whether the command is enabled or not in the current environment.
27
     *
28
     * @return bool
29
     */
30
    public function isEnabled()
31
    {
32
        return Tools::isCommandEnabled();
33 6
    }
34
35 6
    /**
36
     * Sets the configurations of the specified command.
37
     *
38
     * @return void
39
     */
40 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $this
43 24
            ->setName('create:controller')
44
            ->setDescription('Creates a new controller')
45 24
            ->addArgument(
46 24
                'name',
47 24
                InputArgument::REQUIRED,
48 24
                'Name of the table'
49 24
            )->addOption(
50 24
                'camel',
51
                NULL,
52 24
                InputOption::VALUE_NONE,
53 24
                'Uses the camel case naming convention'
54 24
            )->addOption(
55 24
                'keep',
56
                NULL,
57 24
                InputOption::VALUE_NONE,
58 24
                'Keeps the name to be used'
59 24
            )->addOption(
60 24
                'lowercase',
61
                NULL,
62 24
                InputOption::VALUE_NONE,
63 24
                'Keeps the first character of the name to lowercase'
64 24
            );
65 24
    }
66
67 24
    /**
68 24
     * Executes the command.
69 24
     * 
70 24
     * @param \Symfony\Component\Console\Input\InputInterface   $input
71
     * @param \Symfony\Component\Console\Output\OutputInterface $output
72 24
     * @return object|\Symfony\Component\Console\Output\OutputInterface
73 24
     */
74 24
    protected function execute(InputInterface $input, OutputInterface $output)
75 24
    {
76
        $fileName = ucfirst(plural($input->getArgument('name')));
77 24
78 24
        if ($input->getOption('keep')) {
79
            $fileName = ucfirst($input->getArgument('name'));
80
        }
81
82
        $path = APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $fileName . '.php';
83
84
        $info = [
85
            'name' => $fileName,
86
            'type' => 'controller',
87 21
            'path' => $path
88
        ];
89 21
90
        $validator = new ControllerValidator($input->getOption('camel'), $info);
91 21
92 12
        if ($validator->fails()) {
93 12
            $message = $validator->getMessage();
94
95 21
            return $output->writeln('<error>' . $message . '</error>');
96
        }
97
98 21
        $data = [
99 21
            'file' => $info,
100
            'isCamel' => $input->getOption('camel'),
101 21
            'name' => $input->getArgument('name'),
102
            'title' => strtolower($fileName),
103 21
            'type' => $validator->getLibrary()
104 21
        ];
105 21
106 21
        $generator = new ControllerGenerator($this->describe, $data);
107
108 21
        $result = $generator->generate();
109
        $controller = $this->renderer->render('Controller.tpl', $result);
110 21
        $message = 'The controller "' . $fileName . '" has been created successfully!';
111 12
112
        $file = new File($path);
113 12
114
        $file->putContents($controller);
115
        $file->close();
116
117 12
        return $output->writeln('<info>' . $message . '</info>');
118 12
    }
119
}
120