TimeMachine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 48
wmc 9
lcom 1
cbo 2
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A attachListener() 0 4 1
A attachListeners() 0 8 2
B replayEvents() 0 22 5
1
<?php
2
3
namespace Domain\Eventing;
4
5
use Domain\Identity\Identity;
6
use Domain\Tools\ClassToString;
7
8
/**
9
 * @author Sebastiaan Hilbers <[email protected]>
10
 */
11
class TimeMachine
12
{
13
    protected $store;
14
15
    protected $listeners = [];
16
17
    public function __construct(EventStore $store)
18
    {
19
        $this->store = $store;
20
    }
21
22
    public function attachListener($eventName, Listener $listener)
23
    {
24
        $this->listeners[$eventName][] = $listener;
25
    }
26
27
    public function attachListeners(array $listeners)
28
    {
29
        foreach ($listeners as $event => $listener) {
30
            $this->attachListener($event, $listener);
31
        }
32
33
        return $this;
34
    }
35
36
    public function replayEvents(Identity $identity, $offset = null, $max = null)
37
    {
38
        $events = $this->store->getAggregateHistoryFor($identity, $offset, $max);
39
40
        foreach ($events as $event) {
41
            $method = 'when' . ClassToString::short($event);
42
43
            if (!isset($this->listeners[$method])) {
44
                continue;
45
            }
46
47
            if (is_array($this->listeners[$method])) {
48
                foreach ($this->listeners[$method] as $listener) {
49
                    call_user_func($listener, $event);
50
                }
51
            } else {
52
                call_user_func($this->listeners[$method], $event);
53
            }
54
        }
55
56
        return $this;
57
    }
58
}
59