Passed
Push — master ( 41ca68...010c64 )
by Jesse
01:36
created

StateRepresentation   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 19

17 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 3 1
A count() 0 3 1
A offsetGet() 0 3 1
A key() 0 3 1
A offsetExists() 0 3 1
A entityStates() 0 3 1
A with() 0 5 1
A offsetUnset() 0 3 1
A __construct() 0 6 1
A add() 0 14 2
A offsetSet() 0 3 1
A changesSince() 0 3 1
A rewind() 0 3 1
A addToMapIfNew() 0 12 2
A valid() 0 3 1
A next() 0 3 1
A identityMap() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState;
4
5
use Stratadox\IdentityMap\MapsObjectsByIdentity;
6
7
final class StateRepresentation implements State
8
{
9
    private $entityStates;
10
    private $map;
11
12
    public function __construct(
13
        ListsEntityStates $entities,
14
        MapsObjectsByIdentity $map
15
    ) {
16
        $this->entityStates = $entities;
17
        $this->map = $map;
18
    }
19
20
    public static function with(
21
        ListsEntityStates $entities,
22
        MapsObjectsByIdentity $map
23
    ): State {
24
        return new self($entities, $map);
25
    }
26
27
    public function entityStates(): ListsEntityStates
28
    {
29
        return $this->entityStates;
30
    }
31
32
    public function identityMap(): MapsObjectsByIdentity
33
    {
34
        return $this->map;
35
    }
36
37
    public function add(State $additional): State
38
    {
39
        $entityStates = $this->entityStates;
40
        $map = $this->map;
41
        foreach ($additional->entityStates() as $entityState) {
42
            $entityStates = $entityStates->add($entityState);
43
            $map = $this->addToMapIfNew(
44
                $map,
45
                $additional->identityMap(),
46
                $entityState->class(),
47
                $entityState->id()
48
            );
49
        }
50
        return new self($entityStates, $map);
51
    }
52
53
    public function changesSince(State $previous): TellsWhatChanged
54
    {
55
        return $this->entityStates->changesSince($previous->entityStates());
56
    }
57
58
    public function offsetExists($offset): bool
59
    {
60
        return $this->entityStates->offsetExists($offset);
61
    }
62
63
    public function offsetGet($offset): RepresentsEntity
64
    {
65
        return $this->entityStates->offsetGet($offset);
66
    }
67
68
    public function offsetSet($offset, $value): void
69
    {
70
        $this->entityStates->offsetSet($offset, $value);
71
    }
72
73
    public function offsetUnset($offset): void
74
    {
75
        $this->entityStates->offsetUnset($offset);
76
    }
77
78
    public function next(): void
79
    {
80
        $this->entityStates->next();
81
    }
82
83
    public function key(): int
84
    {
85
        return $this->entityStates->key();
86
    }
87
88
    public function valid(): bool
89
    {
90
        return $this->entityStates->valid();
91
    }
92
93
    public function rewind(): void
94
    {
95
        $this->entityStates->rewind();
96
    }
97
98
    public function current(): RepresentsEntity
99
    {
100
        return $this->entityStates->current();
101
    }
102
103
    public function count(): int
104
    {
105
        return $this->entityStates->count();
106
    }
107
108
    private function addToMapIfNew(
109
        MapsObjectsByIdentity $map,
110
        MapsObjectsByIdentity $additionalMap,
111
        string $class,
112
        string $id
113
    ): MapsObjectsByIdentity {
114
        if ($map->has($class, $id)) {
115
            return $map;
116
        }
117
        return $map->add(
118
            $id,
119
            $additionalMap->get($class, $id)
120
        );
121
    }
122
}
123