Completed
Push — master ( 00ed35...63fbb3 )
by Oscar
10:20
created

DebugBar::__invoke()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 1 Features 1
Metric Value
c 13
b 1
f 1
dl 0
loc 56
rs 6.7093
cc 12
eloc 28
nc 14
nop 3

How to fix   Long Method    Complexity   

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
namespace Psr7Middlewares\Middleware;
4
5
use DebugBar\DebugBar as Bar;
6
use DebugBar\StandardDebugBar;
7
use Psr7Middlewares\Middleware;
8
use Psr7Middlewares\Utils;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Middleware to render a debugbar in html responses.
14
 */
15
class DebugBar
16
{
17
    use Utils\HtmlInjectorTrait;
18
19
    /**
20
     * @var Bar|null The debugbar
21
     */
22
    private $debugBar;
23
24
    /**
25
     * @var bool Whether send data using headers in ajax requests
26
     */
27
    private $captureAjax = false;
28
29
    /**
30
     * Constructor. Set the debug bar.
31
     *
32
     * @param Bar|null $debugBar
33
     */
34
    public function __construct(Bar $debugBar = null)
35
    {
36
        $this->debugBar = $debugBar ?: new StandardDebugBar();
37
    }
38
39
    /**
40
     * Configure whether capture ajax requests to send the data with headers.
41
     *
42
     * @param bool $captureAjax
43
     * 
44
     * @return self
45
     */
46
    public function captureAjax($captureAjax = true)
47
    {
48
        $this->captureAjax = $captureAjax;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Execute the middleware.
55
     *
56
     * @param ServerRequestInterface $request
57
     * @param ResponseInterface      $response
58
     * @param callable               $next
59
     *
60
     * @return ResponseInterface
61
     */
62
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
63
    {
64
        if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
65
            throw new RuntimeException('This middleware needs FormatNegotiator executed before');
66
        }
67
68
        $renderer = $this->debugBar->getJavascriptRenderer();
69
70
        //Is an asset?
71
        $path = $request->getUri()->getPath();
72
        $renderPath = $renderer->getBaseUrl();
73
74
        if (strpos($path, $renderPath) === 0) {
75
            $file = $renderer->getBasePath().substr($path, strlen($renderPath));
76
77
            if (file_exists($file)) {
78
                $body = Middleware::createStream();
79
                $body->write(file_get_contents($file));
80
81
                return $response->withBody($body);
82
            }
83
        }
84
85
        $response = $next($request, $response);
86
87
        //Fix the render baseUrl
88
        $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl());
89
        $renderer->setBaseUrl($renderPath);
90
91
        $ajax = Utils\Helpers::isAjax($request);
92
93
        //Redirection response
94
        if (Utils\Helpers::isRedirect($response)) {
95
            if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) {
96
                $this->debugBar->stackData();
97
            }
98
99
        //Html response
100
        } elseif (FormatNegotiator::getFormat($request) === 'html') {
101
            if (!$ajax) {
102
                $response = $this->inject($response, $renderer->renderHead(), 'head');
103
            }
104
105
            $response = $this->inject($response, $renderer->render(!$ajax), 'body');
106
107
        //Ajax response
108
        } elseif ($ajax && $this->captureAjax) {
109
            $headers = $this->debugBar->getDataAsHeaders();
110
111
            foreach ($headers as $name => $value) {
112
                $response = $response->withHeader($name, $value);
113
            }
114
        }
115
116
        return $response;
117
    }
118
}
119