Completed
Pull Request — master (#84)
by Tobias
08:23 queued 05:37
created

DebugPluginCollector::getSucessfulRequests()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 15
ccs 0
cts 12
cp 0
rs 8.8571
cc 5
eloc 8
nc 5
nop 0
crap 30
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
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
     */
33 1
    public function __construct(Formatter $formatter, PluginJournal $journal)
34
    {
35 1
        $this->formatter = $formatter;
36 1
        $this->journal = $journal;
37 1
    }
38
39
    /**
40
     * @param RequestInterface $request
41
     */
42
    public function addRequest(RequestInterface $request, $clientName, $depth)
43
    {
44
        $this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
45
    }
46
47
    /**
48
     * @param ResponseInterface $response
49
     */
50
    public function addResponse(ResponseInterface $response, $clientName, $depth)
51
    {
52
        $this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
53
        $this->data[$clientName]['failure'][$depth][] = false;
54
    }
55
56
    /**
57
     * @param Exception $exception
58
     */
59
    public function addFailure(Exception $exception, $clientName, $depth)
60
    {
61
        if ($exception instanceof Exception\HttpException) {
62
            $formattedResponse = $this->formatter->formatResponse($exception->getResponse());
63
        } elseif ($exception instanceof Exception\TransferException) {
64
            $formattedResponse = $exception->getMessage();
65
        } else {
66
            $formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
67
        }
68
69
        $this->data[$clientName]['response'][$depth][] = $formattedResponse;
70
        $this->data[$clientName]['failure'][$depth][] = true;
71
    }
72
73
    /**
74
     * Returns the successful request-resonse pairs.
75
     *
76
     * @return array
77
     */
78 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...
79
    {
80
        $count = 0;
81
        foreach ($this->data as $client) {
82
            if (isset($client['failure'])) {
83
                foreach ($client['failure'][0] as $failure) {
84
                    if (!$failure) {
85
                        ++$count;
86
                    }
87
                }
88
            }
89
        }
90
91
        return $count;
92
    }
93
94
    /**
95
     * Returns the failed request-resonse pairs.
96
     *
97
     * @return array
98
     */
99 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...
100
    {
101
        $count = 0;
102
        foreach ($this->data as $client) {
103
            if (isset($client['failure'])) {
104
                foreach ($client['failure'][0] as $failure) {
105
                    if ($failure) {
106
                        ++$count;
107
                    }
108
                }
109
            }
110
        }
111
112
        return $count;
113
    }
114
115
    /**
116
     * Returns the total number of request made.
117
     *
118
     * @return int
119
     */
120
    public function getTotalRequests()
121
    {
122
        return $this->getSucessfulRequests() + $this->getFailedRequests();
123
    }
124
125
    /**
126
     * Return a RequestStackProvider for each client.
127
     *
128
     * @return RequestStackProvider[]
129
     */
130
    public function getClients()
131
    {
132
        return RequestStackProvider::createFromCollectedData($this->data);
133
    }
134
135
    /**
136
     * @return PluginJournal
137
     */
138
    public function getJournal()
139
    {
140
        return $this->journal;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function collect(Request $request, Response $response, \Exception $exception = null)
147
    {
148
        // We do not need to collect any data from the Symfony Request and Response
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getName()
155
    {
156
        return 'httplug';
157
    }
158
159
    public function serialize()
160
    {
161
        return serialize([$this->data, $this->journal]);
162
    }
163
164
    public function unserialize($data)
165
    {
166
        list($this->data, $this->journal) = unserialize($data);
167
    }
168
}
169