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

HistoryProjector::applyLabelsMerged()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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