Completed
Push — master ( 7447f5...b893a1 )
by Fabien
04:06
created

Collector::getTotalDuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
     * @param array $clients
25
     */
26 5
    public function __construct(array $clients)
27
    {
28 5
        $this->data['stacks'] = [];
29 5
        $this->data['clients'] = $clients;
30 5
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function collect(Request $request, Response $response, Exception $exception = null)
36
    {
37
        // We do not need to collect any data from the Symfony Request and Response
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function getName()
44
    {
45 1
        return 'httplug';
46
    }
47
48
    /**
49
     * @param Stack $stack
50
     */
51 3
    public function addStack(Stack $stack)
52
    {
53 3
        $this->data['stacks'][] = $stack;
54 3
    }
55
56
    /**
57
     * @return Stack[]
58
     */
59 3
    public function getStacks()
60
    {
61 3
        return $this->data['stacks'];
62
    }
63
64
    /**
65
     * @return Stack|null Return null there is no current stack.
66
     */
67 3
    public function getCurrentStack()
68
    {
69 3
        if (false === $stack = end($this->data['stacks'])) {
70
            return null;
71
        }
72
73 3
        return $stack;
74
    }
75
76
    /**
77
     * @return Stack[]
78
     */
79
    public function getSuccessfulStacks()
80
    {
81
        return array_filter($this->data['stacks'], function (Stack $stack) {
82
            return !$stack->isFailed();
83
        });
84
    }
85
86
    /**
87
     * @return Stack[]
88
     */
89
    public function getFailedStacks()
90
    {
91
        return array_filter($this->data['stacks'], function (Stack $stack) {
92
            return $stack->isFailed();
93
        });
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getClients()
100
    {
101
        return $this->data['clients'];
102
    }
103
104
    /**
105
     * @param $client
106
     *
107
     * @return Stack[]
108
     */
109
    public function getClientStacks($client)
110
    {
111
        return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
112
            return $stack->getClient() == $client;
113
        });
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function getTotalDuration()
120
    {
121
        return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
122
            return $carry + $stack->getDuration();
123
        }, 0);
124
    }
125
}
126