Completed
Push — 1.0 ( a4c55b...057858 )
by joanhey
02:35
created

ModelConsole::create()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 40
Code Lines 20

Duplication

Lines 7
Ratio 17.5 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 7
nop 2
dl 7
loc 40
rs 6.7272
c 0
b 0
f 0
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.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 * @package    Console
17
 * @copyright  Copyright (c) 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
21
/**
22
 * Consola para manejar modelos
23
 *
24
 * @category   Kumbia
25
 * @package    Console
26
 */
27
class ModelConsole
28
{
29
30
    /**
31
     * Comando de consola para crear un modelo
32
     *
33
     * @param array $params parametros nombrados de la consola
34
     * @param string $model modelo
35
     * @throw KumbiaException
36
     */
37
    public function create($params, $model)
38
    {
39
        // nombre de archivo
40
        $file = APP_PATH . 'models';
41
42
        // obtiene el path
43
        $path = explode('/', trim($model, '/'));
44
45
        // obtiene el nombre de modelo
46
        $model_name = array_pop($path);
47
48 View Code Duplication
        if (count($path)) {
0 ignored issues
show
Duplication introduced by
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 .= "/$model_name.php";
56
57
        // si no existe o se sobreescribe
58
        if (!is_file($file) ||
59
                Console::input("El modelo existe, �desea sobrescribirlo? (s/n): ", array('s', 'n')) == 's') {
60
61
            // nombre de clase
62
            $class = Util::camelcase($model_name);
63
64
            // codigo de modelo
65
            ob_start();
66
            include __DIR__ . '/generators/model.php';
67
            $code = '<?php' . PHP_EOL . ob_get_clean();
68
69
            // genera el archivo
70
            if (file_put_contents($file, $code)) {
71
                echo "-> Creado modelo $model_name en: $file" . PHP_EOL;
72
            } else {
73
                throw new KumbiaException("No se ha logrado crear el archivo \"$file\"");
74
            }
75
        }
76
    }
77
78
    /**
79
     * Comando de consola para eliminar un modelo
80
     *
81
     * @param array $params parametros nombrados de la consola
82
     * @param string $model modelo
83
     * @throw KumbiaException
84
     */
85
    public function delete($params, $model)
0 ignored issues
show
Unused Code introduced by
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...
86
    {
87
        // nombre de archivo
88
        $file = APP_PATH . 'models/' . trim($model, '/');
89
90
        // si es un directorio
91 View Code Duplication
        if (is_dir($file)) {
0 ignored issues
show
Duplication introduced by
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...
92
            $success = FileUtil::rmdir($file);
93
        } else {
94
            // entonces es un archivo
95
            $file = "$file.php";
96
            $success = unlink($file);
97
        }
98
99
        // mensaje
100
        if ($success) {
101
            echo "-> Eliminado: $file" . PHP_EOL;
102
        } else {
103
            throw new KumbiaException("No se ha logrado eliminar \"$file\"");
104
        }
105
    }
106
107
}