Completed
Pull Request — master (#127)
by Pascal
08:48
created

DebugPluginCollector::getClients()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Http\Client\Exception;
6
use Http\Message\Formatter;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12
13
/**
14
 * A data collector for the debug plugin.
15
 *
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
final class DebugPluginCollector extends DataCollector
19
{
20
    /**
21
     * @var Formatter
22
     */
23
    private $formatter;
24
25
    /**
26
     * @var PluginJournal
27
     */
28
    private $journal;
29
30
    /**
31
     * @param Formatter     $formatter
32
     * @param PluginJournal $journal
33
     */
34 2
    public function __construct(Formatter $formatter, PluginJournal $journal)
35
    {
36 2
        $this->formatter = $formatter;
37 2
        $this->journal = $journal;
38 2
    }
39
40
    /**
41
     * @param RequestInterface $request
42
     * @param string           $clientName
43
     * @param int              $depth
44
     */
45
    public function addRequest(RequestInterface $request, $clientName, $depth)
46
    {
47
        $this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
48
    }
49
50
    /**
51
     * @param ResponseInterface $response
52
     * @param string            $clientName
53
     * @param int               $depth
54
     */
55
    public function addResponse(ResponseInterface $response, $clientName, $depth)
56
    {
57
        $this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
58
        $this->data[$clientName]['failure'][$depth][] = false;
59
    }
60
61
    /**
62
     * @param Exception $exception
63
     * @param string    $clientName
64
     * @param int       $depth
65
     */
66
    public function addFailure(Exception $exception, $clientName, $depth)
67
    {
68
        if ($exception instanceof Exception\HttpException) {
69
            $formattedResponse = $this->formatter->formatResponse($exception->getResponse());
70
        } elseif ($exception instanceof Exception\TransferException) {
71
            $formattedResponse = $exception->getMessage();
72
        } else {
73
            $formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
74
        }
75
76
        $this->data[$clientName]['response'][$depth][] = $formattedResponse;
77
        $this->data[$clientName]['failure'][$depth][] = true;
78
    }
79
80
    /**
81
     * Returns the successful request-response pairs.
82
     *
83
     * @return int
84
     */
85 View Code Duplication
    public function getSuccessfulRequests()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $count = 0;
88
        foreach ($this->data as $client) {
89
            if (isset($client['failure'])) {
90
                foreach ($client['failure'][0] as $failure) {
91
                    if (!$failure) {
92
                        ++$count;
93
                    }
94
                }
95
            }
96
        }
97
98
        return $count;
99
    }
100
101
    /**
102
     * Returns the failed request-response pairs.
103
     *
104
     * @return int
105
     */
106 View Code Duplication
    public function getFailedRequests()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $count = 0;
109
        foreach ($this->data as $client) {
110
            if (isset($client['failure'])) {
111
                foreach ($client['failure'][0] as $failure) {
112
                    if ($failure) {
113
                        ++$count;
114
                    }
115
                }
116
            }
117
        }
118
119
        return $count;
120
    }
121
122
    /**
123
     * Returns the total number of request made.
124
     *
125
     * @return int
126
     */
127
    public function getTotalRequests()
128
    {
129
        return $this->getSuccessfulRequests() + $this->getFailedRequests();
130
    }
131
132
    /**
133
     * Return a RequestStackProvider for each client.
134
     *
135
     * @return RequestStackProvider[]
136
     */
137
    public function getClients()
138
    {
139
        return RequestStackProvider::createFromCollectedData($this->data);
140
    }
141
142
    /**
143
     * @return PluginJournal
144
     */
145 1
    public function getJournal()
146
    {
147 1
        return $this->journal;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function collect(Request $request, Response $response, \Exception $exception = null)
154
    {
155
        // We do not need to collect any data from the Symfony Request and Response
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function getName()
162
    {
163 1
        return 'httplug';
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function serialize()
170
    {
171
        return serialize([$this->data, $this->journal]);
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function unserialize($data)
178
    {
179
        list($this->data, $this->journal) = unserialize($data);
180
    }
181
}
182