diff --git a/config/services.yaml b/config/services.yaml index 6f66cca..bf9fb01 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -1,5 +1,10 @@ services: Magdev\RedmineBundle\Client\RedmineClient: - arguments: ['@redmine_api.cache', '%magdev_redmine.url%', '%magdev_redmine.apiKey%', '%magdev_redmine.cache.ttl%'] + arguments: + - '@redmine_api.cache' + - '%magdev_redmine.url%' + - '%magdev_redmine.apiKey%' + - '%magdev_redmine.cache.ttl%' + - '%magdev_redmine.cache.enabled%' redmine.api: '@Magdev\RedmineBundle\Client\RedmineClient' diff --git a/src/Client/RedmineClient.php b/src/Client/RedmineClient.php index de2ef8d..cb55f1a 100644 --- a/src/Client/RedmineClient.php +++ b/src/Client/RedmineClient.php @@ -6,14 +6,13 @@ use Redmine\Client\Psr18Client; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory; use Redmine\Api; -use Redmine\Client\Client; use Redmine\Http\HttpClient; use Redmine\Http\Request; use Redmine\Http\Response; use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\Cache\TagAwareCacheInterface; -final class RedmineClient implements Client, HttpClient +final class RedmineClient implements RedmineClientInterface, HttpClient { private ?Psr18Client $client = null; @@ -21,11 +20,21 @@ final class RedmineClient implements Client, HttpClient private TagAwareCacheInterface $cache, private string $url, private string $apiKey, - private int $ttl + private int $ttl, + private bool $cacheEnabled ) {} public function call(string $api, string $method, array $arguments = []): mixed { + if (!$this->cacheEnabled) { + $client = $this->getClient()->getApi($api); + + return \call_user_func_array( + [$client, $method], + $arguments + ); + } + $cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments))); return $this->cache->get($cacheKey, function (ItemInterface $item) use ($api, $method, $arguments): array { @@ -33,7 +42,10 @@ final class RedmineClient implements Client, HttpClient $item->tag([$api, $method]); $client = $this->getClient()->getApi($api); - $value = \call_user_func_array([$client, $method], $arguments); + $value = \call_user_func_array( + [$client, $method], + $arguments + ); return $value; }); diff --git a/src/Client/RedmineClientInterface.php b/src/Client/RedmineClientInterface.php new file mode 100644 index 0000000..0aa7445 --- /dev/null +++ b/src/Client/RedmineClientInterface.php @@ -0,0 +1,11 @@ +processConfiguration(new Configuration(), $configs); + $container->setParameter('magdev_redmine.cache.enabled', $config['cache']['enabled']); $container->setParameter('magdev_redmine.cache.ttl', $config['cache']['ttl']); $container->setParameter('magdev_redmine.url', $config['url']); $container->setParameter('magdev_redmine.apiKey', $config['apiKey']); diff --git a/src/Enum/ApiName.php b/src/Enum/ApiName.php deleted file mode 100644 index e25e967..0000000 --- a/src/Enum/ApiName.php +++ /dev/null @@ -1,27 +0,0 @@ -