Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
CRAP | |
27.66% |
13 / 47 |
| ErrorManager | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
223.26 | |
27.66% |
13 / 47 |
| setContentPolicy($contentPolicy) | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
| getContentPolicy() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setErrorPriority($priority) | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| registerErrorHandler( $f = null) | |
0.00% |
0 / 1 |
10.27 | |
75.00% |
9 / 12 |
|||
| anonymous function() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| registerViewer( array $viewer, $errorCode = 0) | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getErrorHandler() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| serializeHttpProblem(HttpProblem $problem) | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| render( Exception $e) | |
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 16 |
|||
| <?php | |
| namespace BOTK\Core; | |
| use \ErrorException, \Exception; | |
| use BOTK\Core\Models\HttpProblem; | |
| /* | |
| * It is a singleton | |
| */ | |
| class ErrorManager extends Singleton | |
| { | |
| protected $contentPolicy = '\\BOTK\\Core\\Representations\\Error'; | |
| protected $priority = 0; | |
| protected $errorHandler = null; | |
| public function setContentPolicy($contentPolicy) | |
| { | |
| if (!is_subclass_of($contentPolicy,'\\BOTK\\Core\\Representations\\Error')) { | |
| // do not use erromanagement inside erro management :-) | |
| die("$contentPolicy is not a valid Content Negotiation Policy"); | |
| } | |
| $this->contentPolicy = $contentPolicy; | |
| return $this; | |
| } | |
| public function getContentPolicy() | |
| { | |
| return $this->contentPolicy; | |
| } | |
| public function setErrorPriority($priority) | |
| { | |
| $this->priority = $priority; | |
| return $this; | |
| } | |
| /** | |
| * Register or unregister an error handler | |
| */ | |
| public function registerErrorHandler( $f = null) | |
| { | |
| if (is_null($this->errorHandler) && is_callable($f)){ | |
| // Initialize | |
| $this->errorHandler = $f; | |
| $prioryty = $this->priority; | |
| set_error_handler($f); | |
| } elseif ($this->errorHandler && is_null($f)) { | |
| // Reset error handler | |
| restore_error_handler(); | |
| $this->errorHandler = null; | |
| } elseif (is_null($this->errorHandler) && is_null($f)) { | |
| // Install default | |
| $priority = $this->priority; | |
| set_error_handler( | |
| function ($errno, $errstr, $errfile, $errline) use ($priority) | |
| { | |
| throw new ErrorException($errstr, $errno, $priority, $errfile, $errline); | |
| } | |
| ); | |
| $this->errorHandler = function() {}; | |
| } elseif ($this->errorHandler && is_callable($f)) { | |
| // Override existing handler | |
| restore_error_handler(); | |
| set_error_handler($f); | |
| $this->errorHandler = $f; | |
| } | |
| return $this; | |
| } | |
| public function registerViewer( array $viewer, $errorCode = 0) | |
| { | |
| $this->viewers[$errorCode] = $viewer; | |
| } | |
| public function getErrorHandler() | |
| { | |
| return $this->errorHandler; | |
| } | |
| public function serializeHttpProblem(HttpProblem $problem) | |
| { | |
| $CNPClassName = $this->contentPolicy; | |
| return $CNPClassName::render($problem,$CNPClassName::renderers()); | |
| } | |
| public function render( Exception $e) | |
| { | |
| // build error model from Exception | |
| if ($e instanceof HttpErrorException){ | |
| $problem = $e->getHttpProblem(); | |
| } else { | |
| // guess http status code | |
| $errorCode = $e->getCode(); | |
| $isHttpErrorCode = ($errorCode >=400 && array_key_exists($errorCode, Http::$STATUS_CODES)); | |
| $statusCode = $isHttpErrorCode?$errorCode:500; | |
| $phpErrorURI='http://php.net/manual/errorfunc.constants'; | |
| $problem = new HttpProblem( // Populate a new HttpProblem model | |
| $statusCode, | |
| null, // use default title | |
| (string) $e,// developer description is string view for exception | |
| $isHttpErrorCode?null:($phpErrorURI.'#'.$errorCode), // guess problem instance | |
| $isHttpErrorCode?null:$phpErrorURI // guess problem type | |
| ); | |
| } | |
| // Set header and rendering payload | |
| Http::setHttpResponseCode($problem->httpStatus); | |
| return $this->serializeHttpProblem($problem); | |
| } | |
| } |