made calls cacheable

This commit is contained in:
2024-09-13 02:09:15 +02:00
parent 4102902b34
commit 461b074988
4 changed files with 889 additions and 483 deletions

View File

@@ -12,7 +12,9 @@
"require": {
"ext-curl": "*",
"kbsali/redmine-api": "^2.7",
"guzzlehttp/guzzle": "^7"
"guzzlehttp/guzzle": "^7",
"symfony/contracts": "^3.5",
"symfony/cache": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5 || ^10",

1300
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,40 +5,57 @@ namespace Magdev\RedmineBundle\Client;
use Redmine\Client\Psr18Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
final class RedmineClient
{
private ?Psr18Client $client = null;
private ?string $url = null;
private ?string $apiKey = null;
public function __construct(
string $url,
string $apikeyOrUsername,
?string $password = null
) {
$guzzle = new GuzzleClient();
$factory = new GuzzleHttpFactory();
private TagAwareCacheInterface $redmineApiCachePool
) {}
$this->client = new Psr18Client(
$guzzle,
$factory,
$factory,
$url,
$apikeyOrUsername,
$password
);
public function setUrl(string $url): self
{
$this->url = $url;
}
public function __call(mixed $name, mixed $arguments): mixed
public function setApiKey(string $apiKey): self
{
if (!method_exists($this->client, $name)) {
throw new \BadMethodCallException(sprintf(
'Method %s, doesn\'t exist in class %s',
$name,
get_class($this->client)
));
}
$this->apiKey = $apiKey;
}
return call_user_func_array([$this->client, $name], $arguments);
public function call(string $api, string $method, array $arguments = []): mixed
{
$cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments)));
return $this->redmineApiCachePool->get($cacheKey, function (ItemInterface $item): string {
$item->expiresAfter(3600);
$item->tag([$api, $method]);
$apiClient = $this->getClient()->getApi($api);
$value = call_user_method_array($method, $apiClient, $arguments);
return $value;
});
}
public function getClient(): ?Psr18Client
{
if (!$this->client) {
$guzzle = new GuzzleClient();
$factory = new GuzzleHttpFactory();
$this->client = new Psr18Client(
$guzzle,
$factory,
$factory,
$this->url,
$this->apiKey
);
}
return $this->client;
}
}

View File

@@ -23,7 +23,8 @@ final class MagdevRedmineExtension extends Extension
$id = sprintf('magdev.redmine.%s', $name);
$container->register($id, RedmineClient::class)
->setArguments([$connection['url'], $connection['apikey']]);
->addMethodCall('setUrl', [$connection['url']]);
->addMethodCall('setApiKey', [$connection['apikey']]);
$container->registerAliasForArgument($id, RedmineClient::class, "{$name}Client");
if ($name === $config['default_connection']) {