1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vivait\TenantBundle\Registry; |
4
|
|
|
|
5
|
|
|
use Vivait\TenantBundle\Model; |
6
|
|
|
use Vivait\TenantBundle\Model\Tenant; |
7
|
|
|
|
8
|
|
|
class TenantRegistry { |
9
|
|
|
/** |
10
|
|
|
* @var Tenant[] |
11
|
|
|
*/ |
12
|
|
|
private $tenants = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Tenant |
16
|
|
|
*/ |
17
|
|
|
private $current; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Tenant[] $tenants |
21
|
|
|
*/ |
22
|
|
|
public function __construct( array $tenants = array() ) { |
23
|
|
|
$this->addAll($tenants); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Tenant|string $tenant |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function contains($tenant) { |
31
|
|
|
if ($tenant instanceOf Tenant) { |
32
|
|
|
return (isset($this->tenants[$tenant->getKey()])); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return (isset($this->tenants[$tenant])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Tenant[] $tenants |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function addAll(array $tenants) { |
43
|
|
|
foreach ($tenants as $tenant) { |
44
|
|
|
if (!($tenant instanceOf Tenant)) { |
45
|
|
|
throw new \InvalidArgumentException('Invalid Tenant added to registry'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->tenants[$tenant->getKey()] = $tenant; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Tenant $tenant |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function add(Tenant $tenant) { |
59
|
|
|
if (!$this->contains($tenant)) { |
60
|
|
|
$this->tenants[$tenant->getKey()] = $tenant; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get a tenant based on their key |
68
|
|
|
* @param string $key The tenant key |
69
|
|
|
* @return Tenant |
70
|
|
|
*/ |
71
|
|
|
public function get($key) { |
72
|
|
|
if (!$this->contains($key)) { |
73
|
|
|
throw new \OutOfBoundsException(sprintf('No tenant found in registry for key "%s"', $key)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->tenants[$key]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets all tenants |
81
|
|
|
* @return Model\Tenant[] |
82
|
|
|
*/ |
83
|
|
|
public function getAll() { |
84
|
|
|
return $this->tenants; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets current |
89
|
|
|
* @return Tenant |
90
|
|
|
*/ |
91
|
|
|
public function getCurrent() |
92
|
|
|
{ |
93
|
|
|
return $this->current; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets current |
98
|
|
|
* @param Tenant|string $current The current tenant or their key |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setCurrent( $current ) |
102
|
|
|
{ |
103
|
|
|
if (!($current instanceOf Tenant)) { |
104
|
|
|
$current = $this->get( $current ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->current = $current; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|