Files
redmine-bundle/src/Client/RedmineClient.php
2024-09-13 02:52:08 +02:00

53 lines
1.4 KiB
PHP

<?php
namespace Magdev\RedmineBundle\Client;
use Redmine\Client\Psr18Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
final class RedmineClient
{
private ?Psr18Client $client = null;
public function __construct(
private TagAwareCacheInterface $cache,
private string $url,
private string $apiKey
) {}
public function call(string $api, string $method, array $arguments = []): mixed
{
$cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments)));
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($api, $method): array {
$item->expiresAfter(3600);
$item->tag([$api, $method]);
$client = $this->getClient()->getApi($api);
$value = \call_user_func_array([$client, $method], $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;
}
}