Passed
Push — master ( 2ee0d1...1e8995 )
by Tomasz
03:21
created

Collection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

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