Passed
Push — master ( dbfb37...74a6ed )
by Frank
46s queued 13s
created

LightSwitch   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 45
ccs 19
cts 19
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyLightSwitchWasFlipped() 0 3 1
A turnOn() 0 4 2
A createSnapshotState() 0 3 1
A reconstituteFromSnapshotState() 0 6 1
A state() 0 3 1
A turnOff() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\Snapshotting\Tests;
6
7
use EventSauce\EventSourcing\AggregateRootBehaviour;
8
use EventSauce\EventSourcing\AggregateRootId;
9
use EventSauce\EventSourcing\Snapshotting\SnapshottingBehaviour;
10
use EventSauce\EventSourcing\Snapshotting\AggregateRootWithSnapshotting;
11
12
class LightSwitch implements AggregateRootWithSnapshotting
13
{
14
    use AggregateRootBehaviour;
15
    use SnapshottingBehaviour;
16
17
    const OFF = false;
18
    const ON = true;
19
20
    private $state = self::OFF;
21
22 1
    private function createSnapshotState()
23
    {
24 1
        return $this->state;
25
    }
26
27 1
    public function state(): bool
28
    {
29 1
        return $this->state;
30
    }
31
32 3
    public function turnOn(): void
33
    {
34 3
        if (self::OFF == $this->state) {
35 2
            $this->recordThat(LightSwitchWasFlipped::on());
36
        }
37 3
    }
38
39 3
    public function turnOff(): void
40
    {
41 3
        if (self::ON == $this->state) {
42 2
            $this->recordThat(LightSwitchWasFlipped::off());
43
        }
44 3
    }
45
46 4
    protected function applyLightSwitchWasFlipped(LightSwitchWasFlipped $event): void
47
    {
48 4
        $this->state = $event->state();
49 4
    }
50
51 1
    protected static function reconstituteFromSnapshotState(AggregateRootId $id, bool $state): AggregateRootWithSnapshotting
52
    {
53 1
        $lightSwitch = new static($id);
54 1
        $lightSwitch->state = $state;
55
56 1
        return $lightSwitch;
57
    }
58
}
59