Completed
Push — master ( f20448...8c0e84 )
by recca
12s queued 10s
created

ModelPanel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A subscribe() 0 13 3
A getAttributes() 0 7 1
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Illuminate\Support\Str;
6
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
7
8
class ModelPanel extends AbstractSubscriablePanel implements IAjaxPanel
9
{
10
    /**
11
     * $models.
12
     *
13
     * @var array
14
     */
15
    protected $models = [];
16
17
    /**
18
     * $total number of models.
19
     *
20
     * @var int
21
     */
22
    protected $total = 0;
23
24
    /**
25
     * subscribe.
26
     */
27
    protected function subscribe()
28
    {
29
        $events = $this->laravel['events'];
30
        $events->listen('eloquent.*', function ($event, $models) {
31
            if (Str::contains($event, 'eloquent.retrieved')) {
32
                foreach ($models as $model) {
33
                    $class = get_class($model);
34
                    $this->models[$class] = ($this->models[$class] ?? 0) + 1;
35
                    $this->total++;
36
                }
37
            }
38
        });
39
    }
40
41
    /**
42
     * getAttributes.
43
     *
44
     * @return array
45
     */
46
    protected function getAttributes()
47
    {
48
        return [
49
            'total' => $this->total,
50
            'models' => $this->models,
51
        ];
52
    }
53
}
54