Skip to content

Commit 5fec04c

Browse files
committed
fix default cache
1 parent 905916a commit 5fec04c

File tree

3 files changed

+68
-34
lines changed

3 files changed

+68
-34
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Commercetools\Client;
4+
5+
use Psr\Cache\CacheItemPoolInterface;
6+
use Psr\SimpleCache\CacheInterface;
7+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
8+
9+
final class CacheValidator
10+
{
11+
/**
12+
* @psalm-param CacheItemPoolInterface|CacheInterface|null $cache
13+
* @psalm-return CacheItemPoolInterface|CacheInterface
14+
*/
15+
public static function validateCache($cache = null)
16+
{
17+
if ($cache instanceof CacheItemPoolInterface || $cache instanceof CacheInterface) {
18+
return $cache;
19+
}
20+
21+
if (class_exists('Symfony\Component\Cache\Simple\FilesystemCache')) {
22+
/** @psalm-suppress all */
23+
/** @var CacheItemPoolInterface $cache */
24+
$cache = new FilesystemCache('', 0, getcwd() . "/cache");
25+
} else {
26+
/** @psalm-suppress all */
27+
/** @var CacheItemPoolInterface $cache */
28+
$cache = new FilesystemAdapter('', 0, getcwd() . "/cache");
29+
}
30+
31+
return $cache;
32+
}
33+
}

lib/commercetools-base/src/Client/OAuthHandlerFactory.php

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,34 @@
99

1010
namespace Commercetools\Client;
1111

12-
use Cache\Adapter\Filesystem\FilesystemCachePool;
1312
use Commercetools\Exception\InvalidArgumentException;
1413
use GuzzleHttp\Client;
15-
use League\Flysystem\Adapter\Local;
16-
use League\Flysystem\Filesystem;
1714
use Psr\Cache\CacheItemPoolInterface;
1815
use Psr\SimpleCache\CacheInterface;
16+
use Symfony\Component\Cache\Simple\FilesystemCache;
1917

2018
class OAuthHandlerFactory
2119
{
22-
/**
23-
* @psalm-param CacheItemPoolInterface|CacheInterface|null $cache
24-
* @psalm-return CacheItemPoolInterface|CacheInterface
25-
*/
26-
private static function validateCache($cache = null)
27-
{
28-
if ($cache instanceof CacheItemPoolInterface || $cache instanceof CacheInterface) {
29-
return $cache;
30-
}
31-
32-
$filesystemAdapter = new Local(getcwd());
33-
$filesystem = new Filesystem($filesystemAdapter);
34-
$cache = new FilesystemCachePool($filesystem);
35-
36-
return $cache;
37-
}
38-
3920
/**
4021
* @psalm-param CacheItemPoolInterface|CacheInterface|null $cache
4122
*/
4223
public static function ofAuthConfig(AuthConfig $authConfig, $cache = null): OAuth2Handler
4324
{
44-
$cache = self::validateCache($cache);
25+
$cache = CacheValidator::validateCache($cache);
4526
switch (true) {
46-
case $authConfig instanceof ClientCredentialsConfig:
47-
$provider = new CachedTokenProvider(
48-
new ClientCredentialTokenProvider(
49-
new Client($authConfig->getOptions()),
50-
$authConfig->getAuthUri(),
51-
$authConfig->getCredentials()
52-
),
53-
$cache,
54-
$authConfig->getCredentials()->getCacheKey()
55-
);
56-
break;
57-
default:
58-
throw new InvalidArgumentException('Unknown authorization configuration');
27+
case $authConfig instanceof ClientCredentialsConfig:
28+
$provider = new CachedTokenProvider(
29+
new ClientCredentialTokenProvider(
30+
new Client($authConfig->getOptions()),
31+
$authConfig->getAuthUri(),
32+
$authConfig->getCredentials()
33+
),
34+
$cache,
35+
$authConfig->getCredentials()->getCacheKey()
36+
);
37+
break;
38+
default:
39+
throw new InvalidArgumentException('Unknown authorization configuration');
5940

6041
}
6142
return self::ofProvider($provider);

test/unit/CacheValidatorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Commercetools\UnitTest;
4+
5+
use Commercetools\Client\CacheValidator;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
8+
use Symfony\Component\Cache\Simple\FilesystemCache;
9+
10+
class CacheValidatorTest extends TestCase
11+
{
12+
public function testDefaultCache()
13+
{
14+
if (class_exists('Symfony\Component\Cache\Simple\FilesystemCache')) {
15+
$this->assertInstanceOf(FilesystemCache::class, CacheValidator::validateCache());
16+
} else {
17+
$this->assertInstanceOf(FilesystemAdapter::class, CacheValidator::validateCache());
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)