1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Persister\PersisterException; |
6
|
|
|
use As3\Modlr\Persister\PersisterInterface; |
7
|
|
|
use As3\Modlr\Search\ClientException; |
8
|
|
|
use As3\Modlr\Search\ClientInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Registers all available storage layers services (search and persistence) by key. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class StorageLayerManager |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* All registered Persister services. |
19
|
|
|
* |
20
|
|
|
* @var PersisterInterface[] |
21
|
|
|
*/ |
22
|
|
|
private $persisters = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* All Search Client services. |
26
|
|
|
* |
27
|
|
|
* @var ClientInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $searchClients = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Adds/registers a Persister service to the manager. |
33
|
|
|
* |
34
|
|
|
* @param PersisterInterface $persister |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function addPersister(PersisterInterface $persister) |
38
|
|
|
{ |
39
|
|
|
$this->persisters[$persister->getPersisterKey()] = $persister; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds/registers a Search Client service to the manager. |
45
|
|
|
* |
46
|
|
|
* @param ClientInterface $client |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function addSearchClient(ClientInterface $client) |
50
|
|
|
{ |
51
|
|
|
$this->searchClients[$client->getClientKey()] = $client; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets a Persister service by key. |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @return PersisterInterface |
60
|
|
|
* @throws PersisterException If the service was not found. |
61
|
|
|
*/ |
62
|
|
|
public function getPersister($key) |
63
|
|
|
{ |
64
|
|
|
if (empty($key) || false === $this->hasPersister($key)) { |
65
|
|
|
throw PersisterException::persisterNotFound($key); |
66
|
|
|
} |
67
|
|
|
return $this->persisters[$key]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determines if a Persister exists. |
72
|
|
|
* |
73
|
|
|
* @param string $key |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function hasPersister($key) |
77
|
|
|
{ |
78
|
|
|
return isset($this->persisters[$key]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets a Search Client service by key. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @return ClientInterface |
86
|
|
|
* @throws ClientException If the service was not found. |
87
|
|
|
*/ |
88
|
|
|
public function getSearchClient($key) |
89
|
|
|
{ |
90
|
|
|
if (empty($key) || false === $this->hasSearchClient($key)) { |
91
|
|
|
throw ClientException::clientNotFound($key); |
92
|
|
|
} |
93
|
|
|
return $this->searchClients[$key]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Determines if a Search Client exists. |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function hasSearchClient($key) |
103
|
|
|
{ |
104
|
|
|
return isset($this->searchClients[$key]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|