1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Script\Security\Exception\ErrorException; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
8
|
|
|
|
9
|
|
|
class ClientRegistry |
10
|
|
|
{ |
11
|
|
|
use ContainerAwareTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $serviceMap; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ClientRegistry constructor. |
20
|
|
|
* |
21
|
|
|
* @throws ErrorException |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$arguments = \func_get_args(); |
26
|
|
|
|
27
|
|
|
$used = []; |
28
|
|
|
foreach ($arguments as $argument) { |
29
|
|
|
if (!\is_array($argument)) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
$checkExists = \array_intersect(array_keys($used), array_keys($argument)); |
33
|
|
|
$count = \count($checkExists); |
34
|
|
|
if ($count !== 0) { |
35
|
|
|
throw new ErrorException(sprintf('Multiple clients with same key is not allowed! Key'.($count > 1 ? 's' : '').' "%s" appear in configuration more than once!', implode(',', $checkExists))); |
36
|
|
|
} |
37
|
|
|
$used = array_merge($used, $argument); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->serviceMap = $used; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $key |
45
|
|
|
* |
46
|
|
|
* @throws ErrorException |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function getClient($key) |
51
|
|
|
{ |
52
|
|
|
if (!$this->hasClient($key)) { |
53
|
|
|
throw new ErrorException(sprintf('Client "%s" not found in registry', $key)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->container->get($this->serviceMap[$key]['key']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function hasClient($key) |
60
|
|
|
{ |
61
|
|
|
return isset($this->serviceMap[$key]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getNameByClient($key = '') |
65
|
|
|
{ |
66
|
|
|
if ($key !== '' && $this->hasClient($key)) { |
67
|
|
|
return $this->serviceMap[$key]['name']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $key; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getClients() |
74
|
|
|
{ |
75
|
|
|
return $this->serviceMap; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|