Completed
Push — master ( efb36f...19228d )
by David
02:15
created

Repository::createForWrites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Rawkode\Eidetic\EventSourcing;
4
5
use Rawkode\Eidetic\EventStore\EventStore;
6
7
/**
8
 * Class Repository
9
 * @package Rawkode\Eidetic\EventSourcing
10
 */
11
final class Repository
12
{
13
    /** @var $entityClass */
14
    private $entityClass;
15
16
    /** @var EventStore $eventStore */
17
    private $eventStore;
18
19
    /**
20
     * @param string $class
21
     * @param EventStore $eventStore
22
     */
23
    private function __construct($class, EventStore $eventStore)
24
    {
25
        $this->entityClass = $class;
26
        $this->eventStore = $eventStore;
27
    }
28
29
    /**
30
     * @param $class
31
     * @param EventStore $eventStore
32
     * @return Repository
33
     */
34
    public static function createForWrites($class, EventStore $eventStore)
35
    {
36
        return new self($class, $eventStore);
37
    }
38
39
    /**
40
     * @param $key
41
     * @return mixed
42
     */
43
    public function load($key)
44
    {
45
        $events = $this->eventStore->retrieve($key);
46
47
        return call_user_func(array($this->entityClass, 'initialise'), $events);
48
    }
49
50
    /**
51
     * @param EventSourcedEntity $eventSourcedEntity
52
     * @throws IncorrectEntityClassException
53
     */
54
    public function save(EventSourcedEntity $eventSourcedEntity)
55
    {
56
        $this->eventStore->store($eventSourcedEntity->identifier(), $eventSourcedEntity->stagedEvents());
57
    }
58
}
59