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