Completed
Push — master ( 67ee93...84505b )
by Ivannis Suárez
07:55
created

PublishedPostProjector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A projectPostWasCreated() 0 7 1
A projectPostTitleWasChanged() 0 4 1
A shouldBeProjected() 0 11 3
A shouldBeRemoved() 0 11 3
A writeModelClass() 0 4 1
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(),
0 ignored issues
show
Compatibility introduced by
$event->aggregateId() of type object<Cubiche\Domain\Model\IdInterface> is not a sub-type of object<Cubiche\Domain\Mo...\Tests\Fixtures\PostId>. It seems like you assume a concrete implementation of the interface Cubiche\Domain\Model\IdInterface to be always present.

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.

Loading history...
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