]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Handlers/GaletteError.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Handlers / GaletteError.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Error handler that overrides slim's one
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Handlers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since 2017-02-25
35 */
36
37 namespace Galette\Handlers;
38
39 use Analog\Analog;
40 use Slim\Views\Smarty;
41 use Psr\Http\Message\ServerRequestInterface;
42
43 /**
44 * Error handler
45 *
46 * @category Handlers
47 * @name GaletteError
48 * @package Galette
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2017 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since 2017-02-25
54 */
55 trait GaletteError
56 {
57 protected $view;
58
59 /**
60 * Constructor
61 *
62 * @param Smarty $view View instance
63 * @param bool $displayErrorDetails Set to true to display full details
64 */
65 public function __construct(Smarty $view, $displayErrorDetails = false)
66 {
67 $this->view = $view;
68 $this->displayErrorDetails = (bool)$displayErrorDetails;
69 }
70
71
72 /**
73 * Write to the error log whether displayErrorDetails is false or not
74 *
75 * @param \Exception|\Throwable $throwable Error
76 * @overrides \Slim\Handlers\AbstractError::writeToErrorLog()
77 *
78 * @return void
79 */
80 protected function writeToErrorLog($throwable)
81 {
82 $message = 'Galette error:' . PHP_EOL;
83 $message .= $this->renderThrowableAsText($throwable);
84 while ($throwable = $throwable->getPrevious()) {
85 $message .= PHP_EOL . 'Previous error:' . PHP_EOL;
86 $message .= $this->renderThrowableAsText($throwable);
87 }
88
89 $this->logError($message);
90 }
91
92 /**
93 * Wraps the error_log function so that this can be easily tested
94 *
95 * @param string $message Error Message
96 *
97 * @return void
98 */
99 protected function logError($message)
100 {
101 parent::logError($message);
102 Analog::log(
103 $message,
104 Analog::ERROR
105 );
106 }
107
108 /**
109 * Determine which content type we know about is wanted using Accept header
110 *
111 * Note: This method is a bare-bones implementation designed specifically for
112 * Slim's error handling requirements. Consider a fully-feature solution such
113 * as willdurand/negotiation for any other situation.
114 *
115 * @param ServerRequestInterface $request Request
116 * @return string
117 */
118 protected function determineContentType(ServerRequestInterface $request)
119 {
120 if ($request->isXhr()) {
121 //get error as JSON for XHR request; more lisible
122 return 'application/json';
123 }
124 return parent::determineContentType($request);
125 }
126 }