KumbiaException   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleException() 0 32 4
A cleanBuffer() 0 6 2
A setHeader() 0 10 3
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
 *
12
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
13
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
14
 */
15
16
/**
17
 * Clase principal para el manejo de excepciones.
18
 *
19
 * @category   Kumbia
20
 */
21
class KumbiaException extends Exception
22
{
23
    /**
24
     * View de error de la Excepción.
25
     *
26
     * @var string|null
27
     */
28
    protected $view;
29
30
    /**
31
     * Error 404 para los siguientes views.
32
     *
33
     * @var array
34
     */
35
    protected static $view404 = array('no_controller', 'no_action', 'num_params', 'no_view');
36
37
    /**
38
     * Path del template de exception.
39
     *
40
     * @var string
41
     */
42
    protected $template = 'views/templates/exception.phtml';
43
44
    /**
45
     * Constructor de la clase;.
46
     *
47
     * @param string $message mensaje
48
     * @param string $view    vista que se mostrara
49
     */
50
    public function __construct($message, $view = 'exception')
51
    {
52
        $this->view = $view;
53
        parent::__construct($message);
54
    }
55
56
    /**
57
     * Maneja las excepciones no capturadas.
58
     *
59
     * @param Exception|KumbiaException $e
60
     * */
61
    public static function handleException($e)
62
    {
63
        self::setHeader($e);
64
        //TODO quitar el extract, que el view pida los que necesite
65
        extract(Router::get(), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by joanhey
\Router::get() cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
66
        // Registra la autocarga de helpers
67
        spl_autoload_register('kumbia_autoload_helper', true, true);
68
69
        $Controller = Util::camelcase($controller);
70
        ob_start();
71
        if (PRODUCTION) { //TODO: añadir error 500.phtml
72
            self::cleanBuffer();
73
            include APP_PATH.'views/_shared/errors/404.phtml';
74
75
            return;
76
        }
77
        if ($e instanceof self) {
78
            $view = $e->view;
79
            $tpl = $e->template;
80
        } else {
81
            $view = 'exception';
82
            $tpl = 'views/templates/exception.phtml';
83
        }
84
        //Fix problem with action name in REST
85
        $action = $e->getMessage() ?: $action;
86
87
        include CORE_PATH."views/errors/{$view}.phtml";
88
89
        $content = ob_get_clean();
90
        self::cleanBuffer();
91
        include CORE_PATH.$tpl;
92
    }
93
94
    /**
95
     * cleanBuffer
96
     * termina los buffers abiertos.
97
     */
98
    private static function cleanBuffer()
99
    {
100
        while (ob_get_level()) {
101
            ob_end_clean();
102
        }
103
    }
104
105
    /**
106
     * Añade la cabezera de error http.
107
     *
108
     * @param Exception $e
109
     * */
110
    private static function setHeader($e)
111
    {
112
        if ($e instanceof self && in_array($e->view, self::$view404)) {
113
            http_response_code(404);
114
115
            return;
116
        }
117
        http_response_code(500);
118
        //TODO: mover a los views
119
    }
120
}
121