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

ClientDataCollector::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.
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 ClientDataCollector
14
{
15
    /**
16
     * A multidimensional array with requests.
17
     * $requests[0][0] is the first request before all plugins.
18
     * $requests[0][1] is the first request after the first plugin.
19
     *
20
     * @var array
21
     */
22
    private $requests;
23
24
    /**
25
     * A multidimensional array with responses.
26
     * $responses[0][0] is the first responses before all plugins.
27
     * $responses[0][1] is the first responses after the first plugin.
28
     *
29
     * @var array
30
     */
31
    private $responses;
32
33
    /**
34
     *
35
     * @param array $requests
36
     * @param array $responses
37
     */
38
    public function __construct(array $requests, array $responses)
39
    {
40
        $this->requests = $requests;
41
        $this->responses = $responses;
42
    }
43
44
    /**
45
     * Create an array of ClientDataCollector from collected data.
46
     *
47
     * @param array $data
48
     *
49
     * @return ClientDataCollector[]
50
     */
51
    public static function createFromCollectedData(array $data)
52
    {
53
        $clientData = [];
54
        foreach ($data as $clientName => $messages) {
55
            $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...
56
        }
57
58
        return $clientData;
59
    }
60
61
    /**
62
     * @param array $messages is an array with keys 'request' and 'response' which hold requests for each call to
63
     * sendRequest and for each depth.   
64
     *
65
     * @return ClientDataCollector
66
     */
67
    private static function createOne($messages) 
68
    {
69
        $orderedRequests = [];
70
        $orderedResponses = [];
71
72
        foreach ($messages['request'] as $depth => $requests) {
73
            foreach ($requests as $idx => $request) {
74
                $orderedRequests[$idx][$depth] = $request;
75
            }
76
         }
77
78
        foreach ($messages['response'] as $depth => $responses) {
79
            foreach ($responses as $idx => $response) {
80
                $orderedResponses[$idx][$depth] = $response;
81
            }
82
         }
83
84
        return new self($orderedRequests, $orderedResponses);
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function getRequests()
91
    {
92
        return $this->requests;
93
    }
94
95
    /**
96
     * @param array $requests
97
     *
98
     * @return ClientDataCollector
99
     */
100
    public function setRequests($requests)
101
    {
102
        $this->requests = $requests;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getResponses()
111
    {
112
        return $this->responses;
113
    }
114
115
    /**
116
     * @param array $responses
117
     *
118
     * @return ClientDataCollector
119
     */
120
    public function setResponses($responses)
121
    {
122
        $this->responses = $responses;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Get the index keys for the request and response stacks.
129
     *
130
     * @return array
131
     */
132
    public function getStackIndexKeys()
133
    {
134
        return array_keys($this->requests);
135
    }
136
137
    /**
138
     * @param int $idx
139
     *
140
     * @return array responses
141
     */
142
    public function getRequstStack($idx)
143
    {
144
        return $this->requests[$idx];
145
    }
146
147
    /**
148
     * @param int $idx
149
     *
150
     * @return array responses
151
     */
152
    public function getResponseStack($idx)
153
    {
154
        return $this->responses[$idx];
155
    }
156
}