-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientRegistry.php
79 lines (67 loc) · 1.62 KB
/
ClientRegistry.php
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
<?php
namespace Nelmio\SolariumBundle;
use Solarium\Client;
/**
* Class ClientRegistry
*
* Service to access all the clients configured by the bundle
*
* @package Nelmio\SolariumBundle
*/
class ClientRegistry
{
/** @var string */
protected $defaultClientName;
/** @var array */
protected $clients;
public function __construct(array $clients, $defaultClientName)
{
$this->defaultClientName = $defaultClientName;
$this->clients = $clients;
}
/**
* Gets the default client name.
*
* @return string The default client name.
*/
public function getDefaultClientName()
{
return $this->defaultClientName;
}
/**
* Gets the named client.
*
* @param string $name The client name (null for the default one).
*
* @throws \InvalidArgumentException
* @return Client
*/
public function getClient($name = null)
{
if (null === $name) {
$name = $this->defaultClientName;
}
if (in_array($name, $this->getClientNames())) {
return $this->clients[$name];
}
throw new \InvalidArgumentException(($name === null ? 'default client' : 'client ' . $name) . ' not configured');
}
/**
* Gets an array of all registered clients.
*
* @return array An array of Client instances.
*/
public function getClients()
{
return $this->clients;
}
/**
* Gets all client names.
*
* @return array An array of client names.
*/
public function getClientNames()
{
return array_keys($this->clients);
}
}