WhoopsServiceProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 48
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 12 2
A registerWhoops() 0 9 1
A registerErrorPageHandler() 0 10 2
A registerExceptionHandler() 0 11 2
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace WhoopsPimple;
8
9
use Pimple\Container;
10
use Pimple\ServiceProviderInterface;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\Exception\HttpException;
13
use Whoops\Handler\PlainTextHandler;
14
use Whoops\Handler\PrettyPageHandler;
15
use Whoops\Run;
16
17
class WhoopsServiceProvider implements ServiceProviderInterface
18
{
19
    public function register(Container $container)
20
    {
21
        $this->registerErrorPageHandler($container);
22
        $this->registerExceptionHandler($container);
23
        $this->registerWhoops($container);
24
25
        if (set_exception_handler($container['whoops.exception_handler']) !== null) {
26
            restore_exception_handler();
27
        }
28
29
        $container['whoops']->register();
30
    }
31
32
    private function registerWhoops(Container $container)
33
    {
34
        $container['whoops'] = function (Container $container) {
35
            $run = new Run;
36
            $run->allowQuit(false);
37
            $run->pushHandler($container['whoops.error_page_handler']);
38
            return $run;
39
        };
40
    }
41
42
    private function registerErrorPageHandler(Container $container)
43
    {
44
        $container['whoops.error_page_handler'] = function () {
45
            if (PHP_SAPI === 'cli') {
46
                return new PlainTextHandler;
47
            } else {
48
                return new PrettyPageHandler;
49
            }
50
        };
51
    }
52
53
    private function registerExceptionHandler(Container $container)
54
    {
55
        $container['whoops.exception_handler'] = $container->protect(function ($e) use ($container) {
56
            $method = Run::EXCEPTION_HANDLER;
57
            ob_start();
58
            $container['whoops']->$method($e);
59
            $response = ob_get_clean();
60
            $code = $e instanceof HttpException ? $e->getStatusCode() : 500;
61
            return new Response($response, $code);
62
        });
63
    }
64
}
65