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

DebugPluginCollector::getJournal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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