ProjectorState   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 162
c 0
b 0
f 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A name() 0 4 1
A lastEvent() 0 4 1
A lastEventWas() 0 6 1
A isHalted() 0 4 1
A halt() 0 6 1
A isProjecting() 0 4 1
A startProjecting() 0 4 1
A stopProjecting() 0 4 1
A projectorRunsFrom() 0 4 1
A whyIsHalted() 0 4 1
A jsonSerialize() 0 11 1
1
<?php
2
3
/**
4
 * This file is part of slick/cqrs-tools
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\CQRSTools\Domain\Projection;
11
12
use JsonSerializable;
13
use Slick\CQRSTools\Application\Projection\Projector;
14
use Slick\CQRSTools\Domain\Event\EventId;
15
use Slick\CQRSTools\Event;
16
17
/**
18
 * ProjectorState
19
 *
20
 * @package Slick\CQRSTools\Domain\Projection
21
*/
22
final class ProjectorState implements JsonSerializable
23
{
24
25
    /**
26
     * @var string
27
     */
28
    private $name;
29
30
    /**
31
     * @var EventId
32
     */
33
    private $lastEvent;
34
35
    /**
36
     * @var bool
37
     */
38
    private $halted = true;
39
40
    /**
41
     * @var bool
42
     */
43
    private $projecting = false;
44
45
    /**
46
     * @var int
47
     */
48
    private $projectorRunsFrom;
49
50
    /**
51
     * @var null|string
52
     */
53
    private $reason = null;
54
55
    /**
56
     * Creates a Projector State record
57
     *
58
     * @param Projector $projector
59
     */
60
    public function __construct(Projector $projector)
61
    {
62
        $this->name = get_class($projector);
63
        $this->projectorRunsFrom = $projector->runFrom();
64
    }
65
66
    /**
67
     * Projector name/identifier
68
     *
69
     * @return string
70
     */
71
    public function name(): string
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * Last played event
78
     *
79
     * @return null|EventId
80
     */
81
    public function lastEvent(): ?EventId
82
    {
83
        return $this->lastEvent;
84
    }
85
86
    /**
87
     * Registers last played event
88
     *
89
     * @param Event $event
90
     */
91
    public function lastEventWas(Event $event): void
92
    {
93
        $this->lastEvent = $event->eventId();
94
        $this->halted = false;
95
        $this->reason = null;
96
    }
97
98
    /**
99
     * Projector halt state
100
     *
101
     * @return bool
102
     */
103
    public function isHalted(): bool
104
    {
105
        return $this->halted;
106
    }
107
108
    /**
109
     * Halts the projector
110
     *
111
     * @param string $reason The reason why projector should be halted
112
     */
113
    public function halt(string $reason): void
114
    {
115
        $this->halted = true;
116
        $this->reason = $reason;
117
        $this->projecting = false;
118
    }
119
120
    /**
121
     * Reports whether or not this projector is projecting an event stream
122
     *
123
     * @return bool
124
     */
125
    public function isProjecting(): bool
126
    {
127
        return $this->projecting;
128
    }
129
130
    /**
131
     * Sets projecting state as started
132
     */
133
    public function startProjecting(): void
134
    {
135
        $this->projecting = true;
136
    }
137
138
    /**
139
     * Sets projecting state as stopped
140
     */
141
    public function stopProjecting(): void
142
    {
143
        $this->projecting = false;
144
    }
145
146
    /**
147
     * Projector run mode
148
     *
149
     * @return int
150
     */
151
    public function projectorRunsFrom(): int
152
    {
153
        return $this->projectorRunsFrom;
154
    }
155
156
    /**
157
     * Reason why the projector was halted
158
     *
159
     * @return string|null
160
     */
161
    public function whyIsHalted(): ?string
162
    {
163
        return $this->reason;
164
    }
165
166
    /**
167
     * Specify data which should be serialized to JSON
168
     *
169
     * @return mixed data which can be serialized by json_encode(),
170
     *               which is a value of any type other than a resource.
171
     */
172
    public function jsonSerialize()
173
    {
174
        return [
175
            'name' => $this->name,
176
            'projectorRunsFrom' => $this->projectorRunsFrom,
177
            'lastEvent' => $this->lastEvent,
178
            'projecting' => $this->projecting,
179
            'halted' => $this->halted,
180
            'reason' => $this->reason
181
        ];
182
    }
183
}
184