ScaffoldController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 25.29 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 22
loc 87
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 4 1
A crear() 12 17 3
A editar() 10 19 3
A borrar() 0 8 2
A ver() 0 4 1

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
/**
4
 * Controlador base para la construcción de CRUD para modelos rápidamente
5
 *
6
 * @category Kumbia
7
 * @package Controller
8
 */
9
class ScaffoldController extends AdminController
10
{
11
    /** @var string Carpeta en views/_shared/scaffolds/ */
12
    public $scaffold = 'kumbia';
13
    /** @var string Nombre del modelo en CamelCase */
14
    public $model = '';
15
16
    /**
17
     * Resultados paginados
18
     * 
19
     * @param int $page  Página a mostrar
20
     */
21
    public function index($page = 1)
22
    {
23
        $this->data = (new $this->model)->paginate("page: $page", 'order: id desc');
24
    }
25
26
    /**
27
     * Crea un Registro
28
     */
29
    public function crear()
30
    {
31 View Code Duplication
        if (Input::hasPost($this->model)) {
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...
32
33
            $obj = new $this->model;
34
            //En caso que falle la operación de guardar
35
            if (!$obj->save(Input::post($this->model))) {
36
                Flash::error('Falló Operación');
37
                //se hacen persistente los datos en el formulario
38
                $this->{$this->model} = $obj;
39
                return;
40
            }
41
            return Redirect::to();
42
        }
43
        // Sólo es necesario para el autoForm
44
        $this->{$this->model} = new $this->model;
45
    }
46
47
    /**
48
     * Edita un Registro
49
     * 
50
     * @param int $id  Idendificador del registro
51
     */
52
    public function editar($id)
53
    {
54
        View::select('crear');
55
56
        //se verifica si se ha enviado via POST los datos
57 View Code Duplication
        if (Input::hasPost($this->model)) {
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...
58
            $obj = new $this->model;
59
            if (!$obj->update(Input::post($this->model))) {
60
                Flash::error('Falló Operación');
61
                //se hacen persistente los datos en el formulario
62
                $this->{$this->model} = Input::post($this->model);
63
            } else {
64
                return Redirect::to();
65
            }
66
        }
67
68
        //Aplicando la autocarga de objeto, para comenzar la edición
69
        $this->{$this->model} = (new $this->model)->find((int) $id);
70
    }
71
72
    /**
73
     * Borra un Registro
74
     * 
75
     * @param int $id Identificador de registro
76
     */
77
    public function borrar($id)
78
    {
79
        if (!(new $this->model)->delete((int) $id)) {
80
            Flash::error('Falló Operación');
81
        }
82
        //enrutando al index para listar los articulos
83
        Redirect::to();
84
    }
85
86
    /**
87
     * Ver un Registro
88
     * 
89
     * @param int $id Identificador de registro
90
     */
91
    public function ver($id)
92
    {
93
        $this->data = (new $this->model)->find_first((int) $id);
94
    }
95
}