3030use App \Services \InfoProviderSystem \DTOs \PriceDTO ;
3131use App \Services \InfoProviderSystem \DTOs \PurchaseInfoDTO ;
3232use App \Services \InfoProviderSystem \DTOs \SearchResultDTO ;
33- use App \Services \OAuth \OAuthTokenManager ;
3433use App \Settings \InfoProviderSystem \BuerklinSettings ;
3534use Psr \Cache \CacheItemPoolInterface ;
3635use Symfony \Contracts \HttpClient \HttpClientInterface ;
@@ -51,10 +50,9 @@ class BuerklinProvider implements BatchInfoProviderInterface
5150
5251 public function __construct (
5352 private readonly HttpClientInterface $ client ,
54- private readonly OAuthTokenManager $ authTokenManager ,
5553 private readonly CacheItemPoolInterface $ partInfoCache ,
5654 private readonly BuerklinSettings $ settings ,
57- ) {
55+ ) {
5856
5957 }
6058
@@ -64,16 +62,52 @@ public function __construct(
6462 */
6563 private function getToken (): string
6664 {
67- //Check if we already have a token saved for this app, otherwise we have to retrieve one via OAuth
68- if (!$ this ->authTokenManager ->hasToken (self ::OAUTH_APP_NAME )) {
69- $ this ->authTokenManager ->retrieveROPCToken (self ::OAUTH_APP_NAME , $ this ->settings ->username , $ this ->settings ->password );
65+ // Cache token to avoid hammering the auth server on every request
66+ $ cacheKey = 'buerklin.oauth.token ' ;
67+ $ item = $ this ->partInfoCache ->getItem ($ cacheKey );
68+
69+ if ($ item ->isHit ()) {
70+ $ token = $ item ->get ();
71+ if (is_string ($ token ) && $ token !== '' ) {
72+ return $ token ;
73+ }
74+ }
75+
76+ // Buerklin OAuth2 password grant (ROPC)
77+ $ resp = $ this ->client ->request ('POST ' , 'https://www.buerklin.com/authorizationserver/oauth/token/ ' , [
78+ 'headers ' => [
79+ 'Accept ' => 'application/json ' ,
80+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
81+ ],
82+ 'body ' => [
83+ 'grant_type ' => 'password ' ,
84+ 'client_id ' => $ this ->settings ->clientId ,
85+ 'client_secret ' => $ this ->settings ->secret ,
86+ 'username ' => $ this ->settings ->username ,
87+ 'password ' => $ this ->settings ->password ,
88+ ],
89+ ]);
90+
91+ $ data = $ resp ->toArray (false );
92+
93+ if (!isset ($ data ['access_token ' ])) {
94+ throw new \RuntimeException (
95+ 'Invalid token response from Buerklin: HTTP ' . $ resp ->getStatusCode () . ' body= ' . $ resp ->getContent (false )
96+ );
7097 }
7198
72- $ token = $ this ->authTokenManager ->getAlwaysValidTokenString (self ::OAUTH_APP_NAME );
73- if ($ token === null ) {
74- throw new \RuntimeException ('Could not retrieve OAuth token for Buerklin API. ' );
99+ $ token = (string ) $ data ['access_token ' ];
100+
101+ // Cache for (expires_in - 30s) if available
102+ $ ttl = 300 ;
103+ if (isset ($ data ['expires_in ' ]) && is_numeric ($ data ['expires_in ' ])) {
104+ $ ttl = max (60 , (int ) $ data ['expires_in ' ] - 30 );
75105 }
76106
107+ $ item ->set ($ token );
108+ $ item ->expiresAfter ($ ttl );
109+ $ this ->partInfoCache ->save ($ item );
110+
77111 return $ token ;
78112 }
79113
0 commit comments