Completed
Push — master ( 8ecd74...b9a329 )
by Marcel
12s
created

Console::output()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
namespace BeyondCode\QueryDetector\Outputs;
3
use Illuminate\Support\Collection;
4
use Symfony\Component\HttpFoundation\Response;
5
6 View Code Duplication
class Console implements Output
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
7
{
8
    public function output(Collection $detectedQueries, Response $response)
9
    {
10
        if ($response->isRedirection()) {
11
            return;
12
        }
13
        $content = $response->getContent();
14
        $outputContent = $this->getOutputContent($detectedQueries);
15
        $pos = strripos($content, '</body>');
16
        if (false !== $pos) {
17
            $content = substr($content, 0, $pos) . $outputContent . substr($content, $pos);
18
        } else {
19
            $content = $content . $outputContent;
20
        }
21
        // Update the new content and reset the content length
22
        $response->setContent($content);
23
        $response->headers->remove('Content-Length');
24
    }
25
    protected function getOutputContent(Collection $detectedQueries)
26
    {
27
        $output = '<script type="text/javascript">';
28
        $output .= "console.warn('Found the following N+1 queries in this request:\\n\\n";
29
        foreach ($detectedQueries as $detectedQuery) {
30
            $output .= "Model: ".addslashes($detectedQuery['model']). " => Relation: ".addslashes($detectedQuery['relation']);
31
            $output .= " - You should add \"with(\'".$detectedQuery['relation']."\')\" to eager-load this relation.";
32
            $output .= "\\n";
33
        }
34
        $output .= "')";
35
        $output .= '</script>';
36
        return $output;
37
    }
38
}
39