2020use Psr \Cache \CacheItemPoolInterface ;
2121use Psr \Http \Message \ResponseInterface ;
2222use Psr \Http \Message \StreamInterface ;
23- use Psr \Http \Message \UriInterface ;
2423use Psr \Log \LoggerInterface ;
2524
2625class AbstractEndpoint
@@ -62,7 +61,7 @@ public function __construct(protected OpenWeatherMap $api)
6261 */
6362 protected function sendRequest (
6463 string $ method ,
65- UriInterface | string $ baseUrl ,
64+ string $ path ,
6665 array $ query = [],
6766 array $ headers = [],
6867 StreamInterface |string $ body = null
@@ -72,13 +71,13 @@ protected function sendRequest(
7271
7372 $ response = $ this ->httpClientBuilder ->getHttpClient ()->send (
7473 $ method ,
75- $ this ->buildUrl ($ baseUrl , $ query ),
74+ $ this ->buildUrl ($ path , $ query ),
7675 $ headers ,
7776 $ body
7877 );
7978
80- if (( $ statusCode = $ response ->getStatusCode () ) >= 400 ) {
81- $ this ->handleResponseErrors ($ response, $ statusCode );
79+ if ($ response ->getStatusCode () >= 400 ) {
80+ $ this ->handleResponseErrors ($ response );
8281 }
8382
8483 return ResponseMediator::toArray ($ response );
@@ -113,13 +112,13 @@ private function configurePlugins(): void
113112 * @throws UnauthorizedException
114113 * @throws BadRequestException
115114 */
116- private function handleResponseErrors (ResponseInterface $ response, int $ statusCode ): void
115+ private function handleResponseErrors (ResponseInterface $ response ): void
117116 {
118117 $ error = new Error (
119118 ResponseMediator::toArray ($ response )
120119 );
121120
122- match ($ statusCode ) {
121+ match ($ response -> getStatusCode () ) {
123122 400 => throw new BadRequestException ($ error ),
124123 401 => throw new UnauthorizedException ($ error ),
125124 404 => throw new NotFoundException ($ error ),
@@ -128,17 +127,13 @@ private function handleResponseErrors(ResponseInterface $response, int $statusCo
128127 };
129128 }
130129
131- private function buildUrl (UriInterface | string $ baseUrl , array $ query ): string
130+ private function buildUrl (string $ path , array $ query ): string
132131 {
133- if ($ baseUrl instanceof UriInterface) {
134- $ baseUrl = (string ) $ baseUrl ;
135- }
136-
137132 // Add application key to all requests
138133 $ query = $ query + [
139134 'appid ' => $ this ->config ->getApplicationKey ()
140135 ];
141136
142- return \sprintf ('%s?%s ' , $ baseUrl , http_build_query ($ query ));
137+ return \sprintf ('%s%s ?%s ' , $ this -> config -> getBaseUrl (), $ path , http_build_query ($ query ));
143138 }
144139}
0 commit comments