Completed
Pull Request — master (#252)
by Kristof
09:54 queued 04:47
created

HistoryProjector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 219
Duplicated Lines 17.35 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 15
dl 38
loc 219
rs 9.1666
c 0
b 0
f 0

11 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
B applyLabelsMerged() 0 31 2
B applyTranslationApplied() 0 35 5
A applyTranslationDeleted() 0 23 2
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\LabelsMerged;
18
use CultuurNet\UDB3\Event\Events\TitleTranslated;
19
use CultuurNet\UDB3\Event\Events\TranslationApplied;
20
use CultuurNet\UDB3\Event\Events\TranslationDeleted;
21
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
22
use CultuurNet\UDB3\EventHandling\DelegateEventHandlingToSpecificMethodTrait;
23
use CultuurNet\UDB3\Offer\ReadModel\History\OfferHistoryProjector;
24
use CultuurNet\UDB3\ReadModel\JsonDocument;
25
use ValueObjects\String\String;
26
27
class HistoryProjector extends OfferHistoryProjector implements EventListenerInterface
28
{
29
30
    protected function applyEventImportedFromUDB2(
31
        EventImportedFromUDB2 $eventImportedFromUDB2,
32
        DomainMessage $domainMessage
33
    ) {
34
        $udb2Event = EventItemFactory::createEventFromCdbXml(
35
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
36
            $eventImportedFromUDB2->getCdbXml()
37
        );
38
39
        $this->writeHistory(
40
            $eventImportedFromUDB2->getEventId(),
41
            new Log(
42
                $this->dateFromUdb2DateString(
43
                    $udb2Event->getCreationDate()
44
                ),
45
                new String('Aangemaakt in UDB2'),
46
                new String($udb2Event->getCreatedBy())
47
            )
48
        );
49
50
        $this->writeHistory(
51
            $eventImportedFromUDB2->getEventId(),
52
            new Log(
53
                $this->domainMessageDateToNativeDate(
54
                    $domainMessage->getRecordedOn()
55
                ),
56
                new String('Geïmporteerd vanuit UDB2')
57
            )
58
        );
59
    }
60
61 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...
62
        EventCreatedFromCdbXml $eventCreatedFromCdbXml,
63
        DomainMessage $domainMessage
64
    ) {
65
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
66
67
        $this->writeHistory(
68
            $eventCreatedFromCdbXml->getEventId()->toNative(),
69
            new Log(
70
                $this->domainMessageDateToNativeDate(
71
                    $domainMessage->getRecordedOn()
72
                ),
73
                new String(
74
                    'Aangemaakt via EntryAPI door consumer "' . $consumerName . '"'
75
                ),
76
                $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...
77
            )
78
        );
79
    }
80
81 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...
82
        EventUpdatedFromCdbXml $eventUpdatedFromCdbXml,
83
        DomainMessage $domainMessage
84
    ) {
85
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
86
87
        $this->writeHistory(
88
            $eventUpdatedFromCdbXml->getEventId()->toNative(),
89
            new Log(
90
                $this->domainMessageDateToNativeDate(
91
                    $domainMessage->getRecordedOn()
92
                ),
93
                new String(
94
                    'Geüpdatet via EntryAPI door consumer "' . $consumerName . '"'
95
                ),
96
                $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...
97
            )
98
        );
99
    }
100
101
    protected function applyEventUpdatedFromUDB2(
102
        EventUpdatedFromUDB2 $eventUpdatedFromUDB2,
103
        DomainMessage $domainMessage
104
    ) {
105
        $this->writeHistory(
106
            $eventUpdatedFromUDB2->getEventId(),
107
            new Log(
108
                $this->domainMessageDateToNativeDate($domainMessage->getRecordedOn()),
109
                new String('Geüpdatet vanuit UDB2')
110
            )
111
        );
112
    }
113
114
    /**
115
     * @param LabelsMerged $labelsMerged
116
     * @param DomainMessage $domainMessage
117
     */
118
    protected function applyLabelsMerged(
119
        LabelsMerged $labelsMerged,
120
        DomainMessage $domainMessage
121
    ) {
122
        $labels = $labelsMerged->getLabels()->toStrings();
123
        // Quote labels.
124
        $quotedLabels = array_map(
125
            function ($label) {
126
                return "'{$label}'";
127
            },
128
            $labels
129
        );
130
        $quotedLabelsString = implode(', ', $quotedLabels);
131
132
        $message = "Labels {$quotedLabelsString} toegepast";
133
134
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
135
136
        if ($consumerName) {
137
            $message .= ' via EntryAPI door consumer "' . $consumerName . '"';
138
        }
139
140
        $this->writeHistory(
141
            $labelsMerged->getEventId()->toNative(),
142
            new Log(
143
                $this->domainMessageDateToNativeDate($domainMessage->getRecordedOn()),
144
                new String($message),
145
                $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...
146
            )
147
        );
148
    }
149
150
    protected function applyTranslationApplied(
151
        TranslationApplied $translationApplied,
152
        DomainMessage $domainMessage
153
    ) {
154
        $fields = [];
155
156
        if ($translationApplied->getTitle() !== null) {
157
            $fields[] = 'titel';
158
        }
159
        if ($translationApplied->getShortDescription() !== null) {
160
            $fields[] = 'korte beschrijving';
161
        }
162
        if ($translationApplied->getLongDescription() !== null) {
163
            $fields[] = 'lange beschrijving';
164
        }
165
        $fieldString = ucfirst(implode(', ', $fields));
166
167
        $logMessage = "{$fieldString} vertaald ({$translationApplied->getLanguage()->getCode()})";
168
169
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
170
        if ($consumerName) {
171
            $logMessage .= " via EntryAPI door consumer \"{$consumerName}\"";
172
        }
173
174
        $this->writeHistory(
175
            $translationApplied->getEventId()->toNative(),
176
            new Log(
177
                $this->domainMessageDateToNativeDate(
178
                    $domainMessage->getRecordedOn()
179
                ),
180
                new String($logMessage),
181
                $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...
182
            )
183
        );
184
    }
185
186
    /**
187
     * @param TranslationDeleted $translationDeleted
188
     * @param DomainMessage $domainMessage
189
     */
190
    protected function applyTranslationDeleted(
191
        TranslationDeleted $translationDeleted,
192
        DomainMessage $domainMessage
193
    ) {
194
        $message = "Vertaling verwijderd ({$translationDeleted->getLanguage()})";
195
196
        $consumerName = $this->getConsumerFromMetadata($domainMessage->getMetadata());
197
198
        if ($consumerName) {
199
            $message .= ' via EntryAPI door consumer "' . $consumerName . '"';
200
        }
201
202
        $this->writeHistory(
203
            $translationDeleted->getEventId()->toNative(),
204
            new Log(
205
                $this->domainMessageDateToNativeDate(
206
                    $domainMessage->getRecordedOn()
207
                ),
208
                new String($message),
209
                $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...
210
            )
211
        );
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    protected function getLabelAddedClassName()
218
    {
219
        return LabelAdded::class;
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    protected function getLabelRemovedClassName()
226
    {
227
        return LabelRemoved::class;
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    protected function getTitleTranslatedClassName()
234
    {
235
        return TitleTranslated::class;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    protected function getDescriptionTranslatedClassName()
242
    {
243
        return DescriptionTranslated::class;
244
    }
245
}
246