Completed
Pull Request — master (#252)
by Kristof
05:51 queued 49s
created

HistoryProjector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 31.93 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 38
loc 119
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B applyEventImportedFromUDB2() 0 30 1
A applyEventCreatedFromCdbXml() 19 19 1
A applyEventUpdatedFromCdbXml() 19 19 1
A applyEventUpdatedFromUDB2() 0 12 1
A getLabelAddedClassName() 0 4 1
A getLabelRemovedClassName() 0 4 1
A getTitleTranslatedClassName() 0 4 1
A getDescriptionTranslatedClassName() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\History;
4
5
use Broadway\Domain\DateTime;
6
use Broadway\Domain\DomainMessage;
7
use Broadway\Domain\Metadata;
8
use Broadway\EventHandling\EventListenerInterface;
9
use CultuurNet\UDB3\Cdb\EventItemFactory;
10
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
11
use CultuurNet\UDB3\Event\Events\EventCreatedFromCdbXml;
12
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
13
use CultuurNet\UDB3\Event\Events\EventUpdatedFromCdbXml;
14
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
15
use CultuurNet\UDB3\Event\Events\LabelAdded;
16
use CultuurNet\UDB3\Event\Events\LabelRemoved;
17
use CultuurNet\UDB3\Event\Events\TitleTranslated;
18
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
19
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
20
use CultuurNet\UDB3\Offer\ReadModel\History\OfferHistoryProjector;
21
use CultuurNet\UDB3\ReadModel\JsonDocument;
22
use ValueObjects\String\String;
23
24
class HistoryProjector extends OfferHistoryProjector implements EventListenerInterface
25
{
26
27
    protected function applyEventImportedFromUDB2(
28
        EventImportedFromUDB2 $eventImportedFromUDB2,
29
        DomainMessage $domainMessage
30
    ) {
31
        $udb2Event = EventItemFactory::createEventFromCdbXml(
32
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
33
            $eventImportedFromUDB2->getCdbXml()
34
        );
35
36
        $this->writeHistory(
37
            $eventImportedFromUDB2->getEventId(),
38
            new Log(
39
                $this->dateFromUdb2DateString(
40
                    $udb2Event->getCreationDate()
41
                ),
42
                new String('Aangemaakt in UDB2'),
43
                new String($udb2Event->getCreatedBy())
44
            )
45
        );
46
47
        $this->writeHistory(
48
            $eventImportedFromUDB2->getEventId(),
49
            new Log(
50
                $this->domainMessageDateToNativeDate(
51
                    $domainMessage->getRecordedOn()
52
                ),
53
                new String('Geïmporteerd vanuit UDB2')
54
            )
55
        );
56
    }
57
58 View Code Duplication
    protected function applyEventCreatedFromCdbXml(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
        EventCreatedFromCdbXml $eventCreatedFromCdbXml,
60
        DomainMessage $domainMessage
61
    ) {
62
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
63
64
        $this->writeHistory(
65
            $eventCreatedFromCdbXml->getEventId()->toNative(),
66
            new Log(
67
                $this->domainMessageDateToNativeDate(
68
                    $domainMessage->getRecordedOn()
69
                ),
70
                new String(
71
                    'Aangemaakt via EntryAPI door consumer "' . $consumerName . '"'
72
                ),
73
                $this->getAuthorFromMetadata($domainMessage->getMetadata())
0 ignored issues
show
Bug introduced by
It seems like $this->getAuthorFromMeta...Message->getMetadata()) targeting CultuurNet\UDB3\Offer\Re...getAuthorFromMetadata() can also be of type object<ValueObjects\String\String>; however, CultuurNet\UDB3\Event\Re...tory\Log::__construct() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
            )
75
        );
76
    }
77
78 View Code Duplication
    protected function applyEventUpdatedFromCdbXml(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
        EventUpdatedFromCdbXml $eventUpdatedFromCdbXml,
80
        DomainMessage $domainMessage
81
    ) {
82
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
83
84
        $this->writeHistory(
85
            $eventUpdatedFromCdbXml->getEventId()->toNative(),
86
            new Log(
87
                $this->domainMessageDateToNativeDate(
88
                    $domainMessage->getRecordedOn()
89
                ),
90
                new String(
91
                    'Geüpdatet via EntryAPI door consumer "' . $consumerName . '"'
92
                ),
93
                $this->getAuthorFromMetadata($domainMessage->getMetadata())
0 ignored issues
show
Bug introduced by
It seems like $this->getAuthorFromMeta...Message->getMetadata()) targeting CultuurNet\UDB3\Offer\Re...getAuthorFromMetadata() can also be of type object<ValueObjects\String\String>; however, CultuurNet\UDB3\Event\Re...tory\Log::__construct() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
            )
95
        );
96
    }
97
98
    protected function applyEventUpdatedFromUDB2(
99
        EventUpdatedFromUDB2 $eventUpdatedFromUDB2,
100
        DomainMessage $domainMessage
101
    ) {
102
        $this->writeHistory(
103
            $eventUpdatedFromUDB2->getEventId(),
104
            new Log(
105
                $this->domainMessageDateToNativeDate($domainMessage->getRecordedOn()),
106
                new String('Geüpdatet vanuit UDB2')
107
            )
108
        );
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    protected function getLabelAddedClassName()
115
    {
116
        return LabelAdded::class;
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    protected function getLabelRemovedClassName()
123
    {
124
        return LabelRemoved::class;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    protected function getTitleTranslatedClassName()
131
    {
132
        return TitleTranslated::class;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    protected function getDescriptionTranslatedClassName()
139
    {
140
        return DescriptionTranslated::class;
141
    }
142
}
143