Passed
Push — master ( 33b14d...e7440c )
by Thorsten
01:45
created

StreamMap::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/cqrs project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\EventSourcing\EventStore;
12
13
use Countable;
14
use Ds\Map;
15
use Iterator;
16
use IteratorAggregate;
17
18
final class StreamMap implements IteratorAggregate, Countable
19
{
20
    /** @var Map */
21
    private $compositeMap;
22
23 2
    public static function makeEmpty(): self
24
    {
25 2
        return new self;
26
    }
27
28 2
    public function __construct(Stream ...$commitStream)
29
    {
30 2
        $this->compositeMap = new Map($commitStream);
31 2
    }
32
33
    public function register(StreamInterface $commitStream): self
34
    {
35
        $copy = clone $this;
36
        $copy->compositeMap->put((string)$commitStream->getStreamId(), $commitStream);
37
        return $copy;
38
    }
39
40
    public function unregister(StreamInterface $commitStream): self
41
    {
42
        $copy = clone $this;
43
        $copy->compositeMap->remove((string)$commitStream->getStreamId());
44
        return $copy;
45
    }
46
47 2
    public function has(string $key): bool
48
    {
49 2
        return $this->compositeMap->hasKey($key);
50
    }
51
52
    public function get(string $key): Stream
53
    {
54
        return $this->compositeMap->get($key);
55
    }
56
57
    public function count(): int
58
    {
59
        return count($this->compositeMap);
60
    }
61
62
    public function toArray(): array
63
    {
64
        return $this->compositeMap->toArray();
65
    }
66
67
    public function toNative(): array
68
    {
69
        $commitStreams = [];
70
        foreach ($this->compositeMap as $key => $commitStream) {
71
            $commitStreams[$key] = $commitStream->toNative();
72
        }
73
        return $commitStreams;
74
    }
75
76
    public function isEmpty(): bool
77
    {
78
        return $this->compositeMap->isEmpty();
79
    }
80
81
    public function getIterator(): Iterator
82
    {
83
        return $this->compositeMap->getIterator();
84
    }
85
86
    public function __get(string $key): Stream
87
    {
88
        return $this->get($key);
89
    }
90
91
    private function __clone()
92
    {
93
        $this->compositeMap = new Map($this->compositeMap->toArray());
94
    }
95
}
96