33namespace Http \HttplugBundle \Collector ;
44
55use Http \Client \Common \FlexibleHttpClient ;
6+ use Http \Client \Exception \HttpException ;
67use Http \Client \HttpAsyncClient ;
78use Http \Client \HttpClient ;
89use Psr \Http \Message \RequestInterface ;
10+ use Psr \Http \Message \ResponseInterface ;
911
1012/**
1113 * The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
@@ -27,12 +29,18 @@ class ProfileClient implements HttpClient, HttpAsyncClient
2729 */
2830 private $ collector ;
2931
32+ /**
33+ * @var Formatter
34+ */
35+ private $ formatter ;
36+
3037 /**
3138 * @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
3239 * HttpAsyncClient interfaces.
3340 * @param Collector $collector
41+ * @param Formatter $formatter
3442 */
35- public function __construct ($ client , Collector $ collector )
43+ public function __construct ($ client , Collector $ collector, Formatter $ formatter )
3644 {
3745 if (!($ client instanceof HttpClient && $ client instanceof HttpAsyncClient)) {
3846 throw new \RuntimeException (sprintf (
@@ -45,39 +53,94 @@ public function __construct($client, Collector $collector)
4553 }
4654 $ this ->client = $ client ;
4755 $ this ->collector = $ collector ;
56+ $ this ->formatter = $ formatter ;
4857 }
4958
5059 /**
5160 * {@inheritdoc}
5261 */
5362 public function sendAsyncRequest (RequestInterface $ request )
5463 {
55- $ this ->collectRequestInformations ($ request );
64+ $ stack = $ this ->collector ->getCurrentStack ();
65+ $ this ->collectRequestInformations ($ request , $ stack );
5666
57- return $ this ->client ->sendAsyncRequest ($ request );
67+ return $ this ->client ->sendAsyncRequest ($ request )->then (function (ResponseInterface $ response ) use ($ stack ) {
68+ $ this ->collectResponseInformations ($ response , $ stack );
69+
70+ return $ response ;
71+ }, function (\Exception $ exception ) use ($ stack ) {
72+ $ this ->collectExceptionInformations ($ exception , $ stack );
73+
74+ throw $ exception ;
75+ });
5876 }
5977
6078 /**
6179 * {@inheritdoc}
6280 */
6381 public function sendRequest (RequestInterface $ request )
6482 {
65- $ this ->collectRequestInformations ($ request );
83+ $ stack = $ this ->collector ->getCurrentStack ();
84+ $ this ->collectRequestInformations ($ request , $ stack );
85+
86+ try {
87+ $ response = $ this ->client ->sendRequest ($ request );
6688
67- return $ this ->client ->sendRequest ($ request );
89+ $ this ->collectResponseInformations ($ response , $ stack );
90+
91+ return $ response ;
92+ } catch (\Exception $ e ) {
93+ $ this ->collectExceptionInformations ($ e , $ stack );
94+
95+ throw $ e ;
96+ }
6897 }
6998
7099 /**
71100 * @param RequestInterface $request
101+ * @param Stack|null $stack
72102 */
73- private function collectRequestInformations (RequestInterface $ request )
103+ private function collectRequestInformations (RequestInterface $ request, Stack $ stack = null )
74104 {
75- if (!$ stack = $ this -> collector -> getCurrentStack () ) {
105+ if (!$ stack ) {
76106 return ;
77107 }
78108
79- $ stack = $ this ->collector ->getCurrentStack ();
80109 $ stack ->setRequestTarget ($ request ->getRequestTarget ());
81110 $ stack ->setRequestMethod ($ request ->getMethod ());
111+ $ stack ->setRequestScheme ($ request ->getUri ()->getScheme ());
112+ $ stack ->setRequestHost ($ request ->getUri ()->getHost ());
113+ $ stack ->setClientRequest ($ this ->formatter ->formatRequest ($ request ));
114+ }
115+
116+ /**
117+ * @param ResponseInterface $response
118+ * @param Stack|null $stack
119+ */
120+ private function collectResponseInformations (ResponseInterface $ response , Stack $ stack = null )
121+ {
122+ if (!$ stack ) {
123+ return ;
124+ }
125+
126+ $ stack ->setResponseCode ($ response ->getStatusCode ());
127+ $ stack ->setClientResponse ($ this ->formatter ->formatResponse ($ response ));
128+ }
129+
130+ /**
131+ * @param \Exception $exception
132+ * @param Stack|null $stack
133+ */
134+ private function collectExceptionInformations (\Exception $ exception , Stack $ stack = null )
135+ {
136+ if ($ exception instanceof HttpException) {
137+ $ this ->collectResponseInformations ($ exception ->getResponse (), $ stack );
138+ }
139+
140+ if (!$ stack ) {
141+ return ;
142+ }
143+
144+ $ stack ->setClientException ($ this ->formatter ->formatException ($ exception ));
82145 }
83146}
0 commit comments