Completed
Pull Request — master (#135)
by Kristof
04:45
created

replaceEventIdWithItemId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3;
7
8
use Broadway\Serializer\SerializerInterface;
9
use Broadway\Serializer\SimpleInterfaceSerializer;
10
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
11
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
12
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
13
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
14
use CultuurNet\UDB3\Event\Events\LabelAdded;
15
use CultuurNet\UDB3\Event\Events\LabelsMerged;
16
use CultuurNet\UDB3\Event\Events\LabelDeleted;
17
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
18
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
19
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
20
use CultuurNet\UDB3\Event\Events\TitleTranslated;
21
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
22
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
23
use CultuurNet\UDB3\EventSourcing\PayloadManipulatingSerializer;
24
use CultuurNet\UDB3\UsedLabelsMemory\Created as UsedLabelsMemoryCreated;
25
use CultuurNet\UDB3\UsedLabelsMemory\LabelUsed;
26
27
/**
28
 * Factory chaining together the logic to manipulate the payload of old events
29
 * in order to make it usable by new events.
30
 *
31
 * Some cases:
32
 * - changing the class name / namespace after class renames
33
 * - changing the names of properties
34
 */
35
class BackwardsCompatiblePayloadSerializerFactory
36
{
37
38
    private function __construct()
39
    {
40
41
    }
42
43
    /**
44
     * @return SerializerInterface
45
     */
46
    public static function createSerializer()
47
    {
48
        $payloadManipulatingSerializer = new PayloadManipulatingSerializer(
49
            new SimpleInterfaceSerializer()
50
        );
51
52
        /*
53
         * KEYWORDS EVENTS
54
         */
55
56
        $payloadManipulatingSerializer->manipulateEventsOfClass(
57
            'CultuurNet\UDB3\UsedKeywordsMemory\Created',
58
            function (array $serializedObject) {
59
                $serializedObject['class'] = UsedLabelsMemoryCreated::class;
60
61
                return $serializedObject;
62
            }
63
        );
64
65
        $payloadManipulatingSerializer->manipulateEventsOfClass(
66
            'CultuurNet\UDB3\UsedKeywordsMemory\KeywordUsed',
67
            function (array $serializedObject) {
68
                $serializedObject['class'] = LabelUsed::class;
69
70
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
71
72
                return $serializedObject;
73
            }
74
        );
75
76
        /*
77
         * TRANSLATION EVENTS
78
         */
79
80
        $payloadManipulatingSerializer->manipulateEventsOfClass(
81
            'CultuurNet\UDB3\Event\TitleTranslated',
82
            function (array $serializedObject) {
83
                $serializedObject['class'] = TitleTranslated::class;
84
85
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
86
87
                return $serializedObject;
88
            }
89
        );
90
91
        $payloadManipulatingSerializer->manipulateEventsOfClass(
92
            'CultuurNet\UDB3\Event\DescriptionTranslated',
93
            function (array $serializedObject) {
94
                $serializedObject['class'] = DescriptionTranslated::class;
95
96
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
97
98
                return $serializedObject;
99
            }
100
        );
101
102
        /*
103
         * LABEL EVENTS
104
         */
105
106
        $payloadManipulatingSerializer->manipulateEventsOfClass(
107
            'CultuurNet\UDB3\Event\Events\EventWasLabelled',
108
            function (array $serializedObject) {
109
                $serializedObject['class'] = LabelAdded::class;
110
111
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
112
113
                return $serializedObject;
114
            }
115
        );
116
117
        $payloadManipulatingSerializer->manipulateEventsOfClass(
118
            'CultuurNet\UDB3\Event\EventWasTagged',
119 View Code Duplication
            function (array $serializedObject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
                $serializedObject['class'] = LabelAdded::class;
121
122
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
123
124
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
125
126
                return $serializedObject;
127
            }
128
        );
129
130
        $payloadManipulatingSerializer->manipulateEventsOfClass(
131
            'CultuurNet\UDB3\Event\TagErased',
132 View Code Duplication
            function (array $serializedObject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
133
                $serializedObject['class'] = LabelDeleted::class;
134
135
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
136
137
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
138
139
                return $serializedObject;
140
            }
141
        );
142
143
        $payloadManipulatingSerializer->manipulateEventsOfClass(
144
            'CultuurNet\UDB3\Event\Events\Unlabelled',
145
            function (array $serializedObject) {
146
                $serializedObject['class'] = LabelDeleted::class;
147
148
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
149
150
                return $serializedObject;
151
            }
152
        );
153
154
        $payloadManipulatingSerializer->manipulateEventsOfClass(
155
            'CultuurNet\UDB3\Event\Events\LabelsApplied',
156
            function (array $serializedObject) {
157
                $serializedObject['class'] = LabelsMerged::class;
158
159
                $keywordsString = $serializedObject['payload']['keywords_string'];
160
161
                $query = array();
162
                parse_str($keywordsString, $query);
163
164
                $keywords = explode(';', $query['keywords']);
165
                $visibles = explode(';', $query['visibles']);
166
167
                $labelsArray = array();
168
169
                foreach ($keywords as $key => $keyword) {
170
                    $visible = 'true' === $visibles[$key];
171
                    $labelsArray[] = new Label(
172
                        $keyword,
173
                        $visible
174
                    );
175
                }
176
177
                $labels = array_map(
178
                    function (Label $label) {
179
                        return [
180
                            'text' => (string) $label,
181
                            'visible' => $label->isVisible(),
182
                        ];
183
                    },
184
                    $labelsArray
185
                );
186
187
                $serializedObject['payload']['labels'] = $labels;
188
                unset($serializedObject['payload']['keywords_string']);
189
190
                return $serializedObject;
191
            }
192
        );
193
194
        /**
195
         * UBD2 IMPORT
196
         */
197
198
        $payloadManipulatingSerializer->manipulateEventsOfClass(
199
            'CultuurNet\UDB3\Event\EventImportedFromUDB2',
200
            function (array $serializedObject) {
201
                $serializedObject['class'] = EventImportedFromUDB2::class;
202
203
                return $serializedObject;
204
            }
205
        );
206
207
        /**
208
         * BOOKING INFO
209
         */
210
211
        $payloadManipulatingSerializer->manipulateEventsOfClass(
212
            BookingInfoUpdated::class,
213
            function (array $serializedObject) {
214
215
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
216
217
                return $serializedObject;
218
            }
219
        );
220
221
        /**
222
         * TYPICAL AGE RANGE
223
         */
224
225
        $payloadManipulatingSerializer->manipulateEventsOfClass(
226
            TypicalAgeRangeDeleted::class,
227
            function (array $serializedObject) {
228
229
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
230
231
                return $serializedObject;
232
            }
233
        );
234
235
        $payloadManipulatingSerializer->manipulateEventsOfClass(
236
            TypicalAgeRangeUpdated::class,
237
            function (array $serializedObject) {
238
239
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
240
241
                return $serializedObject;
242
            }
243
        );
244
245
        /**
246
         * CONTACT POINT
247
         */
248
249
        $payloadManipulatingSerializer->manipulateEventsOfClass(
250
            ContactPointUpdated::class,
251
            function (array $serializedObject) {
252
253
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
254
255
                return $serializedObject;
256
            }
257
        );
258
259
        /**
260
         *  MAJOR INFO
261
         */
262
263
        $payloadManipulatingSerializer->manipulateEventsOfClass(
264
            MajorInfoUpdated::class,
265
            function (array $serializedObject) {
266
267
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
268
269
                return $serializedObject;
270
            }
271
        );
272
273
        /**
274
         * ORGANIZER
275
         */
276
277
        $payloadManipulatingSerializer->manipulateEventsOfClass(
278
            OrganizerUpdated::class,
279
            function (array $serializedObject) {
280
281
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
282
283
                return $serializedObject;
284
            }
285
        );
286
287
        $payloadManipulatingSerializer->manipulateEventsOfClass(
288
            OrganizerDeleted::class,
289
            function (array $serializedObject) {
290
291
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
292
293
                return $serializedObject;
294
            }
295
        );
296
297
        return $payloadManipulatingSerializer;
298
    }
299
300
    /**
301
     * @param array $serializedObject
302
     * @return array
303
     */
304
    private static function replaceEventIdWithItemId(array $serializedObject)
305
    {
306
        if (isset($serializedObject['payload']['event_id'])) {
307
            $eventId = $serializedObject['payload']['event_id'];
308
            $serializedObject['payload']['item_id'] = $eventId;
309
            unset($serializedObject['payload']['event_id']);
310
        }
311
312
        return $serializedObject;
313
    }
314
315
    /**
316
     * @param array $serializedObject
317
     * @return array
318
     */
319
    private static function replaceKeywordWithLabel(array $serializedObject)
320
    {
321
        $keyword = $serializedObject['payload']['keyword'];
322
        $serializedObject['payload']['label'] = $keyword;
323
        unset($serializedObject['payload']['keyword']);
324
325
        return $serializedObject;
326
    }
327
}
328