0

Final Class Yiisoft\Yii\AuthClient\Collection

InheritanceYiisoft\Yii\AuthClient\Collection

Collection is a storage for all auth clients in the application.

Example application configuration:

'yiisoft/yii-auth-client' => [
    'clients' => [
        'google' => [
            'class' => Yiisoft\Yii\AuthClient\Client\Google::class,
            'clientId' => $_ENV['GOOGLE_CLIENT_ID'],
            'clientSecret' => $_ENV['GOOGLE_CLIENT_SECRET'],
            'oauth2ReturnUrl' => 'https://example.com/auth/google',
        ],
        'facebook' => [
            'class' => Yiisoft\Yii\AuthClient\Client\Facebook::class,
            'clientId' => $_ENV['FACEBOOK_CLIENT_ID'],
            'clientSecret' => $_ENV['FACEBOOK_CLIENT_SECRET'],
        ],
    ],
]

The array key ('google', 'facebook') is used as the client's name. Remaining keys map to setters on the client class, e.g. clientIdsetClientId().

See also \Yiisoft\Yii\AuthClient\CollectionFactory.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( array $clients )
$clients array

                public function __construct(
    /**
     * @var array|OAuth2Interface[] list of OAuth2 clients with their configuration in format: 'clientName' => [...]
     */
    private array $clients,
) {}

            
getClient() public method

public Yiisoft\Yii\AuthClient\OAuth2Interface getClient ( string $name )
$name string

                public function getClient(string $name): OAuth2Interface
{
    if (!$this->hasClient($name)) {
        throw new InvalidArgumentException("Unknown auth client '{$name}'.");
    }
    $client = $this->clients[$name];
    if (!($client instanceof OAuth2Interface)) {
        throw new RuntimeException(
            'The Client should be an OAuth2 Interface.',
        );
    }
    return $client;
}

            
getClients() public method

public array getClients ( )

                public function getClients(): array
{
    $clients = [];
    /**
     * @var OAuth2Interface $client
     * @var string $name
     * @var array $this->clients
     */
    foreach ($this->clients as $name => $client) {
        $clients[$name] = $this->getClient($name);
    }
    return $clients;
}

            
hasClient() public method

Checks if client exists in the hub.

public boolean hasClient ( string $name )
$name string

Client id.

return boolean

Whether client exist.

                public function hasClient(string $name): bool
{
    return array_key_exists($name, $this->clients);
}

            
setClients() public method

public void setClients ( array $clients )
$clients array

List of auth clients indexed by their names

                public function setClients(array $clients): void
{
    $this->clients = $clients;
}