Completed
Pull Request — master (#57)
by
unknown
03:01
created

ModelPanel::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Exception;
6
use Illuminate\Support\Str;
7
use PDO;
8
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
9
10
class ModelPanel extends AbstractSubscriablePanel implements IAjaxPanel
11
{
12
    /**
13
     * $queries.
14
     *
15
     * @var array
16
     */
17
    protected $models = [];
18
19
20
21
    /**
22
     * $counter.
23
     *
24
     * @var int
25
     */
26
    protected $total = 0;
27
28
29
30
31
    /**
32
     * subscribe.
33
     */
34
    protected function subscribe()
35
    {
36
        $events = $this->laravel['events'];
37
        $events->listen('eloquent.*', function ($event, $models) {
38
            if (Str::contains($event, 'eloquent.retrieved')) {
39
                foreach ($models as $model) {
40
                    $class = get_class($model);
41
                    $this->models[$class] = ($this->models[$class] ?? 0) + 1;
42
                    $this->total++;
43
                }
44
            }
45
        });
46
    }
47
48
    /**
49
     * getAttributes.
50
     *
51
     * @return array
52
     */
53
    protected function getAttributes()
54
    {
55
56
        return [
57
            'total' => $this->total,
58
            'models' => $this->models,
59
        ];
60
    }
61
62
}
63