| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Issuer\Metadata\Provider; |
| 6 |
|
| 7 |
use Http\Discovery\Psr17FactoryDiscovery; |
| 8 |
use Http\Discovery\Psr18ClientDiscovery; |
| 9 |
use Psr\Http\Client\ClientInterface; |
| 10 |
use Psr\Http\Message\RequestFactoryInterface; |
| 11 |
use Psr\Http\Message\UriFactoryInterface; |
| 12 |
use Psr\SimpleCache\CacheInterface; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class MetadataProviderBuilder |
| 18 |
{ |
| 19 |
private ?DiscoveryProviderInterface $discoveryProvider = null; |
| 20 |
|
| 21 |
private ?WebFingerProviderInterface $webFingerProvider = null; |
| 22 |
|
| 23 |
private ?ClientInterface $client = null; |
| 24 |
|
| 25 |
private ?RequestFactoryInterface $requestFactory = null; |
| 26 |
|
| 27 |
private ?UriFactoryInterface $uriFactory = null; |
| 28 |
|
| 29 |
private ?CacheInterface $cache = null; |
| 30 |
|
| 31 |
private ?int $cacheTtl = null; |
| 32 |
|
| 33 |
public function setDiscoveryProvider(DiscoveryProviderInterface $discoveryProvider): self |
| 34 |
{ |
| 35 |
$this->discoveryProvider = $discoveryProvider; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function setHttpClient(?ClientInterface $client): self |
| 41 |
{ |
| 42 |
$this->client = $client; |
| 43 |
|
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
public function setWebFingerProvider(WebFingerProviderInterface $webFingerProvider): self |
| 48 |
{ |
| 49 |
$this->webFingerProvider = $webFingerProvider; |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function setClient(?ClientInterface $client): self |
| 58 |
{ |
| 59 |
$this->client = $client; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function setRequestFactory(?RequestFactoryInterface $requestFactory): self |
| 65 |
{ |
| 66 |
$this->requestFactory = $requestFactory; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function setUriFactory(?UriFactoryInterface $uriFactory): self |
| 72 |
{ |
| 73 |
$this->uriFactory = $uriFactory; |
| 74 |
|
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
public function setCache(?CacheInterface $cache): self |
| 79 |
{ |
| 80 |
$this->cache = $cache; |
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
public function setCacheTtl(?int $cacheTtl): self |
| 86 |
{ |
| 87 |
$this->cacheTtl = $cacheTtl; |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
private function buildClient(): ClientInterface |
| 93 |
{ |
| 94 |
return $this->client ?? Psr18ClientDiscovery::find(); |
| 95 |
} |
| 96 |
|
| 97 |
public function buildRequestFactory(): RequestFactoryInterface |
| 98 |
{ |
| 99 |
return $this->requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
| 100 |
} |
| 101 |
|
| 102 |
public function buildUriFactory(): UriFactoryInterface |
| 103 |
{ |
| 104 |
return $this->uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); |
| 105 |
} |
| 106 |
|
| 107 |
private function buildDiscoveryProvider(): DiscoveryProviderInterface |
| 108 |
{ |
| 109 |
return $this->discoveryProvider ?? new DiscoveryProvider( |
| 110 |
$this->buildClient(), |
| 111 |
$this->buildRequestFactory(), |
| 112 |
$this->buildUriFactory() |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
public function buildWebFingerProvider(): WebFingerProviderInterface |
| 117 |
{ |
| 118 |
return $this->webFingerProvider ?? new WebFingerProvider( |
| 119 |
$this->buildClient(), |
| 120 |
$this->buildRequestFactory(), |
| 121 |
$this->buildUriFactory(), |
| 122 |
$this->buildDiscoveryProvider() |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
public function build(): RemoteProviderInterface |
| 127 |
{ |
| 128 |
$provider = new RemoteProvider([ |
| 129 |
$this->buildDiscoveryProvider(), |
| 130 |
$this->buildWebFingerProvider(), |
| 131 |
]); |
| 132 |
|
| 133 |
if (null !== $this->cache) { |
| 134 |
$provider = new CachedProviderDecorator($provider, $this->cache, $this->cacheTtl); |
| 135 |
} |
| 136 |
|
| 137 |
return $provider; |
| 138 |
} |
| 139 |
} |