Passed
Push — master ( b4f6c4...81b9da )
by Thomas
02:51 queued 10s
created

DeltaConfigManifestProxy::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 9
c 1
b 1
f 0
nc 3
nop 3
dl 0
loc 17
rs 9.9666
1
<?php
2
3
namespace LeKoala\DebugBar\Proxy;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\Config\Collections\DeltaConfigCollection;
7
8
class DeltaConfigManifestProxy extends DeltaConfigCollection implements ProxyConfigCollectionInterface
9
{
10
    /**
11
     * @var DeltaConfigCollection
12
     */
13
    protected $parent;
14
15
    /**
16
     * @var array
17
     */
18
    protected $configCalls = [];
19
20
    /**
21
     * @var boolean
22
     */
23
    protected $trackEmpty = false;
24
25
    public static function createFromOriginal(DeltaConfigCollection $collection)
26
    {
27
        $newCollection = static::createFromCollection($collection, $collection->getDeltaMiddleware()->getDisableFlag());
28
        $newCollection->setTrackEmpty(DebugBar::config()->config_track_empty);
29
        return $newCollection;
30
    }
31
32
    /**
33
     * Monitor calls made to get configuration during a request
34
     *
35
     * {@inheritDoc}
36
     */
37
    public function get($class, $name = null, $excludeMiddleware = 0)
38
    {
39
        $result = parent::get($class, $name, $excludeMiddleware);
40
41
        // Only track not empty values by default
42
        if ($result || $this->trackEmpty) {
43
            if (!isset($this->configCalls[$class][$name])) {
44
                $this->configCalls[$class][$name] = [
45
                    'calls' => 0,
46
                    'result' => null
47
                ];
48
            }
49
            $this->configCalls[$class][$name]['calls']++;
50
            $this->configCalls[$class][$name]['result'] = $result;
51
        }
52
53
        return $result;
54
    }
55
56
    /**
57
     * Return a list of all config calls made during the request, including how many times they were called
58
     * and the result
59
     *
60
     * @return array
61
     */
62
    public function getConfigCalls()
63
    {
64
        return $this->configCalls;
65
    }
66
67
    /**
68
     * Get the value of trackEmpty
69
     *
70
     * @return boolean
71
     */
72
    public function getTrackEmpty()
73
    {
74
        return $this->trackEmpty;
75
    }
76
77
    /**
78
     * Set the value of trackEmpty
79
     *
80
     * @param boolean $trackEmpty
81
     *
82
     * @return self
83
     */
84
    public function setTrackEmpty($trackEmpty)
85
    {
86
        $this->trackEmpty = $trackEmpty;
87
        return $this;
88
    }
89
}
90