Completed
Pull Request — v1 (#422)
by
unknown
03:17
created

WhoopsServiceProvider::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 58
ccs 0
cts 37
cp 0
rs 9.0077
cc 4
eloc 31
nc 2
nop 1
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Provider\Phalcon;
8
9
use Phalcon\DI;
10
use Phalcon\DI\Exception;
11
use Whoops\Handler\JsonResponseHandler;
12
use Whoops\Handler\PrettyPageHandler;
13
use Whoops\Run;
14
15
class WhoopsServiceProvider
16
{
17
    /**
18
     * @param DI $di
19
     */
20
    public function __construct(DI $di = null)
21
    {
22
        if (!$di) {
23
            $di = DI::getDefault();
24
        }
25
26
        // There's only ever going to be one error page...right?
27
        $di->setShared('whoops.pretty_page_handler', function () {
28
            return new PrettyPageHandler();
29
        });
30
31
        // There's only ever going to be one error page...right?
32
        $di->setShared('whoops.json_response_handler', function () {
33
            $jsonHandler = new JsonResponseHandler();
34
            $jsonHandler->onlyForAjaxRequests(true);
35
            return $jsonHandler;
36
        });
37
38
        // Retrieves info on the Phalcon environment and ships it off
39
        // to the PrettyPageHandler's data tables:
40
        // This works by adding a new handler to the stack that runs
41
        // before the error page, retrieving the shared page handler
42
        // instance, and working with it to add new data tables
43
        $phalcon_info_handler = function () use ($di) {
44
            try {
45
                $request = $di['request'];
46
            } catch (Exception $e) {
47
                // This error occurred too early in the application's life
48
                // and the request instance is not yet available.
49
                return;
50
            }
51
52
            // Request info:
53
            $di['whoops.pretty_page_handler']->addDataTable('Phalcon Application (Request)', array(
54
                'URI'         => $request->getScheme().'://'.$request->getServer('HTTP_HOST').$request->getServer('REQUEST_URI'),
55
                'Request URI' => $request->getServer('REQUEST_URI'),
56
                'Path Info'   => $request->getServer('PATH_INFO'),
57
                'Query String' => $request->getServer('QUERY_STRING') ?: '<none>',
58
                'HTTP Method' => $request->getMethod(),
59
                'Script Name' => $request->getServer('SCRIPT_NAME'),
60
                //'Base Path'   => $request->getBasePath(),
61
                //'Base URL'    => $request->getBaseUrl(),
62
                'Scheme'      => $request->getScheme(),
63
                'Port'        => $request->getServer('SERVER_PORT'),
64
                'Host'        => $request->getServerName(),
65
            ));
66
        };
67
68
        $di->setShared('whoops', function () use ($di,$phalcon_info_handler) {
69
            $run = new Run();
70
            $run->pushHandler($di['whoops.pretty_page_handler']);
71
            $run->pushHandler($phalcon_info_handler);
72
            $run->pushHandler($di['whoops.json_response_handler']);
73
            return $run;
74
        });
75
76
        $di['whoops']->register();
77
    }
78
}
79