Completed
Pull Request — master (#84)
by Tobias
16:13 queued 06:15
created

DebugPluginCollector::addRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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