Completed
Pull Request — master (#135)
by Kristof
05:09
created

replaceKeywordWithLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
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\EventImportedFromUDB2;
11
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
12
use CultuurNet\UDB3\Event\Events\LabelAdded;
13
use CultuurNet\UDB3\Event\Events\LabelsMerged;
14
use CultuurNet\UDB3\Event\Events\LabelDeleted;
15
use CultuurNet\UDB3\Event\Events\TitleTranslated;
16
use CultuurNet\UDB3\EventSourcing\PayloadManipulatingSerializer;
17
use CultuurNet\UDB3\UsedLabelsMemory\Created as UsedLabelsMemoryCreated;
18
use CultuurNet\UDB3\UsedLabelsMemory\LabelUsed;
19
20
/**
21
 * Factory chaining together the logic to manipulate the payload of old events
22
 * in order to make it usable by new events.
23
 *
24
 * Some cases:
25
 * - changing the class name / namespace after class renames
26
 * - changing the names of properties
27
 */
28
class BackwardsCompatiblePayloadSerializerFactory
29
{
30
31
    private function __construct()
32
    {
33
34
    }
35
36
    /**
37
     * @return SerializerInterface
38
     */
39
    public static function createSerializer()
40
    {
41
        $payloadManipulatingSerializer = new PayloadManipulatingSerializer(
42
            new SimpleInterfaceSerializer()
43
        );
44
45
        /*
46
         * KEYWORDS EVENTS
47
         */
48
49
        $payloadManipulatingSerializer->manipulateEventsOfClass(
50
            'CultuurNet\UDB3\UsedKeywordsMemory\Created',
51
            function (array $serializedObject) {
52
                $serializedObject['class'] = UsedLabelsMemoryCreated::class;
53
54
                return $serializedObject;
55
            }
56
        );
57
58
        $payloadManipulatingSerializer->manipulateEventsOfClass(
59
            'CultuurNet\UDB3\UsedKeywordsMemory\KeywordUsed',
60
            function (array $serializedObject) {
61
                $serializedObject['class'] = LabelUsed::class;
62
63
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
64
65
                return $serializedObject;
66
            }
67
        );
68
69
        /*
70
         * TRANSLATION EVENTS
71
         */
72
73
        $payloadManipulatingSerializer->manipulateEventsOfClass(
74
            'CultuurNet\UDB3\Event\TitleTranslated',
75
            function (array $serializedObject) {
76
                $serializedObject['class'] = TitleTranslated::class;
77
78
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
79
80
                return $serializedObject;
81
            }
82
        );
83
84
        $payloadManipulatingSerializer->manipulateEventsOfClass(
85
            'CultuurNet\UDB3\Event\DescriptionTranslated',
86
            function (array $serializedObject) {
87
                $serializedObject['class'] = DescriptionTranslated::class;
88
89
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
90
91
                return $serializedObject;
92
            }
93
        );
94
95
        /*
96
         * LABEL EVENTS
97
         */
98
99
        $payloadManipulatingSerializer->manipulateEventsOfClass(
100
            'CultuurNet\UDB3\Event\Events\EventWasLabelled',
101
            function (array $serializedObject) {
102
                $serializedObject['class'] = LabelAdded::class;
103
104
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
105
106
                return $serializedObject;
107
            }
108
        );
109
110
        $payloadManipulatingSerializer->manipulateEventsOfClass(
111
            'CultuurNet\UDB3\Event\EventWasTagged',
112 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...
113
                $serializedObject['class'] = LabelAdded::class;
114
115
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
116
117
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
118
119
                return $serializedObject;
120
            }
121
        );
122
123
        $payloadManipulatingSerializer->manipulateEventsOfClass(
124
            'CultuurNet\UDB3\Event\TagErased',
125 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...
126
                $serializedObject['class'] = LabelDeleted::class;
127
128
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
129
130
                $serializedObject = self::replaceKeywordWithLabel($serializedObject);
131
132
                return $serializedObject;
133
            }
134
        );
135
136
        $payloadManipulatingSerializer->manipulateEventsOfClass(
137
            'CultuurNet\UDB3\Event\Events\Unlabelled',
138
            function (array $serializedObject) {
139
                $serializedObject['class'] = LabelDeleted::class;
140
141
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
142
143
                return $serializedObject;
144
            }
145
        );
146
147
        $payloadManipulatingSerializer->manipulateEventsOfClass(
148
            'CultuurNet\UDB3\Event\Events\LabelsApplied',
149
            function (array $serializedObject) {
150
                $serializedObject['class'] = LabelsMerged::class;
151
152
                $keywordsString = $serializedObject['payload']['keywords_string'];
153
154
                $query = array();
155
                parse_str($keywordsString, $query);
156
157
                $keywords = explode(';', $query['keywords']);
158
                $visibles = explode(';', $query['visibles']);
159
160
                $labelsArray = array();
161
162
                foreach ($keywords as $key => $keyword) {
163
                    $visible = 'true' === $visibles[$key];
164
                    $labelsArray[] = new Label(
165
                        $keyword,
166
                        $visible
167
                    );
168
                }
169
170
                $labels = array_map(
171
                    function (Label $label) {
172
                        return [
173
                            'text' => (string) $label,
174
                            'visible' => $label->isVisible(),
175
                        ];
176
                    },
177
                    $labelsArray
178
                );
179
180
                $serializedObject['payload']['labels'] = $labels;
181
                unset($serializedObject['payload']['keywords_string']);
182
183
                return $serializedObject;
184
            }
185
        );
186
187
        /**
188
         * UBD2 IMPORT
189
         */
190
191
        $payloadManipulatingSerializer->manipulateEventsOfClass(
192
            'CultuurNet\UDB3\Event\EventImportedFromUDB2',
193
            function (array $serializedObject) {
194
                $serializedObject['class'] = EventImportedFromUDB2::class;
195
196
                return $serializedObject;
197
            }
198
        );
199
200
        /**
201
         * BOOKING INFO
202
         */
203
204
        $payloadManipulatingSerializer->manipulateEventsOfClass(
205
            'CultuurNet\UDB3\Event\Events\BookingInfoUpdated',
206
            function (array $serializedObject) {
207
208
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
209
210
                return $serializedObject;
211
            }
212
        );
213
214
        /**
215
         * BOOKING INFO
216
         */
217
218
        $payloadManipulatingSerializer->manipulateEventsOfClass(
219
            'CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted',
220
            function (array $serializedObject) {
221
222
                $serializedObject = self::replaceEventIdWithItemId($serializedObject);
223
224
                return $serializedObject;
225
            }
226
        );
227
228
        return $payloadManipulatingSerializer;
229
    }
230
231
    /**
232
     * @param array $serializedObject
233
     * @return array
234
     */
235
    private static function replaceEventIdWithItemId(array $serializedObject)
236
    {
237
        $eventId = $serializedObject['payload']['event_id'];
238
        $serializedObject['payload']['item_id'] = $eventId;
239
        unset($serializedObject['payload']['event_id']);
240
241
        return $serializedObject;
242
    }
243
244
    /**
245
     * @param array $serializedObject
246
     * @return array
247
     */
248
    private static function replaceKeywordWithLabel(array $serializedObject)
249
    {
250
        $keyword = $serializedObject['payload']['keyword'];
251
        $serializedObject['payload']['label'] = $keyword;
252
        unset($serializedObject['payload']['keyword']);
253
254
        return $serializedObject;
255
    }
256
}
257