. */ namespace SP\Util; use Exception; use SP\Domain\Core\Acl\AccountPermissionException; use SP\Domain\Core\Acl\UnauthorizedPageException; use SP\Domain\Core\Exceptions\FileNotFoundException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Services\UpdatedMasterPassException; use SP\Mvc\View\Template; use SP\Mvc\View\TemplateInterface; use function SP\processException; /** * Class ErrorUtil * * @package SP\Util */ final class ErrorUtil { /** * Constantes de errores */ public const ERR_UNAVAILABLE = 0; public const ERR_ACCOUNT_NO_PERMISSION = 1; public const ERR_PAGE_NO_PERMISSION = 2; public const ERR_UPDATE_MPASS = 3; public const ERR_OPERATION_NO_PERMISSION = 4; public const ERR_EXCEPTION = 5; /** * Establecer la plantilla de error con el código indicado. * * @param TemplateInterface $view * @param Exception $e * @param string|null $replace Template replacement * @param bool $render */ public static function showExceptionInView( TemplateInterface $view, Exception $e, ?string $replace = null, bool $render = true ): void { switch (get_class($e)) { case UpdatedMasterPassException::class: self::showErrorInView($view, self::ERR_UPDATE_MPASS, $render, $replace); break; case UnauthorizedPageException::class: self::showErrorInView($view, self::ERR_PAGE_NO_PERMISSION, $render, $replace); break; case AccountPermissionException::class: self::showErrorInView($view, self::ERR_ACCOUNT_NO_PERMISSION, $render, $replace); break; default; self::showErrorInView($view, self::ERR_EXCEPTION, $render, $replace); } } /** * Establecer la plantilla de error con el código indicado. * * @param TemplateInterface $view * @param int $type int con el tipo de error * @param bool $render * @param string|null $replace */ public static function showErrorInView( TemplateInterface $view, int $type, bool $render = true, ?string $replace = null ): void { self::addErrorTemplate($view, $replace); $error = self::getErrorTypes($type); $view->append( 'errors', [ 'type' => SPException::WARNING, 'description' => $error['txt'], 'hint' => $error['hint'], ] ); if ($render) { try { echo $view->render(); } catch (FileNotFoundException $e) { processException($e); echo $e->getMessage(); } } } private static function addErrorTemplate(TemplateInterface $view, string $replace = null): void { if ($replace === null) { $view->resetTemplates(); if ($view->hasContentTemplates()) { $view->resetContentTemplates(); $view->addContentTemplate('error', Template::PARTIALS_DIR); } else { $view->addTemplate('error', Template::PARTIALS_DIR); } } elseif ($view->hasContentTemplates()) { $view->removeContentTemplate($replace); $view->addContentTemplate('error', Template::PARTIALS_DIR); } else { $view->removeTemplate($replace); $view->addTemplate('error', Template::PARTIALS_DIR); } } /** * Return error message by type */ protected static function getErrorTypes(int $type): array { $errorTypes = [ self::ERR_UNAVAILABLE => [ 'txt' => __('Option unavailable'), 'hint' => __('Please contact to the administrator'), ], self::ERR_ACCOUNT_NO_PERMISSION => [ 'txt' => __('You don\'t have permission to access this account'), 'hint' => __('Please contact to the administrator'), ], self::ERR_PAGE_NO_PERMISSION => [ 'txt' => __('You don\'t have permission to access this page'), 'hint' => __('Please contact to the administrator'), ], self::ERR_OPERATION_NO_PERMISSION => [ 'txt' => __('You don\'t have permission to do this operation'), 'hint' => __('Please contact to the administrator'), ], self::ERR_UPDATE_MPASS => [ 'txt' => __('Master password updated'), 'hint' => __('Please, restart the session for update it'), ], self::ERR_EXCEPTION => [ 'txt' => __('An exception occured'), 'hint' => __('Please contact to the administrator'), ], ]; return $errorTypes[$type] ?? [ 'txt' => __('An exception occured'), 'hint' => __('Please contact to the administrator'), ]; } }