QueryRecorder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 6 1
A record() 0 10 1
A getQueries() 0 10 2
A reset() 0 4 1
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