Completed
Pull Request — master (#84)
by Tobias
07:00
created

DebugPluginCollector   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 151
Duplicated Lines 19.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 3 Features 1
Metric Value
wmc 23
c 7
b 3
f 1
lcom 1
cbo 5
dl 30
loc 151
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addRequest() 0 4 1
A addResponse() 0 5 1
A addFailure() 0 13 3
B getSucessfulRequests() 15 15 5
B getFailedRequests() 15 15 5
A getTotalRequests() 0 4 1
A getClients() 0 4 1
A getJournal() 0 4 1
A collect() 0 4 1
A getName() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(Formatter $formatter, PluginJournal $journal)
34
    {
35
        $this->formatter = $formatter;
36
        $this->journal = $journal;
37
    }
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