@@ -65,42 +65,75 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
65
65
66
66
// wrap the next callback to profile the plugin request changes
67
67
$ wrappedNext = function (RequestInterface $ request ) use ($ next , $ profile ) {
68
- $ profile -> setRequest ( $ this ->formatter -> formatRequest ($ request) );
68
+ $ this ->onOutgoingRequest ($ request, $ profile );
69
69
70
70
return $ next ($ request );
71
71
};
72
72
73
73
// wrap the first callback to profile the plugin request changes
74
74
$ wrappedFirst = function (RequestInterface $ request ) use ($ first , $ profile ) {
75
- $ profile -> setRequest ( $ this ->formatter -> formatRequest ($ request) );
75
+ $ this ->onOutgoingRequest ($ request, $ profile );
76
76
77
77
return $ first ($ request );
78
78
};
79
79
80
80
try {
81
81
$ promise = $ this ->plugin ->handleRequest ($ request , $ wrappedNext , $ wrappedFirst );
82
82
} catch (Exception $ e ) {
83
- $ profile ->setFailed (true );
84
- $ profile ->setResponse ($ this ->formatter ->formatException ($ e ));
85
- $ this ->collectRequestInformation ($ request , $ stack );
83
+ $ this ->onException ($ request , $ profile , $ e , $ stack );
86
84
87
85
throw $ e ;
88
86
}
89
87
90
88
return $ promise ->then (function (ResponseInterface $ response ) use ($ profile , $ request , $ stack ) {
91
- $ profile ->setResponse ($ this ->formatter ->formatResponse ($ response ));
92
- $ this ->collectRequestInformation ($ request , $ stack );
89
+ $ this ->onOutgoingResponse ($ response , $ profile , $ request , $ stack );
93
90
94
91
return $ response ;
95
92
}, function (Exception $ exception ) use ($ profile , $ request , $ stack ) {
96
- $ profile ->setFailed (true );
97
- $ profile ->setResponse ($ this ->formatter ->formatException ($ exception ));
98
- $ this ->collectRequestInformation ($ request , $ stack );
93
+ $ this ->onException ($ request , $ profile , $ exception , $ stack );
99
94
100
95
throw $ exception ;
101
96
});
102
97
}
103
98
99
+ /**
100
+ * @param RequestInterface $request
101
+ * @param Profile $profile
102
+ * @param Exception $exception
103
+ * @param Stack $stack
104
+ */
105
+ private function onException (
106
+ RequestInterface $ request ,
107
+ Profile $ profile ,
108
+ Exception $ exception ,
109
+ Stack $ stack = null
110
+ ) {
111
+ $ profile ->setFailed (true );
112
+ $ profile ->setResponse ($ this ->formatter ->formatException ($ exception ));
113
+ $ this ->collectRequestInformation ($ request , $ stack );
114
+ }
115
+
116
+ /**
117
+ * @param RequestInterface $request
118
+ * @param Profile $profile
119
+ */
120
+ private function onOutgoingRequest (RequestInterface $ request , Profile $ profile )
121
+ {
122
+ $ profile ->setRequest ($ this ->formatter ->formatRequest ($ request ));
123
+ }
124
+
125
+ /**
126
+ * @param ResponseInterface $response
127
+ * @param Profile $profile
128
+ * @param RequestInterface $request
129
+ * @param Stack $stack
130
+ */
131
+ private function onOutgoingResponse (ResponseInterface $ response , Profile $ profile , RequestInterface $ request , Stack $ stack = null )
132
+ {
133
+ $ profile ->setResponse ($ this ->formatter ->formatResponse ($ response ));
134
+ $ this ->collectRequestInformation ($ request , $ stack );
135
+ }
136
+
104
137
/**
105
138
* Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
106
139
* and the cache is hit without re-validation.
0 commit comments