Completed
Push — master ( d60d6d...9ab212 )
by Oscar
02:40
created

DebugBar::__invoke()   D

Complexity

Conditions 10
Paths 11

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 1 Features 1
Metric Value
c 11
b 1
f 1
dl 0
loc 43
rs 4.8197
cc 10
eloc 24
nc 11
nop 3

How to fix   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
    private $debugBar;
20
    private $captureAjax = true;
21
22
    /**
23
     * Constructor. Set the debug bar.
24
     *
25
     * @param Bar|null $debugBar
26
     */
27
    public function __construct(Bar $debugBar = null)
28
    {
29
        if ($debugBar !== null) {
30
            $this->debugBar($debugBar);
31
        }
32
    }
33
34
    /**
35
     * Set the debug bar.
36
     *
37
     * @param Bar $debugBar
38
     * 
39
     * @return self
40
     */
41
    public function debugBar(Bar $debugBar)
42
    {
43
        $this->debugBar = $debugBar;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Configure whether capture ajax requests to send the data with headers
50
     *
51
     * @param bool $captureAjax
52
     * 
53
     * @return self
54
     */
55
    public function captureAjax($captureAjax = true)
56
    {
57
        $this->captureAjax = $captureAjax;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Execute the middleware.
64
     *
65
     * @param ServerRequestInterface $request
66
     * @param ResponseInterface      $response
67
     * @param callable               $next
68
     *
69
     * @return ResponseInterface
70
     */
71
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
72
    {
73
        if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
74
            throw new RuntimeException('This middleware needs FormatNegotiator executed before');
75
        }
76
77
        $ajax = Utils\Helpers::isAjax($request);
78
        $debugBar = $this->debugBar ?: new StandardDebugBar();
79
80
        //Redirection response
81
        if (Utils\Helpers::isRedirect($response)) {
82
            if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) {
83
                $debugBar->stackData();
84
            }
85
86
        //Html response
87
        } elseif (FormatNegotiator::getFormat($request) === 'html') {
88
            $renderer = $debugBar->getJavascriptRenderer();
89
90
            ob_start();
91
            echo '<style>';
92
            $renderer->dumpCssAssets();
93
            echo '</style>';
94
95
            echo '<script>';
96
            $renderer->dumpJsAssets();
97
            echo '</script>';
98
99
            echo $renderer->render(!$ajax);
100
101
            $response = $this->inject($response, ob_get_clean());
102
        
103
        //Ajax response
104
        } elseif ($ajax && $this->captureAjax) {
105
            $headers = $debugBar->getDataAsHeaders();
106
107
            foreach ($headers as $name => $value) {
108
                $response = $response->withHeader($name, $value);
109
            }
110
        }
111
112
        return $next($request, $response);
113
    }
114
}
115