ControllerConsole   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 12.28 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 14
loc 114
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 7 56 9
B delete() 7 38 6

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
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Console
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Consola para manejar controladores
19
 *
20
 * @category   Kumbia
21
 * @package    Console
22
 */
23
class ControllerConsole
24
{
25
26
    /**
27
     * Comando de consola para crear un controlador
28
     *
29
     * @param array $params parametros nombrados de la consola
30
     * @param string $controller controlador
31
     * @throw KumbiaException
32
     */
33
    public function create($params, $controller)
34
    {
35
        // nombre de archivo
36
        $file = APP_PATH . 'controllers';
37
38
        // limpia el path de controller
39
        $clean_path = trim($controller, '/');
40
41
        // obtiene el path
42
        $path = explode('/', $clean_path);
43
44
        // obtiene el nombre de controlador
45
        $controller_name = array_pop($path);
46
47
        // si se agrupa el controlador en un directorio
48 View Code Duplication
        if (count($path)) {
0 ignored issues
show
Duplication introduced by joanhey
This code seems to be duplicated across 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...
49
            $dir = implode('/', $path);
50
            $file .= "/$dir";
51
            if (!is_dir($file) && !FileUtil::mkdir($file)) {
52
                throw new KumbiaException("No se ha logrado crear el directorio \"$file\"");
53
            }
54
        }
55
        $file .= "/{$controller_name}_controller.php";
56
57
        // si no existe o se sobreescribe
58
        if (!is_file($file) ||
59
                Console::input("El controlador existe, ¿desea sobrescribirlo? (s/n): ", array('s', 'n')) == 's') {
60
61
            // nombre de clase
62
            $class = Util::camelcase($controller_name);
63
64
            // codigo de controlador
65
            ob_start();
66
            include __DIR__ . '/generators/controller.php';
67
            $code = '<?php' . PHP_EOL . ob_get_clean();
68
69
            // genera el archivo
70
            if (file_put_contents($file, $code)) {
71
                echo "-> Creado controlador $controller_name en: $file" . PHP_EOL;
72
            } else {
73
                throw new KumbiaException("No se ha logrado crear el archivo \"$file\"");
74
            }
75
76
            // directorio para vistas
77
            $views_dir = APP_PATH . "views/$clean_path";
78
79
            //si el directorio no existe
80
            if (!is_dir($views_dir)) {
81
                if (FileUtil::mkdir($views_dir)) {
82
                    echo "-> Creado directorio para vistas: $views_dir" . PHP_EOL;
83
                } else {
84
                    throw new KumbiaException("No se ha logrado crear el directorio \"$views_dir\"");
85
                }
86
            }
87
        }
88
    }
89
90
    /**
91
     * Comando de consola para eliminar un controlador
92
     *
93
     * @param array $params parametros nombrados de la consola
94
     * @param string $controller controlador
95
     * @throw KumbiaException
96
     */
97
    public function delete($params, $controller)
0 ignored issues
show
Unused Code introduced by Emilio Silveira
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        // path limpio al controlador
100
        $clean_path = trim($controller, '/');
101
102
        // nombre de archivo
103
        $file = APP_PATH . "controllers/$clean_path";
104
105
        // si es un directorio
106 View Code Duplication
        if (is_dir($file)) {
0 ignored issues
show
Duplication introduced by joanhey
This code seems to be duplicated across 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...
107
            $success = FileUtil::rmdir($file);
108
        } else {
109
            // entonces es un archivo
110
            $file = "{$file}_controller.php";
111
            $success = unlink($file);
112
        }
113
114
        // mensaje
115
        if ($success) {
116
            echo "-> Eliminado: $file" . PHP_EOL;
117
        } else {
118
            throw new KumbiaException("No se ha logrado eliminar \"$file\"");
119
        }
120
121
        // directorio para vistas
122
        $views_dir = APP_PATH . "views/$clean_path";
123
124
        // intenta eliminar el directorio de vistas
125
        if (is_dir($views_dir)
126
                && Console::input('¿Desea eliminar el directorio de vistas? (s/n): ', array('s', 'n')) == 's') {
127
128
            if (!FileUtil::rmdir($views_dir)) {
129
                throw new KumbiaException("No se ha logrado eliminar \"$views_dir\"");
130
            }
131
132
            echo "-> Eliminado: $views_dir" . PHP_EOL;
133
        }
134
    }
135
136
}