Completed
Push — master ( 5eb8de...a6b875 )
by Tobias
14:06
created

Collector   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 69.81%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 181
ccs 37
cts 53
cp 0.6981
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collect() 0 4 1
A reset() 0 5 1
A getName() 0 4 1
A deactivateStack() 0 4 1
A getActiveStack() 0 4 1
A addStack() 0 4 1
A getChildrenStacks() 0 6 1
A getStacks() 0 4 1
A getSuccessfulStacks() 0 6 1
A getFailedStacks() 0 6 1
A getClientRootStacks() 0 6 2
A countClientMessages() 0 6 1
A countStackMessages() 0 6 1
A getTotalDuration() 0 6 1
A activateStack() 0 8 2
A getClients() 0 10 1
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 18
    public function __construct()
29
    {
30 18
        $this->reset();
31 18
    }
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 18
    public function reset()
45
    {
46 18
        $this->data['stacks'] = [];
47 18
        $this->activeStack = null;
48 18
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function getName()
54
    {
55 2
        return 'httplug';
56
    }
57
58
    /**
59
     * Mark the stack as active. If a stack was already active, use it as parent for our stack.
60
     *
61
     * @param Stack $stack
62
     */
63 7
    public function activateStack(Stack $stack)
64
    {
65 7
        if (null !== $this->activeStack) {
66 2
            $stack->setParent($this->activeStack);
0 ignored issues
show
Documentation introduced by
$this->activeStack is of type object<Http\HttplugBundle\Collector\Stack>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
        }
68
69 7
        $this->activeStack = $stack;
70 7
    }
71
72
    /**
73
     * Mark the stack as inactive.
74
     *
75
     * @param Stack $stack
76
     */
77 5
    public function deactivateStack(Stack $stack)
78
    {
79 5
        $this->activeStack = $stack->getParent();
80 5
    }
81
82
    /**
83
     * @return Stack|null
84
     */
85 7
    public function getActiveStack()
86
    {
87 7
        return $this->activeStack;
88
    }
89
90
    /**
91
     * @param Stack $stack
92
     */
93 6
    public function addStack(Stack $stack)
94
    {
95 6
        $this->data['stacks'][] = $stack;
96 6
    }
97
98
    /**
99
     * @param Stack $parent
100
     *
101
     * @return Stack[]
102
     */
103
    public function getChildrenStacks(Stack $parent)
104
    {
105
        return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) {
106
            return $stack->getParent() === $parent;
107
        });
108
    }
109
110
    /**
111
     * @return Stack[]
112
     */
113 5
    public function getStacks()
114
    {
115 5
        return $this->data['stacks'];
116
    }
117
118
    /**
119
     * @return Stack[]
120
     */
121
    public function getSuccessfulStacks()
122
    {
123
        return array_filter($this->data['stacks'], function (Stack $stack) {
124
            return !$stack->isFailed();
125
        });
126
    }
127
128
    /**
129
     * @return Stack[]
130
     */
131
    public function getFailedStacks()
132
    {
133 1
        return array_filter($this->data['stacks'], function (Stack $stack) {
134
            return $stack->isFailed();
135 1
        });
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function getClients()
142
    {
143 3
        $stacks = array_filter($this->data['stacks'], function (Stack $stack) {
144 2
            return null === $stack->getParent();
145 3
        });
146
147 3
        return array_unique(array_map(function (Stack $stack) {
148 2
            return $stack->getClient();
149 3
        }, $stacks));
150
    }
151
152
    /**
153
     * @param $client
154
     *
155
     * @return Stack[]
156
     */
157
    public function getClientRootStacks($client)
158
    {
159 1
        return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
160 1
            return $stack->getClient() == $client && null == $stack->getParent();
161 1
        });
162
    }
163
164
    /**
165
     * Count all messages for a client.
166
     *
167
     * @param $client
168
     *
169
     * @return int
170
     */
171
    public function countClientMessages($client)
172
    {
173
        return array_sum(array_map(function (Stack $stack) {
174
            return $this->countStackMessages($stack);
175
        }, $this->getClientRootStacks($client)));
176
    }
177
178
    /**
179
     * Recursively count message in stack.
180
     *
181
     * @param Stack $stack
182
     *
183
     * @return int
184
     */
185
    private function countStackMessages(Stack $stack)
186
    {
187
        return 1 + array_sum(array_map(function (Stack $child) {
188
            return $this->countStackMessages($child);
189
        }, $this->getChildrenStacks($stack)));
190
    }
191
192
    /**
193
     * @return int
194
     */
195
    public function getTotalDuration()
196
    {
197
        return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
198
            return $carry + $stack->getDuration();
199
        }, 0);
200
    }
201
}
202