| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\KeyManagement; |
| 6 |
|
| 7 |
use Psr\Cache\CacheItemPoolInterface; |
| 8 |
use Psr\Http\Client\ClientInterface; |
| 9 |
use Psr\Http\Message\RequestFactoryInterface; |
| 10 |
use RuntimeException; |
| 11 |
use Symfony\Component\Cache\Adapter\NullAdapter; |
| 12 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 13 |
use function assert; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
abstract class UrlKeySetFactory |
| 19 |
{ |
| 20 |
private CacheItemPoolInterface $cacheItemPool; |
| 21 |
|
| 22 |
private int $expiresAfter = 3600; |
| 23 |
|
| 24 |
public function __construct( |
| 25 |
private readonly ClientInterface|HttpClientInterface $client, |
| 26 |
private readonly null|RequestFactoryInterface $requestFactory = null |
| 27 |
) { |
| 28 |
if ($this->client instanceof ClientInterface) { |
| 29 |
trigger_deprecation( |
| 30 |
'web-token/jwt-library', |
| 31 |
'3.3', |
| 32 |
'Using "%s" with an instance of "%s" is deprecated, use "%s" instead.', |
| 33 |
self::class, |
| 34 |
ClientInterface::class, |
| 35 |
HttpClientInterface::class |
| 36 |
); |
| 37 |
} |
| 38 |
if (! $this->client instanceof HttpClientInterface && $this->requestFactory === null) { |
| 39 |
throw new RuntimeException(sprintf( |
| 40 |
'The request factory must be provided when using an instance of "%s" as client.', |
| 41 |
ClientInterface::class |
| 42 |
)); |
| 43 |
} |
| 44 |
$this->cacheItemPool = new NullAdapter(); |
| 45 |
} |
| 46 |
|
| 47 |
public function enabledCache(CacheItemPoolInterface $cacheItemPool, int $expiresAfter = 3600): void |
| 48 |
{ |
| 49 |
$this->cacheItemPool = $cacheItemPool; |
| 50 |
$this->expiresAfter = $expiresAfter; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected function getContent(string $url, array $header = []): string |
| 57 |
{ |
| 58 |
$cacheKey = hash('xxh128', $url); |
| 59 |
$item = $this->cacheItemPool->getItem($cacheKey); |
| 60 |
if ($item->isHit()) { |
| 61 |
return $item->get(); |
| 62 |
} |
| 63 |
|
| 64 |
$content = $this->client instanceof HttpClientInterface ? $this->sendSymfonyRequest( |
| 65 |
$url, |
| 66 |
$header |
| 67 |
) : $this->sendPsrRequest($url, $header); |
| 68 |
$item = $this->cacheItemPool->getItem($cacheKey); |
| 69 |
$item->expiresAfter($this->expiresAfter); |
| 70 |
$item->set($content); |
| 71 |
$this->cacheItemPool->save($item); |
| 72 |
|
| 73 |
return $content; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private function sendSymfonyRequest(string $url, array $header = []): string |
| 80 |
{ |
| 81 |
assert($this->client instanceof HttpClientInterface); |
| 82 |
$response = $this->client->request('GET', $url, [ |
| 83 |
'headers' => $header, |
| 84 |
]); |
| 85 |
|
| 86 |
if ($response->getStatusCode() >= 400) { |
| 87 |
throw new RuntimeException('Unable to get the key set.', $response->getStatusCode()); |
| 88 |
} |
| 89 |
|
| 90 |
return $response->getContent(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private function sendPsrRequest(string $url, array $header = []): string |
| 97 |
{ |
| 98 |
assert($this->client instanceof ClientInterface); |
| 99 |
assert($this->requestFactory instanceof RequestFactoryInterface); |
| 100 |
$request = $this->requestFactory->createRequest('GET', $url); |
| 101 |
foreach ($header as $k => $v) { |
| 102 |
$request = $request->withHeader($k, $v); |
| 103 |
} |
| 104 |
$response = $this->client->sendRequest($request); |
| 105 |
|
| 106 |
if ($response->getStatusCode() >= 400) { |
| 107 |
throw new RuntimeException('Unable to get the key set.', $response->getStatusCode()); |
| 108 |
} |
| 109 |
|
| 110 |
return $response->getBody() |
| 111 |
->getContents(); |
| 112 |
} |
| 113 |
} |