Passed
Push — master ( 13425d...f357db )
by Alexander
02:26
created

Collection::getClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
10
/**
11
 * Collection is a storage for all auth clients in the application.
12
 *
13
 * Example application configuration:
14
 *
15
 * ```php
16
 * 'authClients' => [
17
 *     'google' => [
18
 *         '__class' => Yiisoft\Yii\AuthClient\Clients\Google::class,
19
 *         'setClientId()' => ['google_client_id'],
20
 *         'setClientSecret()' => ['google_client_secret'],
21
 *      ],
22
 *     'facebook' => [
23
 *         '__class' => Yiisoft\Yii\AuthClient\Clients\Facebook::class,
24
 *         'setClientId()' => ['facebook_client_id'],
25
 *         'setClientSecret()' => ['facebook_client_secret'],
26
 *     ]
27
 *     ...
28
 * ]
29
 * ```
30
 */
31
final class Collection
32
{
33
    /**
34
     * @var AuthClientInterface[]|array list of Auth clients with their configuration in format: 'clientName' => [...]
35
     */
36
    private array $clients;
37
38 3
    public function __construct(array $clients)
39
    {
40 3
        $this->clients = $clients;
41 3
    }
42
43
    /**
44
     * @return AuthClientInterface[] list of auth clients.
45
     */
46 1
    public function getClients(): array
47
    {
48 1
        $clients = [];
49 1
        foreach ($this->clients as $name => $client) {
50 1
            $clients[$name] = $this->getClient($name);
51
        }
52
53 1
        return $clients;
54
    }
55
56
    /**
57
     * @param array $clients list of auth clients indexed by their names
58
     */
59 1
    public function setClients(array $clients): void
60
    {
61 1
        $this->clients = $clients;
62 1
    }
63
64
    /**
65
     * @param string $name client name
66
     *
67
     * @throws InvalidArgumentException on non existing client request.
68
     * @return AuthClientInterface auth client instance.
69
     */
70 2
    public function getClient(string $name): AuthClientInterface
71
    {
72 2
        if (!$this->hasClient($name)) {
73
            throw new InvalidArgumentException("Unknown auth client '{$name}'.");
74
        }
75
76 2
        $client = $this->clients[$name];
77 2
        if (!($client instanceof AuthClientInterface)) {
78
            throw new RuntimeException(
79
                'Client should be ClientInterface instance. "' . get_class($client) . '" given.'
80
            );
81
        }
82 2
        return $client;
83
    }
84
85
    /**
86
     * Checks if client exists in the hub.
87
     *
88
     * @param string $name client id.
89
     *
90
     * @return bool whether client exist.
91
     */
92 3
    public function hasClient(string $name): bool
93
    {
94 3
        return array_key_exists($name, $this->clients);
95
    }
96
}
97