Test Failed
Pull Request — master (#21)
by Rustam
03:07
created

Collection::getClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 4
cts 6
cp 0.6667
crap 3.3332
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
    public function __construct(array $clients)
39
    {
40 3
        $this->clients = $clients;
41
    }
42 3
43 3
    /**
44 3
     * @return AuthClientInterface[] list of auth clients.
45
     */
46
    public function getClients(): array
47
    {
48
        $clients = [];
49 1
        foreach ($this->clients as $name => $client) {
50
            $clients[$name] = $this->getClient($name);
51 1
        }
52 1
53 1
        return $clients;
54
    }
55
56 1
    /**
57
     * @param array $clients list of auth clients indexed by their names
58
     */
59
    public function setClients(array $clients): void
60
    {
61
        $this->clients = $clients;
62 1
    }
63
64 1
    /**
65 1
     * @param string $name client name
66
     *
67
     * @throws InvalidArgumentException on non existing client request.
68
     * @return AuthClientInterface auth client instance.
69
     */
70
    public function getClient(string $name): AuthClientInterface
71
    {
72
        if (!$this->hasClient($name)) {
73
            throw new InvalidArgumentException("Unknown auth client '{$name}'.");
74 2
        }
75
76 2
        $client = $this->clients[$name];
77
        if (!($client instanceof AuthClientInterface)) {
78
            throw new RuntimeException(
79
                'Client should be ClientInterface instance. "' . get_class($client) . '" given.'
80 2
            );
81 2
        }
82
        return $client;
83 2
    }
84 2
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
    public function hasClient(string $name): bool
93
    {
94
        return array_key_exists($name, $this->clients);
95
    }
96
}
97