Completed
Pull Request — master (#127)
by Pascal
08:48
created

RequestStackProvider::getResponseStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
/**
6
 * An object that managed collected data for each 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
final class RequestStackProvider
14
{
15
    /**
16
     * Array that tell if a request errored or not. true = success, false = failure.
17
     *
18
     * @var array
19
     */
20
    private $failures;
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 $failures  if the response was successful or not
42
     * @param array $requests
43
     * @param array $responses
44
     */
45
    public function __construct(array $failures, array $requests, array $responses)
46
    {
47
        $this->failures = $failures;
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 RequestStackProvider[]
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
Comprehensibility introduced by
Since Http\HttplugBundle\Collector\RequestStackProvider is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

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 RequestStackProvider
74
     */
75
    private static function createOne($messages)
76
    {
77
        $orderedFailure = [];
78
        $orderedRequests = [];
79
        $orderedResponses = [];
80
81
        foreach ($messages['failure'] as $depth => $failures) {
82
            foreach ($failures as $idx => $failure) {
83
                $orderedFailure[$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($orderedFailure, $orderedRequests, $orderedResponses);
100
    }
101
102
    /**
103
     * Get the index keys for the request and response stacks.
104
     *
105
     * @return array
106
     */
107
    public function getStackIndexKeys()
108
    {
109
        return array_keys($this->requests);
110
    }
111
112
    /**
113
     * @param int $idx
114
     *
115
     * @return array
116
     */
117
    public function getRequestStack($idx)
118
    {
119
        return $this->requests[$idx];
120
    }
121
122
    /**
123
     * @param int $idx
124
     *
125
     * @return array
126
     */
127
    public function getResponseStack($idx)
128
    {
129
        return $this->responses[$idx];
130
    }
131
132
    /**
133
     * @param int $idx
134
     *
135
     * @return array
136
     */
137
    public function getFailureStack($idx)
138
    {
139
        return $this->failures[$idx];
140
    }
141
}
142