Passed
Push — devel-3.0 ( e49526...a37b49 )
by Rubén
04:05
created

ErrorUtil::addErrorTemplate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 2
dl 0
loc 18
rs 9.7998
c 0
b 0
f 0
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
        switch (get_class($e)) {
65
            case UpdatedMasterPassException::class:
66
                self::showErrorInView($view, self::ERR_UPDATE_MPASS, $render, $replace);
0 ignored issues
show
Bug introduced by
It seems like $replace can also be of type string; however, parameter $replace of SP\Util\ErrorUtil::showErrorInView() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
                self::showErrorInView($view, self::ERR_UPDATE_MPASS, $render, /** @scrutinizer ignore-type */ $replace);
Loading history...
67
                break;
68
            case UnauthorizedPageException::class:
69
                self::showErrorInView($view, self::ERR_PAGE_NO_PERMISSION, $render, $replace);
70
                break;
71
            case AccountPermissionException::class:
72
                self::showErrorInView($view, self::ERR_ACCOUNT_NO_PERMISSION, $render, $replace);
73
                break;
74
            default;
75
                self::showErrorInView($view, self::ERR_EXCEPTION, $render, $replace);
76
        }
77
    }
78
79
    /**
80
     * Establecer la plantilla de error con el código indicado.
81
     *
82
     * @param \SP\Mvc\View\Template $view
83
     * @param int                   $type int con el tipo de error
84
     * @param bool                  $render
85
     * @param null                  $replace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $replace is correct as it would always require null to be passed?
Loading history...
86
     */
87
    public static function showErrorInView(Template $view, $type, $render = true, $replace = null)
88
    {
89
        self::addErrorTemplate($view, $replace);
90
91
        $error = self::getErrorTypes($type);
92
93
        $view->append('errors',
94
            [
95
                'type' => SPException::WARNING,
96
                'description' => $error['txt'],
97
                'hint' => $error['hint']
98
            ]);
99
100
        if ($render) {
101
            try {
102
                echo $view->render();
103
            } catch (FileNotFoundException $e) {
104
                processException($e);
105
106
                echo $e->getMessage();
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param Template    $view
113
     * @param string|null $replace
114
     */
115
    private static function addErrorTemplate(Template $view, string $replace = null)
116
    {
117
        if ($replace === null) {
118
            $view->resetTemplates();
119
120
            if ($view->hashContentTemplates()) {
121
                $view->resetContentTemplates();
122
                $view->addContentTemplate('error', Template::PARTIALS_DIR);
123
            } else {
124
                $view->addTemplate('error', Template::PARTIALS_DIR);
125
            }
126
        } else {
127
            if ($view->hashContentTemplates()) {
128
                $view->removeContentTemplate($replace);
129
                $view->addContentTemplate('error', Template::PARTIALS_DIR);
130
            } else {
131
                $view->removeTemplate($replace);
132
                $view->addTemplate('error', Template::PARTIALS_DIR);
133
            }
134
        }
135
    }
136
137
    /**
138
     * Return error message by type
139
     *
140
     * @param $type
141
     *
142
     * @return mixed
143
     */
144
    protected static function getErrorTypes($type)
145
    {
146
        $errorTypes = [
147
            self::ERR_UNAVAILABLE => [
148
                'txt' => __('Opción no disponible'),
149
                'hint' => __('Consulte con el administrador')
150
            ],
151
            self::ERR_ACCOUNT_NO_PERMISSION => [
152
                'txt' => __('No tiene permisos para acceder a esta cuenta'),
153
                'hint' => __('Consulte con el administrador')
154
            ],
155
            self::ERR_PAGE_NO_PERMISSION => [
156
                'txt' => __('No tiene permisos para acceder a esta página'),
157
                'hint' => __('Consulte con el administrador')
158
            ],
159
            self::ERR_OPERATION_NO_PERMISSION => [
160
                'txt' => __('No tiene permisos para realizar esta operación'),
161
                'hint' => __('Consulte con el administrador')
162
            ],
163
            self::ERR_UPDATE_MPASS => [
164
                'txt' => __('Clave maestra actualizada'),
165
                'hint' => __('Reinicie la sesión para cambiarla')
166
            ],
167
            self::ERR_EXCEPTION => [
168
                'txt' => __('Se ha producido una excepción'),
169
                'hint' => __('Consulte con el administrador')
170
            ]
171
        ];
172
173
        if (!isset($errorTypes[$type])) {
174
            return [
175
                'txt' => __('Se ha producido una excepción'),
176
                'hint' => __('Consulte con el administrador')
177
            ];
178
        }
179
180
        return $errorTypes[$type];
181
    }
182
}