1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DigitalCz\OpenIDConnect\Discovery; |
6
|
|
|
|
7
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
8
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
9
|
|
|
use LogicException; |
10
|
|
|
use Psr\Http\Client\ClientInterface; |
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
12
|
|
|
use Psr\SimpleCache\CacheInterface; |
13
|
|
|
|
14
|
|
|
final class DiscovererFactory |
15
|
|
|
{ |
16
|
|
|
public static function create( |
17
|
|
|
?ClientInterface $client = null, |
18
|
|
|
?RequestFactoryInterface $requestFactory = null, |
19
|
|
|
?CacheInterface $cache = null, |
20
|
|
|
int $cacheTtl = CachedDiscoverer::DEFAULT_TTL |
21
|
|
|
): Discoverer { |
22
|
|
|
$client ??= self::discoverClient(); |
23
|
|
|
$requestFactory ??= self::discoverRequestFactory(); |
24
|
|
|
|
25
|
|
|
$discoverer = new HttpDiscoverer($client, $requestFactory); |
26
|
|
|
|
27
|
|
|
if ($cache !== null) { |
28
|
|
|
$discoverer = new CachedDiscoverer($discoverer, $cache, $cacheTtl); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $discoverer; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private static function discoverClient(): ClientInterface |
35
|
|
|
{ |
36
|
|
|
if (class_exists(Psr18ClientDiscovery::class)) { |
37
|
|
|
return Psr18ClientDiscovery::find(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
throw new LogicException('Unable to discover ' . ClientInterface::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private static function discoverRequestFactory(): RequestFactoryInterface |
44
|
|
|
{ |
45
|
|
|
if (class_exists(Psr17FactoryDiscovery::class)) { |
46
|
|
|
return Psr17FactoryDiscovery::findRequestFactory(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new LogicException('Unable to discover ' . RequestFactoryInterface::class); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|