Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"brick/math": "^0.9|^0.10|^0.11|^0.12",
"paragonie/constant_time_encoding": "^2.6",
"paragonie/sodium_compat": "^1.20",
"psr/cache": "^3.0",
"psr/clock": "^1.0",
"psr/event-dispatcher": "^1.0",
"psr/http-client": "^1.0",
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,11 @@ parameters:
count: 1
path: src/Library/KeyManagement/KeyConverter/RSAKey.php

-
message: "#^Method Jose\\\\Component\\\\KeyManagement\\\\UrlKeySetFactory\\:\\:getContent\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/Library/KeyManagement/UrlKeySetFactory.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
Expand Down
31 changes: 28 additions & 3 deletions src/Library/KeyManagement/UrlKeySetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Jose\Component\KeyManagement;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use RuntimeException;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use function assert;

Expand All @@ -15,6 +17,10 @@
*/
abstract class UrlKeySetFactory
{
private CacheItemPoolInterface $cacheItemPool;

private int $expiresAfter = 3600;

public function __construct(
private readonly ClientInterface|HttpClientInterface $client,
private readonly null|RequestFactoryInterface $requestFactory = null
Expand All @@ -35,17 +41,36 @@ public function __construct(
ClientInterface::class
));
}
$this->cacheItemPool = new NullAdapter();
}

public function enabledCache(CacheItemPoolInterface $cacheItemPool, int $expiresAfter = 3600): void
{
$this->cacheItemPool = $cacheItemPool;
$this->expiresAfter = $expiresAfter;
}

/**
* @param array<string, string|string[]> $header
*/
protected function getContent(string $url, array $header = []): string
{
if ($this->client instanceof HttpClientInterface) {
return $this->sendSymfonyRequest($url, $header);
$cacheKey = hash('xxh128', $url);
$item = $this->cacheItemPool->getItem($cacheKey);
if ($item->isHit()) {
return $item->get();
}
return $this->sendPsrRequest($url, $header);

$content = $this->client instanceof HttpClientInterface ? $this->sendSymfonyRequest(
$url,
$header
) : $this->sendPsrRequest($url, $header);
$item = $this->cacheItemPool->getItem($cacheKey);
$item->expiresAfter($this->expiresAfter);
$item->set($content);
$this->cacheItemPool->save($item);

return $content;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Library/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"brick/math": "^0.9|^0.10|^0.11|^0.12",
"paragonie/constant_time_encoding": "^2.6",
"paragonie/sodium_compat": "^1.20",
"psr/cache": "^3.0",
"psr/clock": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-client": "^1.0",
Expand Down