1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of the Aggrego. |
5
|
|
|
* (c) Tomasz Kunicki <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Aggrego\FragmentedDataBoardDomain\Board\Shard; |
15
|
|
|
|
16
|
|
|
use Aggrego\Domain\Profile\Profile; |
17
|
|
|
use Aggrego\FragmentedDataBoardDomain\Board\Exception\InvalidUuidComparisonOnReplaceException; |
18
|
|
|
use ArrayIterator; |
19
|
|
|
use Assert\Assertion; |
20
|
|
|
use Countable; |
21
|
|
|
use Iterator; |
22
|
|
|
use IteratorAggregate; |
23
|
|
|
|
24
|
|
|
class Collection implements Countable, IteratorAggregate |
25
|
|
|
{ |
26
|
|
|
/** @var Item[] */ |
27
|
|
|
private $list; |
28
|
|
|
|
29
|
|
|
public function __construct(array $list) |
30
|
|
|
{ |
31
|
|
|
Assertion::allIsInstanceOf($list, InitialItem::class); |
32
|
|
|
$this->list = $list; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function replace(FinalItem $finalShard): void |
36
|
|
|
{ |
37
|
|
|
/** @var Item $shard */ |
38
|
|
|
$list = $this->list; |
39
|
|
|
$listUuid = []; |
40
|
|
|
foreach ($list as $key => $shard) { |
41
|
|
|
if ($shard instanceof FinalItem) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
$listUuid[] = $shard->getUuid()->getValue(); |
45
|
|
|
if ($shard->getUuid()->equal($finalShard->getUuid()) |
46
|
|
|
&& $shard->getProfile()->equal($finalShard->getProfile())) { |
47
|
|
|
$list[$key] = $finalShard; |
48
|
|
|
$this->list = $list; |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new InvalidUuidComparisonOnReplaceException( |
54
|
|
|
sprintf( |
55
|
|
|
'Could not find initial shard by uuid %s in given collection: %s', |
56
|
|
|
$finalShard->getUuid()->getValue(), |
57
|
|
|
join(',', $listUuid) |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function count(): int |
63
|
|
|
{ |
64
|
|
|
return count($this->list); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function isAllShardsFinishedProgress(): bool |
68
|
|
|
{ |
69
|
|
|
foreach ($this as $shard) { |
70
|
|
|
if ($shard instanceof InitialItem) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getIterator(): Iterator |
78
|
|
|
{ |
79
|
|
|
return new ArrayIterator($this->list); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function findAllByProfile(Profile $profile): Iterator |
83
|
|
|
{ |
84
|
|
|
$list = []; |
85
|
|
|
foreach ($this->list as $item) { |
86
|
|
|
if ($item->getProfile()->equal($profile)) { |
87
|
|
|
$list[] = $item; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return new ArrayIterator($list); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|