ErrorHandlerServiceProvider   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 24 2
1
<?php
2
3
namespace App\Providers;
4
5
use App\Common\JsonException;
6
use Pimple\Container;
7
use Exception;
8
9
final class ErrorHandlerServiceProvider extends BaseServiceProvider
10
{
11
    /**
12
     * Register error handlers service provider.
13
     *
14
     * @param Container $container
15
     */
16
    public function register(Container $container)
17
    {
18
        $container['errorHandler'] = function(Container $container) {
19
            return function($request, $response, Exception $exception) use ($container) {
20
                $details = $container['settings']['displayErrorDetails'] ? $exception->getMessage() : 'Internal server error';
21
                $error   = new JsonException(null, 500, 'Internal server error', $details);
22
23
                return $container['apiRenderer']->jsonResponse($response, $error->statusCode, $error->encodeError());
24
            };
25
        };
26
27
        $container['notAllowedHandler'] = function() {
28
            return function($request, $response, $methods) {
29
                throw new JsonException(null, 405, 'Method Not Allowed', 'Method must be one of: '.implode(', ', $methods));
30
            };
31
        };
32
33
        $container['notFoundHandler'] = function() {
34
            return function() {
35
                throw new JsonException(null, 404, 'Not found', 'Entity not found');
36
            };
37
        };
38
39
    }
40
}
41