1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\EventSourcing\Tests\Fixtures\Projector; |
13
|
|
|
|
14
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\EventStream; |
15
|
|
|
use Cubiche\Domain\EventSourcing\Projector\Projector; |
16
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostTitleWasChanged; |
17
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostWasCreated; |
18
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostWasPublished; |
19
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostWasUnPublished; |
20
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourced; |
21
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\ReadModel\PublishedPost; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PublishedPostProjector class. |
25
|
|
|
* |
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class PublishedPostProjector extends Projector |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param PostWasCreated $event |
32
|
|
|
* |
33
|
|
|
* @return PublishedPost |
34
|
|
|
*/ |
35
|
|
|
public function projectPostWasCreated(PostWasCreated $event) |
36
|
|
|
{ |
37
|
|
|
return new PublishedPost( |
38
|
|
|
$event->aggregateId(), |
|
|
|
|
39
|
|
|
$event->title() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param PostTitleWasChanged $event |
45
|
|
|
* @param PublishedPost $readModel |
46
|
|
|
*/ |
47
|
|
|
public function projectPostTitleWasChanged(PostTitleWasChanged $event, PublishedPost $readModel) |
48
|
|
|
{ |
49
|
|
|
$readModel->setTitle($event->title()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param EventStream $eventStream |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
protected function shouldBeProjected(EventStream $eventStream) |
58
|
|
|
{ |
59
|
|
|
$events = $eventStream->events(); |
60
|
|
|
foreach ($events as $event) { |
61
|
|
|
if ($event instanceof PostWasPublished) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param EventStream $eventStream |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
protected function shouldBeRemoved(EventStream $eventStream) |
75
|
|
|
{ |
76
|
|
|
$events = $eventStream->events(); |
77
|
|
|
foreach ($events as $event) { |
78
|
|
|
if ($event instanceof PostWasUnPublished) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function writeModelClass() |
90
|
|
|
{ |
91
|
|
|
return PostEventSourced::class; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.