Completed
Pull Request — master (#84)
by Tobias
26:04 queued 16:05
created

DebugPluginCollector::addFailure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
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
    }
52
53
    /**
54
     * @param Exception $exception
55
     */
56
    public function addFailure(Exception $exception, $clientName, $depth)
57
    {
58
        if ($exception instanceof Exception\HttpException) {
59
            $formattedResponse = $this->formatter->formatResponse($exception->getResponse());
60
        } elseif ($exception instanceof Exception\TransferException) {
61
            $formattedResponse = $exception->getMessage();
62
        } else {
63
            $formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
64
        }
65
66
        $this->data[$clientName]['response'][$depth][] = $formattedResponse;
67
        $this->data[$clientName]['failure'][$depth][] = true;
68
    }
69
70
    /**
71
     * Returns the successful request-resonse pairs.
72
     *
73
     * @return array
74
     */
75 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...
76
    {
77
        $count = 0;
78
        foreach ($this->data as $client) {
79
            if (isset($client['request'])) {
80
                $count += count($client['request'][0]);
81
            }
82
        }
83
84
        return $count;
85
    }
86
87
    /**
88
     * Returns the failed request-resonse pairs.
89
     *
90
     * @return array
91
     */
92 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...
93
    {
94
        $count = 0;
95
        foreach ($this->data as $client) {
96
            if (isset($client['failure'])) {
97
                $count += count($client['failure'][0]);
98
            }
99
        }
100
101
        return $count;
102
    }
103
104
    /**
105
     * Returns the total number of request made.
106
     *
107
     * @return int
108
     */
109
    public function getTotalRequests()
110
    {
111
        return $this->getSucessfulRequests() + $this->getFailedRequests();
112
    }
113
114
    /**
115
     * @return ClientDataCollector[]
116
     */
117
    public function getClients()
118
    {
119
        return ClientDataCollector::createFromCollectedData($this->data);
120
    }
121
122
    /**
123
     * @return PluginJournal
124
     */
125
    public function getJournal()
126
    {
127
        return $this->journal;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function collect(Request $request, Response $response, \Exception $exception = null)
134
    {
135
        // We do not need to collect any data from the Symfony Request and Response
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getName()
142
    {
143
        return 'httplug';
144
    }
145
146
    public function serialize()
147
    {
148
        return serialize([$this->data, $this->journal]);
149
    }
150
151
    public function unserialize($data)
152
    {
153
        list($this->data, $this->journal) = unserialize($data);
154
    }
155
}
156