11<?php
22
3+ declare (strict_types=1 );
4+
35namespace CraigPotter \Barstool ;
46
57use Saloon \Http \Response ;
68use Illuminate \Support \Str ;
79use InvalidArgumentException ;
810use Saloon \Http \PendingRequest ;
11+ use Psr \Http \Message \UriInterface ;
12+ use Saloon \Contracts \Body \BodyRepository ;
913use Saloon \Repositories \Body \StreamBodyRepository ;
1014use Saloon \Exceptions \Request \FatalRequestException ;
1115
@@ -44,6 +48,17 @@ public static function record(PendingRequest|Response|FatalRequestException $dat
4448 };
4549 }
4650
51+ /**
52+ * @return array{
53+ * connector_class: class-string,
54+ * request_class: class-string,
55+ * method: string,
56+ * url: string,
57+ * request_headers: array<string, string>|null,
58+ * request_body: BodyRepository|string|null,
59+ * successful: false
60+ * }
61+ */
4762 private static function getRequestData (PendingRequest $ request ): array
4863 {
4964 $ body = $ request ->body ();
@@ -63,6 +78,16 @@ private static function getRequestData(PendingRequest $request): array
6378 ];
6479 }
6580
81+ /**
82+ * @return array{
83+ * url: UriInterface,
84+ * status: 'failed'|'successful',
85+ * response_headers: array<string, mixed>,
86+ * response_body: string,
87+ * response_status: int,
88+ * successful: bool
89+ * }
90+ */
6691 private static function getResponseData (Response $ response ): array
6792 {
6893 $ responseBody = self ::getResponseBody ($ response );
@@ -77,6 +102,17 @@ private static function getResponseData(Response $response): array
77102 ];
78103 }
79104
105+ /**
106+ * @return array{
107+ * url: UriInterface,
108+ * status: 'fatal',
109+ * response_headers: null,
110+ * response_body: null,
111+ * response_status: null,
112+ * successful: false,
113+ * fatal_error: string
114+ * }
115+ */
80116 private static function getFatalData (FatalRequestException $ exception ): array
81117 {
82118 return [
@@ -102,14 +138,16 @@ private static function recordRequest(PendingRequest $data): void
102138 $ entry ->save ();
103139 }
104140
105- private static function recordResponse (Response | PendingRequest $ data ): void
141+ private static function recordResponse (Response $ data ): void
106142 {
107- $ uuid = $ data ->getPsrRequest ()->getHeader ('X-Barstool-UUID ' )[0 ] ?? null ;
143+ $ psrRequest = $ data ->getPsrRequest ();
144+
145+ $ uuid = $ psrRequest ->getHeader ('X-Barstool-UUID ' )[0 ] ?? null ;
108146 if (is_null ($ uuid )) {
109147 return ;
110148 }
111149
112- $ entry = Models \Barstool::where ( 'uuid ' , $ uuid)-> first ( );
150+ $ entry = Models \Barstool::query ()-> firstWhere ( 'uuid ' , $ uuid );
113151
114152 if ($ entry ) {
115153 $ entry ->fill ([
@@ -120,20 +158,22 @@ private static function recordResponse(Response|PendingRequest $data): void
120158 }
121159 }
122160
123- /**
124- * @param Response|PendingRequest $data
125- */
126- public static function calculateDuration (Response |PendingRequest |FatalRequestException $ data ): mixed
161+ public static function calculateDuration (Response |PendingRequest $ data ): int
127162 {
128- return $ data ->getConnector ()->config ()->get ('barstool-response-time ' , microtime (true ) * 1000 ) - $ data ->getConnector ()->config ()->get ('barstool-request-time ' );
163+ $ config = $ data ->getConnector ()->config ();
164+
165+ $ requestTime = (int ) $ config ->get ('barstool-request-time ' );
166+ $ responseTime = (int ) $ config ->get ('barstool-response-time ' , microtime (true ) * 1000 );
167+
168+ return $ responseTime - $ requestTime ;
129169 }
130170
131171 private static function recordFatal (FatalRequestException $ data ): void
132172 {
133173 $ pendingRequest = $ data ->getPendingRequest ();
134174 $ uuid = $ pendingRequest ->headers ()->get ('X-Barstool-UUID ' );
135175
136- $ entry = Models \Barstool::where ( 'uuid ' , $ uuid)-> first ( );
176+ $ entry = Models \Barstool::query ()-> firstWhere ( 'uuid ' , $ uuid );
137177
138178 if ($ entry ) {
139179 $ entry ->fill ([
@@ -147,7 +187,7 @@ private static function recordFatal(FatalRequestException $data): void
147187 /**
148188 * Get the supported content types for response bodies.
149189 *
150- * @return array< string>
190+ * @return string[]
151191 */
152192 private static function supportedContentTypes (): array
153193 {
@@ -160,6 +200,9 @@ private static function supportedContentTypes(): array
160200 ];
161201 }
162202
203+ /**
204+ * @return array<string, string>|null
205+ */
163206 public static function getRequestHeaders (PendingRequest $ request ): ?array
164207 {
165208 $ excludedHeaders = config ('barstool.excluded_request_headers ' , []);
0 commit comments