WordPress\AiClient\Providers

ProviderRegistry::setRequestAuthenticationForProviderprivateWP 0.1.0

Sets the request authentication for a specific provider, hooking up its class instances.

Method of the class: ProviderRegistry{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->setRequestAuthenticationForProvider( $className, $requestAuthentication ): void;
$className(class-string) (required)
The provider class name.
$requestAuthentication(RequestAuthenticationInterface) (required)
The authentication instance.

Changelog

Since 0.1.0 Introduced.

ProviderRegistry::setRequestAuthenticationForProvider() code WP 7.0

private function setRequestAuthenticationForProvider(string $className, RequestAuthenticationInterface $requestAuthentication): void
{
    $authenticationMethod = $className::metadata()->getAuthenticationMethod();
    if ($authenticationMethod === null) {
        throw new InvalidArgumentException(sprintf('Provider %s does not expect any authentication, but got %s.', $className, get_class($requestAuthentication)));
    }
    $expectedClass = $authenticationMethod->getImplementationClass();
    if (!$requestAuthentication instanceof $expectedClass) {
        throw new InvalidArgumentException(sprintf('Provider %s expects authentication of type %s, but got %s.', $className, $expectedClass, get_class($requestAuthentication)));
    }
    $availability = $className::availability();
    if ($availability instanceof WithRequestAuthenticationInterface) {
        $availability->setRequestAuthentication($requestAuthentication);
    }
    $modelMetadataDirectory = $className::modelMetadataDirectory();
    if ($modelMetadataDirectory instanceof WithRequestAuthenticationInterface) {
        $modelMetadataDirectory->setRequestAuthentication($requestAuthentication);
    }
    if (is_subclass_of($className, ProviderWithOperationsHandlerInterface::class)) {
        $operationsHandler = $className::operationsHandler();
        if ($operationsHandler instanceof WithRequestAuthenticationInterface) {
            $operationsHandler->setRequestAuthentication($requestAuthentication);
        }
    }
}