clientMetadata = $clientMetadata; return $this; } public function setIssuer(?IssuerInterface $issuer): self { $this->issuer = $issuer; return $this; } public function setJwksProvider(?JwksProviderInterface $jwksProvider): self { $this->jwksProvider = $jwksProvider; return $this; } public function setAuthMethodFactory(?AuthMethodFactoryInterface $authMethodFactory): self { $this->authMethodFactory = $authMethodFactory; return $this; } public function setHttpClient(?HttpClient $httpClient): self { $this->httpClient = $httpClient; return $this; } private function buildJwksProvider(): JwksProviderInterface { if (null !== $this->jwksProvider) { return $this->jwksProvider; } if (null === $this->clientMetadata) { return new MemoryJwksProvider(['keys' => []]); } $jwks = $this->clientMetadata->getJwks(); if (null !== $jwks) { return new MemoryJwksProvider($jwks); } return new MemoryJwksProvider(['keys' => []]); } private function buildAuthMethodFactory(): AuthMethodFactoryInterface { return $this->authMethodFactory ?? new AuthMethodFactory([ new ClientSecretBasic(), new ClientSecretJwt(), new ClientSecretPost(), new None(), new PrivateKeyJwt(), new TLSClientAuth(), new SelfSignedTLSClientAuth(), ]); } private function buildHttpClient(): HttpClient { return $this->httpClient ?? Psr18ClientDiscovery::find(); } public function build(): ClientInterface { if (null === $this->issuer) { throw new InvalidArgumentException('Issuer must be provided'); } if (null === $this->clientMetadata) { throw new InvalidArgumentException('Client metadata must be provided'); } return new Client( $this->issuer, $this->clientMetadata, $this->buildJwksProvider(), $this->buildAuthMethodFactory(), $this->buildHttpClient() ); } }