Completed
Pull Request — master (#84)
by Tobias
06:56
created

DebugPluginCollector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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
    public function __construct(Formatter $formatter, PluginJournal $journal)
35
    {
36
        $this->formatter = $formatter;
37
        $this->journal = $journal;
38
    }
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-resonse pairs.
82
     *
83
     * @return array
84
     */
85 View Code Duplication
    public function getSucessfulRequests()
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
90
            if (isset($client['failure'])) {
91
                foreach ($client['failure'][0] as $failure) {
92
                    if (!$failure) {
93
                        ++$count;
94
                    }
95
                }
96
            }
97
        }
98
99
        return $count;
100
    }
101
102
    /**
103
     * Returns the failed request-resonse pairs.
104
     *
105
     * @return array
106
     */
107 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...
108
    {
109
        $count = 0;
110
        foreach ($this->data as $client) {
111
112
            if (isset($client['failure'])) {
113
                foreach ($client['failure'][0] as $failure) {
114
                    if ($failure) {
115
                        ++$count;
116
                    }
117
                }
118
            }
119
        }
120
121
        return $count;
122
    }
123
124
    /**
125
     * Returns the total number of request made.
126
     *
127
     * @return int
128
     */
129
    public function getTotalRequests()
130
    {
131
        return $this->getSucessfulRequests() + $this->getFailedRequests();
132
    }
133
134
    /**
135
     * Return a RequestStackProvider for each client.
136
     *
137
     * @return RequestStackProvider[]
138
     */
139
    public function getClients()
140
    {
141
        return RequestStackProvider::createFromCollectedData($this->data);
142
    }
143
144
    /**
145
     * @return PluginJournal
146
     */
147
    public function getJournal()
148
    {
149
        return $this->journal;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function collect(Request $request, Response $response, \Exception $exception = null)
156
    {
157
        // We do not need to collect any data from the Symfony Request and Response
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getName()
164
    {
165
        return 'httplug';
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function serialize()
172
    {
173
        return serialize([$this->data, $this->journal]);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function unserialize($data)
180
    {
181
        list($this->data, $this->journal) = unserialize($data);
182
    }
183
}
184