QueryRecorder::getQueries()   A
last analyzed

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 0
1
<?php
2
3
namespace Facade\Ignition\QueryRecorder;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Database\Events\QueryExecuted;
7
8
class QueryRecorder
9
{
10
    /** @var \Facade\Ignition\QueryRecorder\Query|[] */
11
    protected $queries = [];
12
13
    /** @var \Illuminate\Contracts\Foundation\Application */
14
    protected $app;
15
16
    public function __construct(Application $app)
17
    {
18
        $this->app = $app;
19
    }
20
21
    public function register()
22
    {
23
        $this->app['events']->listen(QueryExecuted::class, [$this, 'record']);
24
25
        return $this;
26
    }
27
28
    public function record(QueryExecuted $queryExecuted)
29
    {
30
        $maximumQueries = $this->app['config']->get('flare.reporting.maximum_number_of_collected_queries', 200);
31
32
        $reportBindings = $this->app['config']->get('flare.reporting.report_query_bindings', true);
33
34
        $this->queries[] = Query::fromQueryExecutedEvent($queryExecuted, $reportBindings);
35
36
        $this->queries = array_slice($this->queries, $maximumQueries * -1, $maximumQueries);
37
    }
38
39
    public function getQueries(): array
40
    {
41
        $queries = [];
42
43
        foreach ($this->queries as $query) {
44
            $queries[] = $query->toArray();
45
        }
46
47
        return $queries;
48
    }
49
50
    public function reset()
51
    {
52
        $this->queries = [];
53
    }
54
}
55