Query::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Facade\Ignition\QueryRecorder;
4
5
use Illuminate\Database\Events\QueryExecuted;
6
7
class Query
8
{
9
    /** @var string */
10
    protected $sql;
11
12
    /** @var float */
13
    protected $time;
14
15
    /** @var string */
16
    protected $connectionName;
17
18
    /** @var null|array */
19
    protected $bindings;
20
21
    /** @var float */
22
    protected $microtime;
23
24
    public static function fromQueryExecutedEvent(QueryExecuted $queryExecuted, bool $reportBindings = false)
25
    {
26
        return new static(
27
            $queryExecuted->sql,
28
            $queryExecuted->time,
29
            $queryExecuted->connectionName ?? '',
30
            $reportBindings ? $queryExecuted->bindings : null
31
        );
32
    }
33
34
    protected function __construct(
35
        string $sql,
36
        float $time,
37
        string $connectionName,
38
        ?array $bindings = null,
39
        ?float $microtime = null
40
    ) {
41
        $this->sql = $sql;
42
        $this->time = $time;
43
        $this->connectionName = $connectionName;
44
        $this->bindings = $bindings;
45
        $this->microtime = $microtime ?? microtime(true);
46
    }
47
48
    public function toArray(): array
49
    {
50
        return [
51
            'sql' => $this->sql,
52
            'time' => $this->time,
53
            'connection_name' => $this->connectionName,
54
            'bindings' => $this->bindings,
55
            'microtime' => $this->microtime,
56
        ];
57
    }
58
}
59