Completed
Push — master ( 2a2c83...501967 )
by recca
02:25
created

DatabasePanel::hightlight()   C

Complexity

Conditions 16
Paths 1

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 16.044

Importance

Changes 0
Metric Value
cc 16
eloc 38
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 62
ccs 34
cts 36
cp 0.9444
crap 16.044
rs 6.1338

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 Recca0120\LaravelTracy\Panels;
4
5
use PDO;
6
use Exception;
7
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
8
9
class DatabasePanel extends AbstractSubscriablePanel implements IAjaxPanel
10
{
11
    /**
12
     * $queries.
13
     *
14
     * @var array
15
     */
16
    protected $queries = [];
17
18
    /**
19
     * $totalTime.
20
     *
21
     * @var float
22
     */
23
    protected $totalTime = 0.0;
24
25
    /**
26
     * $counter.
27
     *
28
     * @var int
29
     */
30
    protected $counter = 0;
31
32
    /**
33
     * subscribe.
34
     */
35 2
    protected function subscribe()
36
    {
37 2
        $events = $this->laravel['events'];
38 2
        if (version_compare($this->laravel->version(), 5.2, '>=') === true) {
39
            $events->listen('Illuminate\Database\Events\QueryExecuted', function ($event) {
40 1
                $this->logQuery(
41 1
                    $event->sql,
42 1
                    $event->bindings,
43 1
                    $event->time,
44 1
                    $event->connectionName,
45 1
                    $event->connection->getPdo()
46 1
                );
47 1
            });
48 1
        } else {
49 1
            $events->listen('illuminate.query', function ($sql, $bindings, $time, $connectionName) {
50 1
                $this->logQuery(
51 1
                    $sql,
52 1
                    $bindings,
53 1
                    $time,
54 1
                    $connectionName,
55 1
                    $this->laravel['db']->connection($connectionName)->getPdo()
56 1
                );
57 1
            });
58
        }
59 2
    }
60
61
    /**
62
     * logQuery.
63
     *
64
     * @param string $sql
65
     * @param array $bindings
66
     * @param int $time
67
     * @param string $name
68
     * @param PDO $pdo
69
     * @param string $driver
70
     * @return $this
71
     */
72 3
    public function logQuery($sql, $bindings = [], $time = 0, $name = null, PDO $pdo = null, $driver = 'mysql')
73
    {
74 3
        ++$this->counter;
75 3
        $this->totalTime += $time;
76 3
        $source = static::findSource();
77 3
        $editorLink = static::editorLink($source);
78 3
        $this->queries[] = [
79 3
            'sql' => $sql,
80 3
            'bindings' => $bindings,
81 3
            'time' => $time,
82 3
            'name' => $name,
83 3
            'pdo' => $pdo,
84 3
            'driver' => $driver,
85 3
            'source' => $source,
86 3
            'editorLink' => $editorLink,
87 3
            'hightlight' => null,
88
        ];
89
90 3
        return $this;
91
    }
92
93
    /**
94
     * getAttributes.
95
     *
96
     * @return array
97
     */
98 3
    protected function getAttributes()
99
    {
100 3
        $queries = [];
101 3
        foreach ($this->queries as $query) {
102 3
            $sql = $query['sql'];
103 3
            $bindings = $query['bindings'];
104 3
            $pdo = $query['pdo'];
105 3
            $driver = $query['driver'];
106 3
            $version = 0;
107
108 3
            $hightlight = Helper::hightlight($sql, $bindings, $pdo);
109 3
            $explains = [];
110 3
            $hints = [];
111 3
            if ($pdo instanceof PDO) {
112 2
                $driver = $this->getDatabaseDriver($pdo);
113 2
                if ($driver === 'mysql') {
114 1
                    $version = $this->getDatabaseVersion($pdo);
115 1
                    $explains = Helper::explain($pdo, $sql, $bindings);
116 1
                    $hints = Helper::performQueryAnalysis($sql, $version, $driver);
117 1
                }
118 2
            }
119
120 3
            $queries[] = array_merge($query, compact('hightlight', 'explains', 'hints', 'driver', 'version'));
121 3
        }
122
123
        return [
124 3
            'counter' => $this->counter,
125 3
            'totalTime' => $this->totalTime,
126 3
            'queries' => $queries,
127 3
        ];
128
    }
129
130
    /**
131
     * getDatabaseDriver.
132
     *
133
     * @param \PDO $pdo
134
     * @return string
135
     */
136 2
    protected function getDatabaseDriver(PDO $pdo)
137
    {
138
        try {
139 2
            $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
140 2
        } catch (Exception $e) {
141 1
            $driver = null;
142
        }
143
144 2
        return $driver;
145
    }
146
147
    /**
148
     * getDatabaseVersion.
149
     *
150
     * @param \PDO $pdo
151
     * @return string
152
     */
153 1
    protected function getDatabaseVersion(PDO $pdo)
154
    {
155
        try {
156 1
            $version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
157 1
        } catch (Exception $e) {
158 1
            $version = 0;
159
        }
160
161 1
        return $version;
162
    }
163
}
164