1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman; |
4
|
|
|
|
5
|
|
|
use League\Container\Exception\NotFoundException; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Storeman\Exception\ConfigurationException; |
8
|
|
|
|
9
|
|
|
final class VaultContainer implements ContainerInterface, \Countable, \IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Vault[] |
13
|
|
|
*/ |
14
|
|
|
protected $vaults = []; |
15
|
|
|
|
16
|
|
|
public function __construct(Configuration $configuration, Storeman $storeman) |
17
|
|
|
{ |
18
|
|
|
foreach ($configuration->getVaults() as $vaultConfiguration) |
19
|
|
|
{ |
20
|
|
|
if (array_key_exists($vaultConfiguration->getTitle(), $this->vaults)) |
21
|
|
|
{ |
22
|
|
|
throw new ConfigurationException("Duplicate vault title: {$vaultConfiguration->getTitle()}"); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->vaults[$vaultConfiguration->getTitle()] = new Vault($storeman, $vaultConfiguration); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns a vault by its title. |
31
|
|
|
* |
32
|
|
|
* @param string $title |
33
|
|
|
* @return Vault |
34
|
|
|
*/ |
35
|
|
|
public function getVaultByTitle(string $title): ?Vault |
36
|
|
|
{ |
37
|
|
|
return array_key_exists($title, $this->vaults) ? $this->vaults[$title] : null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a subset of the configured vaults identified by the given set of titles. |
42
|
|
|
* |
43
|
|
|
* @param array $titles |
44
|
|
|
* @return Vault[] |
45
|
|
|
*/ |
46
|
|
|
public function getVaultsByTitles(array $titles): array |
47
|
|
|
{ |
48
|
|
|
return array_filter($this->vaults, function(Vault $vault) use ($titles) { |
49
|
|
|
|
50
|
|
|
return in_array($vault->getVaultConfiguration()->getTitle(), $titles); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns all those vaults that have a given revision. |
56
|
|
|
* |
57
|
|
|
* @param int $revision |
58
|
|
|
* @return Vault[] |
59
|
|
|
*/ |
60
|
|
|
public function getVaultsHavingRevision(int $revision): array |
61
|
|
|
{ |
62
|
|
|
return array_filter($this->vaults, function(Vault $vault) use ($revision) { |
63
|
|
|
|
64
|
|
|
return $vault->getVaultLayout()->getSynchronizations()->getSynchronization($revision) !== null; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the vault with the highest priority. |
70
|
|
|
* |
71
|
|
|
* @param Vault[] $vaults Vaults to consider. Defaults to all configured vaults. |
72
|
|
|
* @return Vault |
73
|
|
|
*/ |
74
|
|
|
public function getPrioritizedVault(array $vaults = null): ?Vault |
75
|
|
|
{ |
76
|
|
|
/** @var Vault[] $vaults */ |
77
|
|
|
$vaults = ($vaults === null) ? $this : $vaults; |
78
|
|
|
|
79
|
|
|
/** @var Vault $return */ |
80
|
|
|
$return = null; |
81
|
|
|
|
82
|
|
|
foreach ($vaults as $vault) |
83
|
|
|
{ |
84
|
|
|
if ($return === null || $return->getVaultConfiguration()->getPriority() < $vault->getVaultConfiguration()->getPriority()) |
85
|
|
|
{ |
86
|
|
|
$return = $vault; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function has($id) |
97
|
|
|
{ |
98
|
|
|
return $this->getVaultByTitle($id) !== null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function get($id) |
105
|
|
|
{ |
106
|
|
|
if ($vault = $this->getVaultByTitle($id)) |
107
|
|
|
{ |
108
|
|
|
return $vault; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new NotFoundException(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function count() |
118
|
|
|
{ |
119
|
|
|
return count($this->vaults); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getIterator() |
126
|
|
|
{ |
127
|
|
|
return new \ArrayIterator(array_values($this->vaults)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|