Completed
Push — master ( 33ed6a...1627ff )
by Tobias
06:48
created

Collector::getClientRootStacks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Exception;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
9
10
/**
11
 * The Collector hold profiled Stacks pushed by StackPlugin. It also have a list of configured clients.
12
 * All those data are used to display the HTTPlug panel in the Symfony profiler.
13
 *
14
 * The collector is not designed for execution in a threaded application and does not support plugins that execute an
15
 * other request before the current one is sent by the client.
16
 *
17
 * @author Fabien Bourigault <[email protected]>
18
 *
19
 * @internal
20
 */
21
class Collector extends DataCollector
22
{
23
    /**
24
     * @var Stack|null
25
     */
26
    private $activeStack;
27
28 16
    public function __construct()
29
    {
30 16
        $this->data['stacks'] = [];
31 16
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function collect(Request $request, Response $response, Exception $exception = null)
37
    {
38
        // We do not need to collect any data from the Symfony Request and Response
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function getName()
45
    {
46 2
        return 'httplug';
47
    }
48
49
    /**
50
     * Mark the stack as active. If a stack was already active, use it as parent for our stack.
51
     *
52
     * @param Stack $stack
53
     */
54 6
    public function activateStack(Stack $stack)
55
    {
56 6
        if ($this->activeStack !== null) {
57 2
            $stack->setParent($this->activeStack);
58 2
        }
59
60 6
        $this->activeStack = $stack;
61 6
    }
62
63
    /**
64
     * Mark the stack as inactive.
65
     *
66
     * @param Stack $stack
67
     */
68 5
    public function deactivateStack(Stack $stack)
69
    {
70 5
        $this->activeStack = $stack->getParent();
71 5
    }
72
73
    /**
74
     * @return Stack|null
75
     */
76 6
    public function getActiveStack()
77
    {
78 6
        return $this->activeStack;
79
    }
80
81
    /**
82
     * @param Stack $stack
83
     */
84 5
    public function addStack(Stack $stack)
85
    {
86 5
        $this->data['stacks'][] = $stack;
87 5
    }
88
89
    /**
90
     * @param Stack $parent
91
     *
92
     * @return Stack[]
93
     */
94
    public function getChildrenStacks(Stack $parent)
95
    {
96
        return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) {
97
            return $stack->getParent() === $parent;
98
        });
99
    }
100
101
    /**
102
     * @return Stack[]
103
     */
104 4
    public function getStacks()
105
    {
106 4
        return $this->data['stacks'];
107
    }
108
109
    /**
110
     * @return Stack[]
111
     */
112
    public function getSuccessfulStacks()
113
    {
114
        return array_filter($this->data['stacks'], function (Stack $stack) {
115
            return !$stack->isFailed();
116
        });
117
    }
118
119
    /**
120
     * @return Stack[]
121
     */
122 1
    public function getFailedStacks()
123
    {
124
        return array_filter($this->data['stacks'], function (Stack $stack) {
125
            return $stack->isFailed();
126 1
        });
127
    }
128
129
    /**
130
     * @return array
131
     */
132 3
    public function getClients()
133
    {
134
        $stacks = array_filter($this->data['stacks'], function (Stack $stack) {
135 2
            return $stack->getParent() === null;
136 3
        });
137
138
        return array_unique(array_map(function (Stack $stack) {
139 2
            return $stack->getClient();
140 3
        }, $stacks));
141
    }
142
143
    /**
144
     * @param $client
145
     *
146
     * @return Stack[]
147
     */
148 1
    public function getClientRootStacks($client)
149
    {
150
        return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
151 1
            return $stack->getClient() == $client && $stack->getParent() == null;
152 1
        });
153
    }
154
155
    /**
156
     * Count all messages for a client.
157
     *
158
     * @param $client
159
     *
160
     * @return int
161
     */
162
    public function countClientMessages($client)
163
    {
164
        return array_sum(array_map(function (Stack $stack) {
165
            return $this->countStackMessages($stack);
166
        }, $this->getClientRootStacks($client)));
167
    }
168
169
    /**
170
     * Recursively count message in stack.
171
     *
172
     * @param Stack $stack
173
     *
174
     * @return int
175
     */
176
    private function countStackMessages(Stack $stack)
177
    {
178
        return 1 + array_sum(array_map(function (Stack $child) {
179
            return $this->countStackMessages($child);
180
        }, $this->getChildrenStacks($stack)));
181
    }
182
183
    /**
184
     * @return int
185
     */
186
    public function getTotalDuration()
187
    {
188
        return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
189
            return $carry + $stack->getDuration();
190
        }, 0);
191
    }
192
}
193