CacheCollector::collect()   B
last analyzed

Complexity

Conditions 10
Paths 36

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 20
c 1
b 0
f 0
nc 36
nop 0
dl 0
loc 31
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LeKoala\DebugBar\Collector;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Control\Director;
7
use DebugBar\DataCollector\Renderable;
8
use LeKoala\DebugBar\Proxy\CacheProxy;
9
use DebugBar\DataCollector\AssetProvider;
10
use DebugBar\DataCollector\DataCollector;
11
12
/**
13
 * Collects data about the cache
14
 */
15
class CacheCollector extends DataCollector implements Renderable, AssetProvider
16
{
17
    protected $showGet = false;
18
19
    public function getName()
20
    {
21
        return 'cache';
22
    }
23
24
    public function getAssets()
25
    {
26
        // This depends on ConfigCollector assets
27
        return [
28
            'base_path' => '/' . DebugBar::moduleResource('javascript')->getRelativePath(),
29
            'base_url' => Director::makeRelative(DebugBar::moduleResource('javascript')->getURL()),
30
            'css' => 'config/widget.css',
31
            'js' => 'config/widget.js'
32
        ];
33
    }
34
35
    public function collect()
36
    {
37
        $result = CacheProxy::getData();
38
39
        $keys = [];
40
        $showGet = $this->showGet || isset($_REQUEST['debug_cacheshowget']);
41
        foreach ($result as $k => $v) {
42
            $type = $v['type'] ?? null;
43
            if (!$showGet && $type == "get") {
44
                continue;
45
            }
46
            $val = $v['value'];
47
            if (!is_string($val)) {
48
                $val = json_encode($val);
49
            }
50
            // Long values are trimmed by DebugBar js
51
            if (!empty($v['ttl'])) {
52
                $val .= " - TTL: " . $v['ttl'];
53
            }
54
            if (!empty($v['caller'])) {
55
                $val .= " - (" . $v['caller'] . ")";
56
            }
57
            if ($type == 'set' && $showGet) {
58
                $val = "SET - " . $val;
59
            }
60
            $keys[$v['key']] = $val;
61
        }
62
63
        return [
64
            'count' => count($keys),
65
            'keys' => $keys
66
        ];
67
    }
68
69
    public function getWidgets()
70
    {
71
        $widgets = [
72
            'Cache' => [
73
                'icon' => 'puzzle-piece',
74
                'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
75
                'map' => 'cache.keys',
76
                'default' => '{}'
77
            ]
78
        ];
79
        $widgets['Cache:badge'] = [
80
            'map' => 'cache.count',
81
            'default' => 0
82
        ];
83
84
        return $widgets;
85
    }
86
87
    /**
88
     * Get the value of showGet
89
     */
90
    public function getShowGet()
91
    {
92
        return $this->showGet;
93
    }
94
95
    /**
96
     * Set the value of showGet
97
     *
98
     * @param bool $showGet
99
     */
100
    public function setShowGet($showGet)
101
    {
102
        $this->showGet = $showGet;
103
        return $this;
104
    }
105
}
106