Completed
Push — master ( 325d09...af44b2 )
by Ma
02:19
created

ErrorHandler::errorHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace Api;
3
4
class ErrorHandler
5
{
6
7
    private $logger;
8
9
    public function __construct($logger)
10
    {
11
        $this->logger = $logger;
12
    }
13
14
    private function buildErrorMessage($message) {
15
        header('Content-Type: application/json');
16
        die(json_encode(['error' => $message]));
0 ignored issues
show
Coding Style Compatibility introduced by
The method buildErrorMessage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
17
    }
18
19
    public function errorHandler($level, $message, $file) {
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
        $this->logger->addWarning($message);
21
        $this->buildErrorMessage('Error occur.');
22
     }
23
24
    public function exceptionHandler($exception) {
25
        $this->logger->addWarning($exception->getMessage());
26
        $this->buildErrorMessage('Error occur.');
27
     }
28
}