Completed
Pull Request — master (#84)
by Tobias
08:55
created

ClientDataProvider::setResponses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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
     * @var array
18
     */
19
    private $failure;
20
21
    /**
22
     * A multidimensional array with requests.
23
     * $requests[0][0] is the first request before all plugins.
24
     * $requests[0][1] is the first request after the first plugin.
25
     *
26
     * @var array
27
     */
28
    private $requests;
29
30
    /**
31
     * A multidimensional array with responses.
32
     * $responses[0][0] is the first responses before all plugins.
33
     * $responses[0][1] is the first responses after the first plugin.
34
     *
35
     * @var array
36
     */
37
    private $responses;
38
39
    /**
40
     * @param array $failure if the response was successful or not
41
     * @param array $requests
42
     * @param array $responses
43
     */
44
    public function __construct(array $failure, array $requests, array $responses)
45
    {
46
        $this->failure = $failure;
47
        $this->requests = $requests;
48
        $this->responses = $responses;
49
    }
50
51
    /**
52
     * Create an array of ClientDataCollector from collected data.
53
     *
54
     * @param array $data
55
     *
56
     * @return ClientDataProvider[]
57
     */
58
    public static function createFromCollectedData(array $data)
59
    {
60
        $clientData = [];
61
        foreach ($data as $clientName => $messages) {
62
            $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...
63
        }
64
65
        return $clientData;
66
    }
67
68
    /**
69
     * @param array $messages is an array with keys 'failure', 'request' and 'response' which hold requests for each call to
70
     *                        sendRequest and for each depth.
71
     *
72
     * @return ClientDataProvider
73
     */
74
    private static function createOne($messages)
75
    {
76
        $orderedFaulure = [];
77
        $orderedRequests = [];
78
        $orderedResponses = [];
79
80
        foreach ($messages['failure'] as $depth => $failures) {
81
            foreach ($failures as $idx => $failure) {
82
                $orderedFaulure[$idx][$depth] = $failure;
83
            }
84
        }
85
86
        foreach ($messages['request'] as $depth => $requests) {
87
            foreach ($requests as $idx => $request) {
88
                $orderedRequests[$idx][$depth] = $request;
89
            }
90
        }
91
92
        foreach ($messages['response'] as $depth => $responses) {
93
            foreach ($responses as $idx => $response) {
94
                $orderedResponses[$idx][$depth] = $response;
95
            }
96
        }
97
98
        return new self($orderedFaulure, $orderedRequests, $orderedResponses);
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getRequests()
105
    {
106
        return $this->requests;
107
    }
108
109
    /**
110
     * @param array $requests
111
     *
112
     * @return ClientDataProvider
113
     */
114
    public function setRequests($requests)
115
    {
116
        $this->requests = $requests;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getResponses()
125
    {
126
        return $this->responses;
127
    }
128
129
    /**
130
     * @param array $responses
131
     *
132
     * @return ClientDataProvider
133
     */
134
    public function setResponses($responses)
135
    {
136
        $this->responses = $responses;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Get the index keys for the request and response stacks.
143
     *
144
     * @return array
145
     */
146
    public function getStackIndexKeys()
147
    {
148
        return array_keys($this->requests);
149
    }
150
151
    /**
152
     * @param int $idx
153
     *
154
     * @return array responses
155
     */
156
    public function getRequstStack($idx)
157
    {
158
        return $this->requests[$idx];
159
    }
160
161
    /**
162
     * @param int $idx
163
     *
164
     * @return array responses
165
     */
166
    public function getResponseStack($idx)
167
    {
168
        return $this->responses[$idx];
169
    }
170
171
    /**
172
     * @param int $idx
173
     *
174
     * @return array failures
175
     */
176
    public function getFailureStack($idx)
177
    {
178
        return $this->failure[$idx];
179
    }
180
}
181