|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawkode\Eidetic\EventStore\DBALEventStore; |
|
4
|
|
|
|
|
5
|
|
|
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity; |
|
6
|
|
|
use Rawkode\Eidetic\EventStore\EventStore; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Repository |
|
10
|
|
|
* @package Rawkode\Eidetic\EventStore\DBALEventStore |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Repository |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var $entityClass */ |
|
15
|
|
|
private $entityClass; |
|
16
|
|
|
|
|
17
|
|
|
/** @var EventStore $eventStore */ |
|
18
|
|
|
private $eventStore; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param EventSourcedEntity $class |
|
22
|
|
|
* @param EventStore $eventStore |
|
23
|
|
|
*/ |
|
24
|
|
|
private function __construct($class, EventStore $eventStore) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->entityClass = $class; |
|
27
|
|
|
$this->eventStore = $eventStore; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $class |
|
32
|
|
|
* @param EventStore $eventStore |
|
33
|
|
|
* @return Repository |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function createForType($class, EventStore $eventStore) |
|
36
|
|
|
{ |
|
37
|
|
|
return new self($class, $eventStore); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param $key |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
|
|
public function load($key) |
|
45
|
|
|
{ |
|
46
|
|
|
$class = $this->eventStore->getClassForKey($key); |
|
47
|
|
|
|
|
48
|
|
|
$this->enforceTypeConstraint($class); |
|
49
|
|
|
|
|
50
|
|
|
$events = $this->eventStore->retrieve($key); |
|
51
|
|
|
|
|
52
|
|
|
return $class::initialise($events); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param EventSourcedEntity $class |
|
57
|
|
|
* @throws IncorrectEntityException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function save(EventSourcedEntity $class) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->enforceTypeConstraint($class); |
|
|
|
|
|
|
62
|
|
|
$this->eventStore->store($class->identifier(), $class->stagedEvents()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $class |
|
67
|
|
|
* @throws IncorrectEntityException |
|
68
|
|
|
*/ |
|
69
|
|
|
private function enforceTypeConstraint($class) |
|
70
|
|
|
{ |
|
71
|
|
|
if (get_class($class) !== get_class($this->entityClass)) { |
|
72
|
|
|
throw new IncorrectEntityException(get_class($class) . " is not the same as " . get_class($this->entityClass)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: