LightSwitch   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 48
ccs 15
cts 15
cp 1
rs 10
c 3
b 0
f 0
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\AggregateRootWithSnapshotting;
10
use EventSauce\EventSourcing\Snapshotting\SnapshottingBehaviour;
11
12
final class LightSwitch implements AggregateRootWithSnapshotting
13
{
14
    use AggregateRootBehaviour;
15
    use SnapshottingBehaviour;
16
17
    public const OFF = false;
18
    public const ON = true;
19
20
    private bool $state = self::OFF;
21
22
    private function createSnapshotState(): bool
0 ignored issues
show
Unused Code introduced by
The method createSnapshotState() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
23
    {
24
        return $this->state;
25
    }
26
27
    public function state(): bool
28 1
    {
29
        return $this->state;
30 1
    }
31
32
    public function turnOn(): void
33 1
    {
34
        if (self::OFF == $this->state) {
35 1
            $this->recordThat(LightSwitchWasFlipped::on());
36
        }
37
    }
38 3
39
    public function turnOff(): void
40 3
    {
41 2
        if (self::ON == $this->state) {
42
            $this->recordThat(LightSwitchWasFlipped::off());
43 3
        }
44
    }
45 3
46
    protected function applyLightSwitchWasFlipped(LightSwitchWasFlipped $event): void
47 3
    {
48 2
        $this->state = $event->state();
49
    }
50 3
51
    /**
52 4
     * @param bool $state
53
     */
54 4
    protected static function reconstituteFromSnapshotState(AggregateRootId $id, $state): AggregateRootWithSnapshotting
55 4
    {
56
        $lightSwitch = new static($id);
57
        $lightSwitch->state = (bool) $state;
58
59
        return $lightSwitch;
60 1
    }
61
}
62