ScaffoldController::editar()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 10
Ratio 55.56 %

Importance

Changes 0
Metric Value
dl 10
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
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
    public function index($page=1)
17
    {
18
        $this->data = (new $this->model)->paginate("page: $page", 'order: id desc');
19
    }
20
21
    /**
22
     * Crea un Registro
23
     */
24
    public function crear()
25
    {
26 View Code Duplication
        if (Input::hasPost($this->model)) {
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...
27
28
            $obj = new $this->model;
29
            //En caso que falle la operación de guardar
30
            if (!$obj->save(Input::post($this->model))) {
31
                Flash::error('Falló Operación');
32
                //se hacen persistente los datos en el formulario
33
                $this->{$this->model} = $obj;
34
                return;
35
            }
36
            return Redirect::to();
0 ignored issues
show
Bug introduced by
Are you sure the usage of Redirect::to() targeting Redirect::to() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37
        }
38
        // Sólo es necesario para el autoForm
39
        $this->{$this->model} = new $this->model;
40
    }
41
42
    /**
43
     * Edita un Registro
44
     */
45
    public function editar($id)
46
    {
47
        View::select('crear');
48
49
        //se verifica si se ha enviado via POST los datos
50 View Code Duplication
        if (Input::hasPost($this->model)) {
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...
51
            $obj = new $this->model;
52
            if (!$obj->update(Input::post($this->model))) {
53
                Flash::error('Falló Operación');
54
                //se hacen persistente los datos en el formulario
55
                $this->{$this->model} = Input::post($this->model);
56
            } else {
57
                return Redirect::to();
0 ignored issues
show
Bug introduced by
Are you sure the usage of Redirect::to() targeting Redirect::to() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
            }
59
        }
60
61
        //Aplicando la autocarga de objeto, para comenzar la edición
62
        $this->{$this->model} = (new $this->model)->find((int) $id);
63
    }
64
65
    /**
66
     * Borra un Registro
67
     */
68
    public function borrar($id)
69
    {
70
        if (!(new $this->model)->delete((int) $id)) {
71
            Flash::error('Falló Operación');
72
        }
73
        //enrutando al index para listar los articulos
74
        Redirect::to();
75
    }
76
77
    /**
78
     * Ver un Registro
79
     */
80
    public function ver($id)
81
    {
82
        $this->data = (new $this->model)->find_first((int) $id);
83
    }
84
85
}
86