Passed
Push — master ( 64da52...b1a7d7 )
by Peter
05:43
created

ApiIssueTrait::handleErrors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 24
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Http\Controllers;
6
7
use AbterPhp\Framework\Domain\Entities\IToJsoner;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Framework\Domain\Entities\IToJsoner was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Opulence\Http\Responses\Response;
9
use Opulence\Http\Responses\ResponseHeaders;
10
11
trait ApiIssueTrait
12
{
13
    /** @var string */
14
    protected $problemBaseUrl;
15
16
    /**
17
     * @param string $msg
18
     * @param array  $errors
19
     *
20
     * @return Response
21
     */
22
    protected function handleErrors(string $msg, array $errors): Response
23
    {
24
        $this->logger->debug($msg);
25
26
        $detail = [];
27
        foreach ($errors as $key => $keyErrors) {
28
            foreach ($keyErrors as $keyError) {
29
                $detail[] = sprintf('%s: %s', $key, $keyError);
30
            }
31
        }
32
33
        $status  = ResponseHeaders::HTTP_BAD_REQUEST;
34
        $content = [
35
            'type'   => sprintf('%sbad-request', $this->problemBaseUrl),
36
            'title'  => 'Bad Request',
37
            'status' => $status,
38
            'detail' => implode("\n", $detail),
39
        ];
40
41
        $response = new Response();
42
        $response->setStatusCode($status);
43
        $response->setContent(json_encode($content));
44
45
        return $response;
46
    }
47
48
    /**
49
     * @param string     $msg
50
     * @param \Exception $exception
51
     *
52
     * @return Response
53
     */
54
    protected function handleException(string $msg, \Exception $exception): Response
55
    {
56
        $this->logger->error($msg, $this->getExceptionContext($exception));
57
58
        $status  = ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR;
59
        $content = [
60
            'type'   => sprintf('%sinternal-server-error', $this->problemBaseUrl),
61
            'title'  => 'Internal Server Error',
62
            'status' => $status,
63
            'detail' => $exception->getMessage(),
64
        ];
65
66
        $response = new Response();
67
        $response->setStatusCode($status);
68
        $response->setContent(json_encode($content));
69
70
        return $response;
71
    }
72
73
    /**
74
     * @param \Exception $exception
75
     *
76
     * @return array
77
     */
78
    protected function getExceptionContext(\Exception $exception): array
79
    {
80
        $result = [static::LOG_CONTEXT_EXCEPTION => $exception->getMessage()];
0 ignored issues
show
Bug introduced by
The constant AbterPhp\Framework\Http\...::LOG_CONTEXT_EXCEPTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
82
        $i = 1;
83
        while ($exception = $exception->getPrevious()) {
84
            $result[sprintf(static::LOG_PREVIOUS_EXCEPTION, $i++)] = $exception->getMessage();
0 ignored issues
show
Bug introduced by
The constant AbterPhp\Framework\Http\...:LOG_PREVIOUS_EXCEPTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
        }
86
87
        return $result;
88
    }
89
}
90