Passed
Push — master ( 7a10ae...94a94d )
by Tomasz
01:49
created

Collection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Aggrego\Domain\ProgressiveBoard\Shard;
6
7
use Aggrego\Domain\ProgressiveBoard\Exception\InvalidUuidComparisonOnReplaceException;
8
use ArrayIterator;
9
use Assert\Assertion;
10
use Countable;
11
use Iterator;
12
use IteratorAggregate;
13
use Traversable;
14
15
class Collection implements Countable, IteratorAggregate
16
{
17
    /** @var Item[] */
18
    private $list;
19
20
    public function __construct(array $list)
21
    {
22
        Assertion::allIsInstanceOf($list, InitialItem::class);
23
        $this->list = $list;
24
    }
25
26
    public function replace(FinalItem $finalShard): void
27
    {
28
        /** @var Item $shard */
29
        $list = $this->list;
30
        $listUuid = [];
31
        foreach ($list as $key => $shard) {
32
            if ($shard instanceof FinalItem) {
33
                continue;
34
            }
35
            $listUuid[] = $shard->getUuid()->getValue();
36
            if ($shard->getUuid()->equal($finalShard->getUuid())
37
                && $shard->getProfile()->equal($finalShard->getProfile())) {
38
                $list[$key] = $finalShard;
39
                $this->list = $list;
40
                return;
41
            }
42
        }
43
44
        throw new InvalidUuidComparisonOnReplaceException(
45
            sprintf(
46
                'Could not find initial shard by uuid %s in given collection: %s',
47
                $finalShard->getUuid()->getValue(),
48
                join(',', $listUuid)
49
            )
50
        );
51
    }
52
53
    public function count(): int
54
    {
55
        return count($this->list);
56
    }
57
58
    public function isAllShardsFinishedProgress(): bool
59
    {
60
        foreach ($this as $shard) {
61
            if ($shard instanceof InitialItem) {
62
                return false;
63
            }
64
        }
65
        return true;
66
    }
67
68
    public function getIterator(): Iterator
69
    {
70
        return new ArrayIterator($this->list);
71
    }
72
}
73