DebugBar::__invoke()   C
last analyzed

Complexity

Conditions 12
Paths 25

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 5.9842
c 0
b 0
f 0
cc 12
eloc 25
nc 25
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\Utils;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Middleware to render a debugbar in html responses.
13
 */
14
class DebugBar
15
{
16
    use Utils\HtmlInjectorTrait;
17
    use Utils\AttributeTrait;
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
        $renderer = $this->debugBar->getJavascriptRenderer();
65
66
        //Is an asset?
67
        $path = $request->getUri()->getPath();
68
        $renderPath = $renderer->getBaseUrl();
69
70
        if (strpos($path, $renderPath) === 0) {
71
            $file = $renderer->getBasePath().substr($path, strlen($renderPath));
72
73
            if (file_exists($file)) {
74
                return $response->withBody(self::createStream($file, 'r'));
75
            }
76
        }
77
78
        $response = $next($request, $response);
79
80
        //Fix the render baseUrl
81
        $generator = BasePath::getGenerator($request);
82
83
        if ($generator) {
84
            $renderer->setBaseUrl($generator($renderer->getBaseUrl()));
85
        }
86
87
        $ajax = Utils\Helpers::isAjax($request);
88
89
        //Redirection response
90
        if (Utils\Helpers::isRedirect($response)) {
91
            if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) {
92
                $this->debugBar->stackData();
93
            }
94
95
        //Html response
96
        } elseif (Utils\Helpers::getMimeType($response) === 'text/html') {
97
            if (!$ajax) {
98
                $response = $this->inject($response, $renderer->renderHead(), 'head');
99
            }
100
101
            $response = $this->inject($response, $renderer->render(!$ajax), 'body');
102
103
        //Ajax response
104
        } elseif ($ajax && $this->captureAjax) {
105
            $headers = $this->debugBar->getDataAsHeaders();
106
107
            foreach ($headers as $name => $value) {
108
                $response = $response->withHeader($name, $value);
109
            }
110
        }
111
112
        return $response;
113
    }
114
}
115