ClientBuilder::setHttpClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\Client;
6
7
use Facile\JoseVerifier\JWK\JwksProviderInterface;
8
use Facile\JoseVerifier\JWK\MemoryJwksProvider;
9
use Facile\OpenIDClient\AuthMethod\AuthMethodFactory;
10
use Facile\OpenIDClient\AuthMethod\AuthMethodFactoryInterface;
11
use Facile\OpenIDClient\AuthMethod\ClientSecretBasic;
12
use Facile\OpenIDClient\AuthMethod\ClientSecretJwt;
13
use Facile\OpenIDClient\AuthMethod\ClientSecretPost;
14
use Facile\OpenIDClient\AuthMethod\None;
15
use Facile\OpenIDClient\AuthMethod\PrivateKeyJwt;
16
use Facile\OpenIDClient\AuthMethod\SelfSignedTLSClientAuth;
17
use Facile\OpenIDClient\AuthMethod\TLSClientAuth;
18
use Facile\OpenIDClient\Client\Metadata\ClientMetadataInterface;
19
use Facile\OpenIDClient\Exception\InvalidArgumentException;
20
use Facile\OpenIDClient\Issuer\IssuerInterface;
21
use Http\Discovery\Psr18ClientDiscovery;
22
use Psr\Http\Client\ClientInterface as HttpClient;
23
24
class ClientBuilder
25
{
26
    /** @var ClientMetadataInterface|null */
27
    private $clientMetadata;
28
29
    /** @var IssuerInterface|null */
30
    private $issuer;
31
32
    /** @var JwksProviderInterface|null */
33
    private $jwksProvider;
34
35
    /** @var AuthMethodFactoryInterface|null */
36
    private $authMethodFactory;
37
38
    /** @var HttpClient|null */
39
    private $httpClient;
40
41
    public function setClientMetadata(?ClientMetadataInterface $clientMetadata): self
42
    {
43
        $this->clientMetadata = $clientMetadata;
44
45
        return $this;
46
    }
47
48
    public function setIssuer(?IssuerInterface $issuer): self
49
    {
50
        $this->issuer = $issuer;
51
52
        return $this;
53
    }
54
55
    public function setJwksProvider(?JwksProviderInterface $jwksProvider): self
56
    {
57
        $this->jwksProvider = $jwksProvider;
58
59
        return $this;
60
    }
61
62
    public function setAuthMethodFactory(?AuthMethodFactoryInterface $authMethodFactory): self
63
    {
64
        $this->authMethodFactory = $authMethodFactory;
65
66
        return $this;
67
    }
68
69
    public function setHttpClient(?HttpClient $httpClient): self
70
    {
71
        $this->httpClient = $httpClient;
72
73
        return $this;
74
    }
75
76
    private function buildJwksProvider(): JwksProviderInterface
77
    {
78
        if (null !== $this->jwksProvider) {
79
            return $this->jwksProvider;
80
        }
81
82
        if (null === $this->clientMetadata) {
83
            return new MemoryJwksProvider(['keys' => []]);
84
        }
85
86
        $jwks = $this->clientMetadata->getJwks();
87
88
        if (null !== $jwks) {
89
            new MemoryJwksProvider($jwks);
90
        }
91
92
        return new MemoryJwksProvider(['keys' => []]);
93
    }
94
95
    private function buildAuthMethodFactory(): AuthMethodFactoryInterface
96
    {
97
        return $this->authMethodFactory ?? new AuthMethodFactory([
98
            new ClientSecretBasic(),
99
            new ClientSecretJwt(),
100
            new ClientSecretPost(),
101
            new None(),
102
            new PrivateKeyJwt(),
103
            new TLSClientAuth(),
104
            new SelfSignedTLSClientAuth(),
105
        ]);
106
    }
107
108
    private function buildHttpClient(): HttpClient
109
    {
110
        return $this->httpClient ?? Psr18ClientDiscovery::find();
111
    }
112
113
    public function build(): ClientInterface
114
    {
115
        if (null === $this->issuer) {
116
            throw new InvalidArgumentException('Issuer must be provided');
117
        }
118
119
        if (null === $this->clientMetadata) {
120
            throw new InvalidArgumentException('Client metadata must be provided');
121
        }
122
123
        return new Client(
124
            $this->issuer,
125
            $this->clientMetadata,
126
            $this->buildJwksProvider(),
127
            $this->buildAuthMethodFactory(),
128
            $this->buildHttpClient()
129
        );
130
    }
131
}
132