-
Notifications
You must be signed in to change notification settings - Fork 8
/
OAuthHandlerFactory.php
68 lines (58 loc) · 1.98 KB
/
OAuthHandlerFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
/**
* This file has been auto generated
* Do not change it.
*/
namespace Commercetools\Client;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use Commercetools\Exception\InvalidArgumentException;
use GuzzleHttp\Client;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
class OAuthHandlerFactory
{
/**
* @psalm-param CacheItemPoolInterface|CacheInterface|null $cache
* @psalm-return CacheItemPoolInterface|CacheInterface
*/
private static function validateCache($cache = null)
{
if ($cache instanceof CacheItemPoolInterface || $cache instanceof CacheInterface) {
return $cache;
}
$filesystemAdapter = new Local(getcwd());
$filesystem = new Filesystem($filesystemAdapter);
$cache = new FilesystemCachePool($filesystem);
return $cache;
}
/**
* @psalm-param CacheItemPoolInterface|CacheInterface|null $cache
*/
public static function ofAuthConfig(AuthConfig $authConfig, $cache = null): OAuth2Handler
{
$cache = self::validateCache($cache);
switch (true) {
case $authConfig instanceof ClientCredentialsConfig:
$provider = new CachedTokenProvider(
new ClientCredentialTokenProvider(
new Client($authConfig->getOptions()),
$authConfig->getAuthUri(),
$authConfig->getCredentials()
),
$cache,
$authConfig->getCredentials()->getCacheKey()
);
break;
default:
throw new InvalidArgumentException('Unknown authorization configuration');
}
return self::ofProvider($provider);
}
public static function ofProvider(TokenProvider $provider): OAuth2Handler
{
return new OAuth2Handler($provider);
}
}