Completed
Pull Request — master (#84)
by Tobias
18:41 queued 08:43
created

ClientDataCollector::getStackIndexKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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