ErrorHandler::buildErrorMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace Api;
3
4
use Monolog\Logger;
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
7
class ErrorHandler
8
{
9
    /**
10
     * @var Logger
11
     */
12
    private $logger;
13
    /**
14
     * Constructor
15
     *
16
     * @param Logger $logger logger class
17
     */
18
    public function __construct(Logger $logger)
19
    {
20
        $this->logger = $logger;
21
    }
22
    /**
23
     * Building error message
24
     *
25
     * @param string $message
26
     */
27
    private function buildErrorMessage($message)
28
    {
29
        $response = new JsonResponse();
30
        $response->setData(
31
            [
32
            'error' => $message
33
            ]
34
        );
35
        $response->send();
36
        die();
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...
37
    }
38
    /**
39
     * Handling php error
40
     *
41
     * @param integer $level error level
42
     * @param string $message error message
43
     */
44
    public function errorHandler($level, $message)
45
    {
46
        $this->logger->addWarning($message);
47
        $this->buildErrorMessage('Error occur.');
48
    }
49
    /**
50
     * Handling exceptions
51
     *
52
     * @param object $exception exception class
53
     */
54
    public function exceptionHandler($exception)
55
    {
56
        $this->logger->addWarning($exception->getMessage());
57
        $this->buildErrorMessage('Error occur.');
58
    }
59
}
60