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