-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathClientManager.php
More file actions
106 lines (90 loc) · 2.02 KB
/
ClientManager.php
File metadata and controls
106 lines (90 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace ArkEcosystem\Client;
use InvalidArgumentException;
/**
* This is the client manager class.
*/
class ClientManager
{
/**
* The default client instance.
*
* @var string
*/
private $default = 'main';
/**
* The active client instances.
*
* @var array
*/
private $clients = [];
/**
* Connect to the given client.
*
* @param string $host
* @param string $name
*
* @return Connection
*/
public function connect(string $host, string $name = 'main'): Connection
{
if (isset($this->clients[$name])) {
throw new InvalidArgumentException("Client [$name] is already configured.");
}
$this->clients[$name] = new Connection($host);
return $this->clients[$name];
}
/**
* Disconnect from the given client.
*
* @param string|null $name
*/
public function disconnect(?string $name = null): void
{
$name = $name ?? $this->getDefaultClient();
unset($this->clients[$name]);
}
/**
* Get a client instance.
*
* @param string|null $name
*
* @return Connection
*/
public function client(?string $name = null): Connection
{
$name = $name ?? $this->getDefaultClient();
if (! isset($this->clients[$name])) {
throw new InvalidArgumentException("Client [$name] not configured.");
}
return $this->clients[$name];
}
/**
* Get the default client name.
*
* @return string
*/
public function getDefaultClient(): string
{
return $this->default;
}
/**
* Set the default client name.
*
* @param string $name
*/
public function setDefaultClient(string $name): void
{
$this->default = $name;
}
/**
* Return all of the created clients.
*
* @return array[]
*/
public function getClients(): array
{
return $this->clients;
}
}