Completed
Pull Request — master (#84)
by Tobias
09:11
created

DebugPluginCollector   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 142
Duplicated Lines 15.49 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 5
dl 22
loc 142
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addRequest() 0 4 1
A addResponse() 0 4 1
A addFailure() 0 13 3
A getSucessfulRequests() 11 11 3
A getFailedRequests() 11 11 3
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 Http\Message\Formatter\SimpleFormatter;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
14
/**
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class DebugPluginCollector extends DataCollector
18
{
19
    /**
20
     * @var Formatter
21
     */
22
    private $formatter;
23
24
    /**
25
     * @var PluginJournal
26
     */
27
    private $journal;
28
29
    /**
30
     * @param Formatter $formatter
31
     */
32
    public function __construct(Formatter $formatter, PluginJournal $journal)
33
    {
34
        $this->formatter = $formatter;
35
        $this->journal = $journal;
36
    }
37
38
    /**
39
     * @param RequestInterface $request
40
     */
41
    public function addRequest(RequestInterface $request, $clientName, $depth)
42
    {
43
        $this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
44
    }
45
46
    /**
47
     * @param ResponseInterface $response
48
     */
49
    public function addResponse(ResponseInterface $response, $clientName, $depth)
50
    {
51
        $this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
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
    /**
73
     * Returns the successful request-resonse pairs.
74
     *
75
     * @return array
76
     */
77 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...
78
    {
79
        $count = 0;
80
        foreach ($this->data as $client) {
81
            if (isset($client['request'])) {
82
                $count += count($client['request'][0]);
83
            }
84
        }
85
86
        return $count;
87
    }
88
89
    /**
90
     * Returns the failed request-resonse pairs.
91
     *
92
     * @return array
93
     */
94 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...
95
    {
96
        $count = 0;
97
        foreach ($this->data as $client) {
98
            if (isset($client['failure'])) {
99
                $count += count($client['failure'][0]);
100
            }
101
        }
102
103
        return $count;
104
    }
105
106
    /**
107
     * Returns the total number of request made.
108
     *
109
     * @return int
110
     */
111
    public function getTotalRequests()
112
    {
113
        return $this->getSucessfulRequests() + $this->getFailedRequests();
114
    }
115
116
    /**
117
     *
118
     * @return ClientDataCollector[]
119
     */
120
    public function getClients()
121
    {
122
        return ClientDataCollector::createFromCollectedData($this->data);
123
    }
124
125
    /**
126
     * @return PluginJournal
127
     */
128
    public function getJournal()
129
    {
130
        return $this->journal;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function collect(Request $request, Response $response, \Exception $exception = null)
137
    {
138
        // We do not need to collect any data from the Symfony Request and Response
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getName()
145
    {
146
        return 'httplug';
147
    }
148
149
    public function serialize()
150
    {
151
        return serialize([$this->data, $this->journal]);
152
    }
153
154
    public function unserialize($data)
155
    {
156
        list($this->data, $this->journal) = unserialize($data);
157
    }
158
}
159