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

ClientDataProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
c 2
b 1
f 0
lcom 3
cbo 0
dl 0
loc 169
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createFromCollectedData() 0 9 2
C createOne() 0 26 7
A getRequests() 0 4 1
A setRequests() 0 6 1
A getResponses() 0 4 1
A setResponses() 0 6 1
A getStackIndexKeys() 0 4 1
A getRequstStack() 0 4 1
A getResponseStack() 0 4 1
A getFailureStack() 0 4 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
/**
6
 * An object to handle the collected data for a client. This is used to display data.
7
 *
8
 * The Request object at $requests[0][2] is the state of the object between the third
9
 * and the fourth plugin. The response after that plugin is found in $responses[0][2].
10
 *
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class ClientDataProvider
14
{
15
    /**
16
     * Array that tell if a request errored or not. true = success, false = failure.
17
     *
18
     * @var array
19
     */
20
    private $failure;
21
22
    /**
23
     * A multidimensional array with requests.
24
     * $requests[0][0] is the first request before all plugins.
25
     * $requests[0][1] is the first request after the first plugin.
26
     *
27
     * @var array
28
     */
29
    private $requests;
30
31
    /**
32
     * A multidimensional array with responses.
33
     * $responses[0][0] is the first responses before all plugins.
34
     * $responses[0][1] is the first responses after the first plugin.
35
     *
36
     * @var array
37
     */
38
    private $responses;
39
40
    /**
41
     * @param array $failure   if the response was successful or not
42
     * @param array $requests
43
     * @param array $responses
44
     */
45
    public function __construct(array $failure, array $requests, array $responses)
46
    {
47
        $this->failure = $failure;
48
        $this->requests = $requests;
49
        $this->responses = $responses;
50
    }
51
52
    /**
53
     * Create an array of ClientDataCollector from collected data.
54
     *
55
     * @param array $data
56
     *
57
     * @return ClientDataProvider[]
58
     */
59
    public static function createFromCollectedData(array $data)
60
    {
61
        $clientData = [];
62
        foreach ($data as $clientName => $messages) {
63
            $clientData[$clientName] = static::createOne($messages);
0 ignored issues
show
Bug introduced by
Since createOne() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createOne() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
64
        }
65
66
        return $clientData;
67
    }
68
69
    /**
70
     * @param array $messages is an array with keys 'failure', 'request' and 'response' which hold requests for each call to
71
     *                        sendRequest and for each depth.
72
     *
73
     * @return ClientDataProvider
74
     */
75
    private static function createOne($messages)
76
    {
77
        $orderedFaulure = [];
78
        $orderedRequests = [];
79
        $orderedResponses = [];
80
81
        foreach ($messages['failure'] as $depth => $failures) {
82
            foreach ($failures as $idx => $failure) {
83
                $orderedFaulure[$idx][$depth] = $failure;
84
            }
85
        }
86
87
        foreach ($messages['request'] as $depth => $requests) {
88
            foreach ($requests as $idx => $request) {
89
                $orderedRequests[$idx][$depth] = $request;
90
            }
91
        }
92
93
        foreach ($messages['response'] as $depth => $responses) {
94
            foreach ($responses as $idx => $response) {
95
                $orderedResponses[$idx][$depth] = $response;
96
            }
97
        }
98
99
        return new self($orderedFaulure, $orderedRequests, $orderedResponses);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function getRequests()
106
    {
107
        return $this->requests;
108
    }
109
110
    /**
111
     * @param array $requests
112
     *
113
     * @return ClientDataProvider
114
     */
115
    public function setRequests($requests)
116
    {
117
        $this->requests = $requests;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getResponses()
126
    {
127
        return $this->responses;
128
    }
129
130
    /**
131
     * @param array $responses
132
     *
133
     * @return ClientDataProvider
134
     */
135
    public function setResponses($responses)
136
    {
137
        $this->responses = $responses;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the index keys for the request and response stacks.
144
     *
145
     * @return array
146
     */
147
    public function getStackIndexKeys()
148
    {
149
        return array_keys($this->requests);
150
    }
151
152
    /**
153
     * @param int $idx
154
     *
155
     * @return array responses
156
     */
157
    public function getRequstStack($idx)
158
    {
159
        return $this->requests[$idx];
160
    }
161
162
    /**
163
     * @param int $idx
164
     *
165
     * @return array responses
166
     */
167
    public function getResponseStack($idx)
168
    {
169
        return $this->responses[$idx];
170
    }
171
172
    /**
173
     * @param int $idx
174
     *
175
     * @return array failures
176
     */
177
    public function getFailureStack($idx)
178
    {
179
        return $this->failure[$idx];
180
    }
181
}
182