Passed
Push — devel-3.0 ( 6b6d4a...0e3c8b )
by Rubén
03:12
created

ErrorUtil   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 132
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B showExceptionInView() 0 36 7
A getErrorTypes() 0 37 2
A showErrorInView() 0 18 3
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Util;
26
27
use SP\Core\Acl\AccountPermissionException;
28
use SP\Core\Acl\UnauthorizedPageException;
29
use SP\Core\Exceptions\FileNotFoundException;
30
use SP\Core\Exceptions\SPException;
31
use SP\Mvc\View\Template;
32
use SP\Services\User\UpdatedMasterPassException;
33
34
/**
35
 * Class ErrorUtil
36
 *
37
 * @package SP\Util
38
 */
39
final class ErrorUtil
40
{
41
    /**
42
     * Constantes de errores
43
     */
44
    const ERR_UNAVAILABLE = 0;
45
    const ERR_ACCOUNT_NO_PERMISSION = 1;
46
    const ERR_PAGE_NO_PERMISSION = 2;
47
    const ERR_UPDATE_MPASS = 3;
48
    const ERR_OPERATION_NO_PERMISSION = 4;
49
    const ERR_EXCEPTION = 5;
50
51
    /**
52
     * Establecer la plantilla de error con el código indicado.
53
     *
54
     * @param \SP\Mvc\View\Template $view
55
     * @param \Exception            $e
56
     * @param  string               $replace Template replacement
57
     * @param bool                  $render
58
     */
59
    public static function showExceptionInView(Template $view,
60
                                               \Exception $e,
61
                                               $replace = null,
62
                                               $render = true)
63
    {
64
        if ($replace === null) {
65
            $view->resetTemplates();
66
67
            if ($view->hashContentTemplates()) {
68
                $view->resetContentTemplates();
69
                $view->addContentTemplate('error', Template::PARTIALS_DIR);
70
            } else {
71
                $view->addTemplate('error', Template::PARTIALS_DIR);
72
            }
73
        } else {
74
            if ($view->hashContentTemplates()) {
75
                $view->removeContentTemplate($replace);
76
                $view->addContentTemplate('error', Template::PARTIALS_DIR);
77
            } else {
78
                $view->removeTemplate($replace);
79
                $view->addTemplate('error', Template::PARTIALS_DIR);
80
            }
81
        }
82
83
        switch (get_class($e)) {
84
            case UpdatedMasterPassException::class:
85
                self::showErrorInView($view, self::ERR_UPDATE_MPASS, $render);
86
                break;
87
            case UnauthorizedPageException::class:
88
                self::showErrorInView($view, self::ERR_PAGE_NO_PERMISSION, $render);
89
                break;
90
            case AccountPermissionException::class:
91
                self::showErrorInView($view, self::ERR_ACCOUNT_NO_PERMISSION, $render);
92
                break;
93
            default;
94
                self::showErrorInView($view, self::ERR_EXCEPTION, $render);
95
        }
96
    }
97
98
    /**
99
     * Establecer la plantilla de error con el código indicado.
100
     *
101
     * @param \SP\Mvc\View\Template $view
102
     * @param int                   $type int con el tipo de error
103
     * @param bool                  $render
104
     */
105
    public static function showErrorInView(Template $view, $type, $render = true)
106
    {
107
        $error = self::getErrorTypes($type);
108
109
        $view->append('errors',
110
            [
111
                'type' => SPException::WARNING,
112
                'description' => $error['txt'],
113
                'hint' => $error['hint']
114
            ]);
115
116
        if ($render) {
117
            try {
118
                echo $view->render();
119
            } catch (FileNotFoundException $e) {
120
                processException($e);
121
122
                echo $e->getMessage();
123
            }
124
        }
125
    }
126
127
    /**
128
     * Return error message by type
129
     *
130
     * @param $type
131
     *
132
     * @return mixed
133
     */
134
    protected static function getErrorTypes($type)
135
    {
136
        $errorTypes = [
137
            self::ERR_UNAVAILABLE => [
138
                'txt' => __('Opción no disponible'),
139
                'hint' => __('Consulte con el administrador')
140
            ],
141
            self::ERR_ACCOUNT_NO_PERMISSION => [
142
                'txt' => __('No tiene permisos para acceder a esta cuenta'),
143
                'hint' => __('Consulte con el administrador')
144
            ],
145
            self::ERR_PAGE_NO_PERMISSION => [
146
                'txt' => __('No tiene permisos para acceder a esta página'),
147
                'hint' => __('Consulte con el administrador')
148
            ],
149
            self::ERR_OPERATION_NO_PERMISSION => [
150
                'txt' => __('No tiene permisos para realizar esta operación'),
151
                'hint' => __('Consulte con el administrador')
152
            ],
153
            self::ERR_UPDATE_MPASS => [
154
                'txt' => __('Clave maestra actualizada'),
155
                'hint' => __('Reinicie la sesión para cambiarla')
156
            ],
157
            self::ERR_EXCEPTION => [
158
                'txt' => __('Se ha producido una excepción'),
159
                'hint' => __('Consulte con el administrador')
160
            ]
161
        ];
162
163
        if (!isset($errorTypes[$type])) {
164
            return [
165
                'txt' => __('Se ha producido una excepción'),
166
                'hint' => __('Consulte con el administrador')
167
            ];
168
        }
169
170
        return $errorTypes[$type];
171
    }
172
}