@@ -25,7 +25,7 @@ class RequestContext
2525 private ?BannerMessageManager $ banner_message_manager ;
2626 // Should use an enum, TODO
2727 private string $ environment ;
28- private string $ api_key_in_use ;
28+ private ? string $ api_key_in_use ;
2929
3030 private string $ user_api_key_header = 'X-WPT-API-KEY ' ;
3131
@@ -49,7 +49,7 @@ public function __construct(array $global_request, array $server = [], array $op
4949 $ this ->host = $ options ['host ' ] ?? Util::getSetting ('host ' , "" );
5050
5151 $ this ->environment = Environment::$ Production ;
52- $ this ->api_key_in_use = "" ;
52+ $ this ->api_key_in_use = null ;
5353 }
5454
5555 public function getRaw (): array
@@ -160,19 +160,20 @@ public function getEnvironment(): string
160160 * */
161161 public function getApiKeyInUse (): string
162162 {
163- if (empty ($ this ->api_key_in_use )) {
164- $ user_api_key_header = $ this ->user_api_key_header ;
165- $ request_headers = getallheaders ();
166- $ matching_headers = array_filter ($ request_headers , function ($ k ) use ($ user_api_key_header ) {
167- return strtolower ($ k ) == strtolower ($ user_api_key_header );
168- }, ARRAY_FILTER_USE_KEY );
169- if (!empty ($ matching_headers )) {
170- $ user_api_key = array_values ($ matching_headers )[0 ];
171- }
172-
173- $ this ->api_key_in_use = $ user_api_key ;
163+ if ($ this ->api_key_in_use == null ) {
164+ $ this ->api_key_in_use = $ this ->readApiKey ();
174165 }
175-
176166 return $ this ->api_key_in_use ;
177167 }
168+
169+ private function readApiKey ()
170+ {
171+ $ request_headers = getallheaders ();
172+ foreach ($ request_headers as $ k => $ value ) {
173+ if (strtolower ($ k ) == strtolower ($ this ->user_api_key_header )) {
174+ return trim ($ value );
175+ }
176+ }
177+ return '' ;
178+ }
178179}
0 commit comments