@@ -65,42 +65,75 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6565
6666 // wrap the next callback to profile the plugin request changes
6767 $ wrappedNext = function (RequestInterface $ request ) use ($ next , $ profile ) {
68- $ profile -> setRequest ( $ this ->formatter -> formatRequest ($ request) );
68+ $ this ->onOutgoingRequest ($ request, $ profile );
6969
7070 return $ next ($ request );
7171 };
7272
7373 // wrap the first callback to profile the plugin request changes
7474 $ wrappedFirst = function (RequestInterface $ request ) use ($ first , $ profile ) {
75- $ profile -> setRequest ( $ this ->formatter -> formatRequest ($ request) );
75+ $ this ->onOutgoingRequest ($ request, $ profile );
7676
7777 return $ first ($ request );
7878 };
7979
8080 try {
8181 $ promise = $ this ->plugin ->handleRequest ($ request , $ wrappedNext , $ wrappedFirst );
8282 } 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 );
8684
8785 throw $ e ;
8886 }
8987
9088 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 );
9390
9491 return $ response ;
9592 }, 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 );
9994
10095 throw $ exception ;
10196 });
10297 }
10398
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+
104137 /**
105138 * Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
106139 * and the cache is hit without re-validation.
0 commit comments