Completed
Push — master ( 9792a6...aaaf86 )
by recca
10:22
created

ViewPanel::limitCollection()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 6
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
8
9
class ViewPanel extends AbstractSubscriablePanel implements IAjaxPanel
10
{
11
    /**
12
     * $limit.
13
     *
14
     * @var int
15
     */
16
    public $limit = 50;
17
    /**
18
     * $views.
19
     *
20
     * @var array
21
     */
22
    protected $views = [];
23
24
    /**
25
     * subscribe.
26
     */
27 2
    protected function subscribe()
28
    {
29 2
        if (version_compare($this->laravel->version(), 5.4, '>=') === true) {
30 1
            $this->laravel['events']->listen('composing:*', function ($key, $payload) {
31 1
                $this->logView($payload[0]);
32 1
            });
33
        } else {
34 1
            $this->laravel['events']->listen('composing:*', function ($payload) {
35 1
                $this->logView($payload);
36 1
            });
37
        }
38 2
    }
39
40
    /**
41
     * logView.
42
     *
43
     * @param  \Illuminate\Contracts\View\View
44
     * @return string
45
     */
46 2
    protected function logView($view)
47
    {
48 2
        $name = $view->getName();
49 2
        $data = $this->limitCollection(Arr::except($view->getData(), ['__env', 'app']));
50 2
        $path = static::editorLink($view->getPath());
51 2
        preg_match('/href=\"(.+)\"/', $path, $m);
52 2
        $path = (count($m) > 1) ? '(<a href="'.$m[1].'">source</a>)' : '';
53 2
        $this->views[] = compact('name', 'data', 'path');
54 2
    }
55
56
    /**
57
     * limitCollection.
58
     *
59
     * @param array $data
60
     * @return array
61
     */
62 2
    protected function limitCollection($data)
63
    {
64 2
        $results = [];
65 2
        foreach ($data as $key => $value) {
66 2
            if (is_array($value) === true && count($value) > $this->limit) {
67 2
                $value = array_slice($value, 0, $this->limit);
68
            }
69
70 2
            if ($value instanceof Collection && $value->count() > $this->limit) {
71 2
                $value = $value->take($this->limit);
72
            }
73
74 2
            $results[$key] = $value;
75
        }
76
77 2
        return $results;
78
    }
79
80
    /**
81
     * getAttributes.
82
     *
83
     * @return array
84
     */
85 2
    protected function getAttributes()
86
    {
87
        return [
88 2
            'rows' => $this->views,
89
        ];
90
    }
91
}
92