1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Transfer\Storage; |
11
|
|
|
|
12
|
|
|
use Transfer\Storage\Exception\BadStorageIterableException; |
13
|
|
|
use Transfer\Storage\Exception\ObjectNotFoundException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Storage chaining utility. |
17
|
|
|
*/ |
18
|
|
|
class ChainStorage implements StorageInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array Storage collection |
22
|
|
|
*/ |
23
|
|
|
private $storageCollection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $storageCollection Storage collection |
27
|
|
|
*/ |
28
|
5 |
|
public function __construct(array $storageCollection) |
29
|
|
|
{ |
30
|
5 |
|
$this->storageCollection = $storageCollection; |
31
|
|
|
|
32
|
5 |
|
foreach ($this->storageCollection as $storage) { |
33
|
5 |
|
if (!$storage instanceof StorageInterface) { |
34
|
1 |
|
throw new \InvalidArgumentException('Storage collection must contain only StorageInterface instances.'); |
35
|
|
|
} |
36
|
5 |
|
} |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
2 |
|
public function add($object, $id = null) |
43
|
|
|
{ |
44
|
|
|
/** @var StorageInterface $storage */ |
45
|
2 |
|
foreach ($this->storageCollection as $storage) { |
46
|
2 |
|
if ($storage->add($object, $id)) { |
47
|
1 |
|
return true; |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
1 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
2 |
|
public function contains($object) |
58
|
|
|
{ |
59
|
|
|
/** @var StorageInterface $storage */ |
60
|
2 |
|
foreach ($this->storageCollection as $storage) { |
61
|
2 |
|
if ($storage->contains($object)) { |
62
|
1 |
|
return true; |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function containsId($id) |
73
|
|
|
{ |
74
|
|
|
/** @var StorageInterface $storage */ |
75
|
2 |
|
foreach ($this->storageCollection as $storage) { |
76
|
2 |
|
if ($storage->containsId($id)) { |
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
2 |
|
public function findById($id) |
88
|
|
|
{ |
89
|
|
|
/** @var StorageInterface $storage */ |
90
|
2 |
|
foreach ($this->storageCollection as $storage) { |
91
|
|
|
try { |
92
|
2 |
|
return $storage->findById($id); |
93
|
1 |
|
} catch (ObjectNotFoundException $e) { |
94
|
|
|
// Go to next storage instance |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
throw new ObjectNotFoundException($id); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
2 |
|
public function remove($object) |
105
|
|
|
{ |
106
|
|
|
/** @var StorageInterface $storage */ |
107
|
2 |
|
foreach ($this->storageCollection as $storage) { |
108
|
2 |
|
if ($storage->remove($object)) { |
109
|
1 |
|
return true; |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
2 |
|
public function removeById($id) |
120
|
|
|
{ |
121
|
|
|
/** @var StorageInterface $storage */ |
122
|
2 |
|
foreach ($this->storageCollection as $storage) { |
123
|
2 |
|
if ($storage->removeById($id)) { |
124
|
1 |
|
return true; |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
|
128
|
1 |
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
2 |
|
public function all() |
135
|
|
|
{ |
136
|
2 |
|
$iterator = new \AppendIterator(); |
137
|
|
|
|
138
|
|
|
/** @var StorageInterface $storage */ |
139
|
2 |
|
foreach ($this->storageCollection as $storage) { |
140
|
2 |
|
if (is_array($storage->all())) { |
141
|
1 |
|
$iterator->append(new \ArrayIterator($storage->all())); |
142
|
2 |
|
} elseif ($storage->all() instanceof \Iterator) { |
143
|
1 |
|
$iterator->append($storage->all()); |
144
|
1 |
|
} else { |
145
|
1 |
|
throw new BadStorageIterableException($storage); |
146
|
|
|
} |
147
|
1 |
|
} |
148
|
|
|
|
149
|
1 |
|
return $iterator; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|