Completed
Pull Request — master (#243)
by Luc
05:28
created

CdbXMLImporter   F

Complexity

Total Complexity 83

Size/Duplication

Total Lines 618
Duplicated Lines 12.3 %

Coupling/Cohesion

Components 1
Dependencies 20

Importance

Changes 0
Metric Value
wmc 83
lcom 1
cbo 20
dl 76
loc 618
rs 1.5221
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A documentWithCdbXML() 0 72 3
A addDescriptionFilter() 0 4 1
A dateFromUdb2UnixTime() 0 9 1
A importDescription() 0 15 2
D importPicture() 39 39 9
B importLocation() 0 26 3
D importOrganizer() 0 39 9
C importPriceInfo() 0 47 9
F importBookingInfo() 0 65 15
B importContactPoint() 27 56 6
A importTerms() 10 20 4
A importTypicalAgeRange() 0 7 2
A importPerformers() 0 14 4
A importLanguages() 0 12 3
B importSeeAlso() 0 21 7
B importUitInVlaanderenReference() 0 27 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CdbXMLImporter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CdbXMLImporter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use CultureFeed_Cdb_Data_File;
6
use CultuurNet\UDB3\CalendarFactoryInterface;
7
use CultuurNet\UDB3\Cdb\CdbId\EventCdbIdExtractorInterface;
8
use CultuurNet\UDB3\Cdb\PriceDescriptionParser;
9
use CultuurNet\UDB3\LabelImporter;
10
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\CdbXMLItemBaseImporter;
11
use CultuurNet\UDB3\SluggerInterface;
12
use CultuurNet\UDB3\StringFilter\StringFilterInterface;
13
14
/**
15
 * Takes care of importing cultural events in the CdbXML format (UDB2)
16
 * into a UDB3 JSON-LD document.
17
 */
18
class CdbXMLImporter
19
{
20
    /**
21
     * @var CdbXMLItemBaseImporter
22
     */
23
    private $cdbXMLItemBaseImporter;
24
25
    /**
26
     * @var EventCdbIdExtractorInterface
27
     */
28
    private $cdbIdExtractor;
29
30
    /**
31
     * @var PriceDescriptionParser
32
     */
33
    private $priceDescriptionParser;
34
35
    /**
36
     * @var CalendarFactoryInterface
37
     */
38
    private $calendarFactory;
39
40
    /**
41
     * @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter
42
     * @param EventCdbIdExtractorInterface $cdbIdExtractor
43
     * @param PriceDescriptionParser $priceDescriptionParser
44
     * @param CalendarFactoryInterface $calendarFactory
45
     */
46
    public function __construct(
47
        CdbXMLItemBaseImporter $dbXMLItemBaseImporter,
48
        EventCdbIdExtractorInterface $cdbIdExtractor,
49
        PriceDescriptionParser $priceDescriptionParser,
50
        CalendarFactoryInterface $calendarFactory
51
    ) {
52
        $this->cdbXMLItemBaseImporter = $dbXMLItemBaseImporter;
53
        $this->cdbIdExtractor = $cdbIdExtractor;
54
        $this->priceDescriptionParser = $priceDescriptionParser;
55
        $this->calendarFactory = $calendarFactory;
56
    }
57
58
    /**
59
     * @var StringFilterInterface[]
60
     */
61
    private $descriptionFilters = [];
62
63
    /**
64
     * Imports a UDB2 event into a UDB3 JSON-LD document.
65
     *
66
     * @param \stdClass $base
67
     *   The JSON-LD document to start from.
68
     * @param \CultureFeed_Cdb_Item_Event $event
69
     *   The cultural event data from UDB2 to import.
70
     * @param PlaceServiceInterface $placeManager
71
     *   The manager from which to retrieve the JSON-LD of a place.
72
     * @param OrganizerServiceInterface $organizerManager
73
     *   The manager from which to retrieve the JSON-LD of an organizer.
74
     * @param SluggerInterface $slugger
75
     *   The slugger that's used to generate a sameAs reference.
76
     *
77
     * @return \stdClass
78
     *   The document with the UDB2 event data merged in.
79
     */
80
    public function documentWithCdbXML(
81
        $base,
82
        \CultureFeed_Cdb_Item_Event $event,
83
        PlaceServiceInterface $placeManager,
84
        OrganizerServiceInterface $organizerManager,
85
        SluggerInterface $slugger
86
    ) {
87
        $jsonLD = clone $base;
88
89
        /** @var \CultureFeed_Cdb_Data_EventDetail $detail */
90
        $detail = null;
91
92
        /** @var \CultureFeed_Cdb_Data_EventDetail[] $details */
93
        $details = $event->getDetails();
94
95
        foreach ($details as $languageDetail) {
96
            $language = $languageDetail->getLanguage();
97
98
            // The first language detail found will be used to retrieve
99
            // properties from which in UDB3 are not any longer considered
100
            // to be language specific.
101
            if (!$detail) {
102
                $detail = $languageDetail;
103
            }
104
105
            $jsonLD->name[$language] = $languageDetail->getTitle();
106
107
            $this->importDescription($languageDetail, $jsonLD, $language);
108
        }
109
110
        $this->cdbXMLItemBaseImporter->importAvailable($event, $jsonLD);
111
112
        $this->importPicture($detail, $jsonLD);
113
114
        $labelImporter = new LabelImporter();
115
        $labelImporter->importLabels($event, $jsonLD);
116
117
        $jsonLD->calendarSummary = $detail->getCalendarSummary();
118
119
        $this->importLocation($event, $placeManager, $jsonLD);
120
121
        $this->importOrganizer($event, $organizerManager, $jsonLD);
122
123
        $this->importBookingInfo($event, $detail, $jsonLD);
124
125
        $this->importPriceInfo($detail, $jsonLD);
126
127
        $this->importTerms($event, $jsonLD);
128
129
        $this->cdbXMLItemBaseImporter->importPublicationInfo($event, $jsonLD);
130
131
        $calendar = $this->calendarFactory->createFromCdbCalendar($event->getCalendar());
132
        $jsonLD = (object)array_merge((array)$jsonLD, $calendar->toJsonLd());
133
134
        $this->importTypicalAgeRange($event, $jsonLD);
135
136
        $this->importPerformers($detail, $jsonLD);
137
138
        $this->importLanguages($event, $jsonLD);
139
140
        $this->importUitInVlaanderenReference($event, $slugger, $jsonLD);
141
142
        $this->cdbXMLItemBaseImporter->importExternalId($event, $jsonLD);
143
144
        $this->importSeeAlso($event, $jsonLD);
145
146
        $this->importContactPoint($event, $jsonLD);
147
148
        $this->cdbXMLItemBaseImporter->importWorkflowStatus($event, $jsonLD);
149
150
        return $jsonLD;
151
    }
152
153
    /**
154
     * @param StringFilterInterface $filter
155
     */
156
    public function addDescriptionFilter(StringFilterInterface $filter)
157
    {
158
        $this->descriptionFilters[] = $filter;
159
    }
160
161
    /**
162
     * @param int $unixTime
163
     * @return \DateTime
164
     */
165
    private function dateFromUdb2UnixTime($unixTime)
166
    {
167
        $dateTime = new \DateTime(
168
            '@' . $unixTime,
169
            new \DateTimeZone('Europe/Brussels')
170
        );
171
172
        return $dateTime;
173
    }
174
175
    /**
176
     * @param \CultureFeed_Cdb_Data_EventDetail $languageDetail
177
     * @param \stdClass $jsonLD
178
     * @param string $language
179
     */
180
    private function importDescription($languageDetail, $jsonLD, $language)
181
    {
182
        $descriptions = [
183
            $languageDetail->getShortDescription(),
184
            $languageDetail->getLongDescription()
185
        ];
186
        $descriptions = array_filter($descriptions);
187
        $description = implode('<br/>', $descriptions);
188
189
        foreach ($this->descriptionFilters as $descriptionFilter) {
190
            $description = $descriptionFilter->filter($description);
191
        };
192
193
        $jsonLD->description[$language] = $description;
194
    }
195
196
    /**
197
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
198
     * @param \stdClass $jsonLD
199
     *
200
     * This is based on code found in the culturefeed theme.
201
     * @see https://github.com/cultuurnet/culturefeed/blob/master/culturefeed_agenda/theme/theme.inc#L266-L284
202
     */
203 View Code Duplication
    private function importPicture($detail, $jsonLD)
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...
204
    {
205
        $mainPicture = null;
206
207
        // first check if there is a media file that is main and has the PHOTO media type
208
        $photos = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO);
209
        foreach ($photos as $photo) {
210
            if ($photo->isMain()) {
211
                $mainPicture = $photo;
212
            }
213
        }
214
215
        // the IMAGEWEB media type is deprecated but can still be used as a main image if there is no PHOTO
216
        if (empty($mainPicture)) {
217
            $images = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB);
218
            foreach ($images as $image) {
219
                if ($image->isMain()) {
220
                    $mainPicture = $image;
221
                }
222
            }
223
        }
224
225
        // if there is no explicit main image we just use the oldest picture of any type
226
        if (empty($mainPicture)) {
227
            $pictures = $detail->getMedia()->byMediaTypes(
228
                [
229
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO,
230
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB
231
                ]
232
            );
233
234
            $pictures->rewind();
235
            $mainPicture = count($pictures) > 0 ? $pictures->current() : null;
236
        }
237
238
        if ($mainPicture) {
239
            $jsonLD->image = $mainPicture->getHLink();
240
        }
241
    }
242
243
    /**
244
     * @param \CultureFeed_Cdb_Item_Event $event
245
     * @param PlaceServiceInterface $placeManager
246
     * @param \stdClass $jsonLD
247
     */
248
    private function importLocation(\CultureFeed_Cdb_Item_Event $event, PlaceServiceInterface $placeManager, $jsonLD)
249
    {
250
        $location = array();
251
        $location['@type'] = 'Place';
252
253
        $location_id = $this->cdbIdExtractor->getRelatedPlaceCdbId($event);
254
255
        if ($location_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location_id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
256
            $location += (array)$placeManager->placeJSONLD($location_id);
257
        } else {
258
            $location_cdb = $event->getLocation();
259
            $location['name']['nl'] = $location_cdb->getLabel();
260
            $address = $location_cdb->getAddress()->getPhysicalAddress();
261
            if ($address) {
262
                $location['address'] = array(
263
                    'addressCountry' => $address->getCountry(),
264
                    'addressLocality' => $address->getCity(),
265
                    'postalCode' => $address->getZip(),
266
                    'streetAddress' =>
267
                        $address->getStreet() . ' ' . $address->getHouseNumber(
268
                        ),
269
                );
270
            }
271
        }
272
        $jsonLD->location = $location;
273
    }
274
275
    /**
276
     * @param \CultureFeed_Cdb_Item_Event $event
277
     * @param OrganizerServiceInterface $organizerManager
278
     * @param \stdClass $jsonLD
279
     */
280
    private function importOrganizer(
281
        \CultureFeed_Cdb_Item_Event $event,
282
        OrganizerServiceInterface $organizerManager,
283
        $jsonLD
284
    ) {
285
        $organizer = null;
286
        $organizer_id = $this->cdbIdExtractor->getRelatedOrganizerCdbId($event);
287
        $organizer_cdb = $event->getOrganiser();
288
        $contact_info_cdb = $event->getContactInfo();
289
290
        if ($organizer_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $organizer_id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
291
            $organizer = (array)$organizerManager->organizerJSONLD($organizer_id);
292
        } elseif ($organizer_cdb && $contact_info_cdb) {
293
            $organizer = array();
294
            $organizer['name'] = $organizer_cdb->getLabel();
295
296
            $emails_cdb = $contact_info_cdb->getMails();
297
            if (count($emails_cdb) > 0) {
298
                $organizer['email'] = array();
299
                foreach ($emails_cdb as $email) {
300
                    $organizer['email'][] = $email->getMailAddress();
301
                }
302
            }
303
304
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones_cdb */
305
            $phones_cdb = $contact_info_cdb->getPhones();
306
            if (count($phones_cdb) > 0) {
307
                $organizer['phone'] = array();
308
                foreach ($phones_cdb as $phone) {
309
                    $organizer['phone'][] = $phone->getNumber();
310
                }
311
            }
312
        }
313
314
        if (!is_null($organizer)) {
315
            $organizer['@type'] = 'Organizer';
316
            $jsonLD->organizer = $organizer;
317
        }
318
    }
319
320
    /**
321
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
322
     * @param \stdClass $jsonLD
323
     */
324
    private function importPriceInfo(
325
        \CultureFeed_Cdb_Data_EventDetail $detail,
326
        $jsonLD
327
    ) {
328
        $prices = array();
329
330
        $price = $detail->getPrice();
331
332
        if ($price) {
333
            $description = $price->getDescription();
334
335
            if ($description) {
336
                $prices = $this->priceDescriptionParser->parse($description);
337
            }
338
339
            // If price description was not interpretable, fall back to
340
            // price title and value.
341
            if (empty($prices) && $price->getValue() !== null) {
342
                $prices['Basistarief'] = floatval($price->getValue());
343
            }
344
        }
345
346
        if (!empty($prices)) {
347
            $priceInfo = array();
348
349
            /** @var \CultureFeed_Cdb_Data_Price $price */
350
            foreach ($prices as $title => $value) {
351
                $priceInfoItem = array(
352
                    'name' => $title,
353
                    'priceCurrency' => 'EUR',
354
                    'price' => $value,
355
                );
356
357
                $priceInfoItem['category'] = 'tariff';
358
359
                if ($priceInfoItem['name'] === 'Basistarief') {
360
                    $priceInfoItem['category'] = 'base';
361
                }
362
363
                $priceInfo[] = $priceInfoItem;
364
            }
365
366
            if (!empty($priceInfo)) {
367
                $jsonLD->priceInfo = $priceInfo;
368
            }
369
        }
370
    }
371
372
    /**
373
     * @param \CultureFeed_Cdb_Item_Event $event
374
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
375
     * @param \stdClass $jsonLD
376
     */
377
    private function importBookingInfo(
378
        \CultureFeed_Cdb_Item_Event $event,
379
        \CultureFeed_Cdb_Data_EventDetail $detail,
380
        $jsonLD
381
    ) {
382
        $bookingInfo = array();
383
384
        $price = $detail->getPrice();
385
        if ($price) {
386
            if ($price->getDescription()) {
387
                $bookingInfo['description'] = $price->getDescription();
388
            }
389
            if ($price->getTitle()) {
390
                $bookingInfo['name'] = $price->getTitle();
391
            }
392
            if ($price->getValue() !== null) {
393
                $bookingInfo['priceCurrency'] = 'EUR';
394
                $bookingInfo['price'] = floatval($price->getValue());
395
            }
396
            if ($bookingPeriod = $event->getBookingPeriod()) {
397
                $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom());
398
                $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill());
399
400
                $bookingInfo['availabilityStarts'] = $startDate->format('c');
401
                $bookingInfo['availabilityEnds'] = $endDate->format('c');
402
            }
403
        }
404
405
        // Add reservation contact data.
406
        $contactInfo = $event->getContactInfo();
407
        if ($contactInfo) {
408
            /** @var \CultureFeed_Cdb_Data_Url[] $urls */
409
            $urls = $contactInfo->getUrls();
410
            foreach ($urls as $url) {
411
                if ($url->isForReservations()) {
412
                    $bookingInfo['url'] = $url->getUrl();
413
                    break;
414
                }
415
            }
416
417
            if (array_key_exists('url', $bookingInfo)) {
418
                $bookingInfo['urlLabel'] = 'Reserveer plaatsen';
419
            }
420
421
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones */
422
            $phones = $contactInfo->getPhones();
423
            foreach ($phones as $phone) {
424
                if ($phone->isForReservations()) {
425
                    $bookingInfo['phone'] = $phone->getNumber();
426
                    break;
427
                }
428
            }
429
430
            foreach ($contactInfo->getMails() as $mail) {
431
                if ($mail->isForReservations()) {
432
                    $bookingInfo['email'] = $mail->getMailAddress();
433
                    break;
434
                }
435
            }
436
        }
437
438
        if (!empty($bookingInfo)) {
439
            $jsonLD->bookingInfo = $bookingInfo;
440
        }
441
    }
442
443
    /**
444
     * @param \CultureFeed_Cdb_Item_Event $event
445
     * @param \stdClass $jsonLD
446
     */
447
    private function importContactPoint(
448
        \CultureFeed_Cdb_Item_Event $event,
449
        \stdClass $jsonLD
450
    ) {
451
        $contactInfo = $event->getContactInfo();
452
453
        $notForReservations = function ($item) {
454
            /** @var \CultureFeed_Cdb_Data_Url|\CultureFeed_Cdb_Data_Phone|\CultureFeed_Cdb_Data_Mail $item */
455
            return !$item->isForReservations();
456
        };
457
458
        if ($contactInfo) {
459
            $contactPoint = array();
460
461
            $emails = array_filter($contactInfo->getMails(), $notForReservations);
462
463 View Code Duplication
            if (!empty($emails)) {
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...
464
                $contactPoint['email'] = array_map(
465
                    function (\CultureFeed_Cdb_Data_Mail $email) {
466
                        return $email->getMailAddress();
467
                    },
468
                    $emails
469
                );
470
                $contactPoint['email'] = array_values($contactPoint['email']);
471
            }
472
473
            $phones = array_filter($contactInfo->getPhones(), $notForReservations);
474
475 View Code Duplication
            if (!empty($phones)) {
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...
476
                $contactPoint['phone'] = array_map(
477
                    function (\CultureFeed_Cdb_Data_phone $phone) {
478
                        return $phone->getNumber();
479
                    },
480
                    $phones
481
                );
482
                $contactPoint['phone'] = array_values($contactPoint['phone']);
483
            }
484
485
            $urls = array_filter($contactInfo->getUrls(), $notForReservations);
486
487 View Code Duplication
            if (!empty($urls)) {
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...
488
                $contactPoint['url'] = array_map(
489
                    function (\CultureFeed_Cdb_Data_Url $url) {
490
                        return $url->getUrl();
491
                    },
492
                    $urls
493
                );
494
                $contactPoint['url'] = array_values($contactPoint['url']);
495
            }
496
497
            array_filter($contactPoint);
498
            if (!empty($contactPoint)) {
499
                $jsonLD->contactPoint = $contactPoint;
500
            }
501
        }
502
    }
503
504
    /**
505
     * @param \CultureFeed_Cdb_Item_Event $event
506
     * @param \stdClass $jsonLD
507
     */
508
    private function importTerms(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
509
    {
510
        $themeBlacklist = [
511
            'Thema onbepaald',
512
            'Meerder kunstvormen',
513
            'Meerdere filmgenres'
514
        ];
515
        $categories = array();
516 View Code Duplication
        foreach ($event->getCategories() as $category) {
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...
517
            /* @var \Culturefeed_Cdb_Data_Category $category */
518
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
519
                $categories[] = array(
520
                    'label' => $category->getName(),
521
                    'domain' => $category->getType(),
522
                    'id' => $category->getId(),
523
                );
524
            }
525
        }
526
        $jsonLD->terms = $categories;
527
    }
528
529
    /**
530
     * @param \CultureFeed_Cdb_Item_Event $event
531
     * @param \stdClass $jsonLD
532
     */
533
    private function importTypicalAgeRange(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
534
    {
535
        $ageFrom = $event->getAgeFrom();
536
        if ($ageFrom) {
537
            $jsonLD->typicalAgeRange = "{$ageFrom}-";
538
        }
539
    }
540
541
    /**
542
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
543
     * @param \stdClass $jsonLD
544
     */
545
    private function importPerformers(\CultureFeed_Cdb_Data_EventDetail $detail, $jsonLD)
546
    {
547
        /** @var \CultureFeed_Cdb_Data_Performer $performer */
548
        $performers = $detail->getPerformers();
549
        if ($performers) {
550
            foreach ($performers as $performer) {
551
                if ($performer->getLabel()) {
552
                    $performerData = new \stdClass();
553
                    $performerData->name = $performer->getLabel();
554
                    $jsonLD->performer[] = $performerData;
555
                }
556
            }
557
        }
558
    }
559
560
    /**
561
     * @param \CultureFeed_Cdb_Item_Event $event
562
     * @param \stdClass $jsonLD
563
     */
564
    private function importLanguages(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
565
    {
566
        /** @var \CultureFeed_Cdb_Data_Language $udb2Language */
567
        $languages = $event->getLanguages();
568
        if ($languages) {
569
            $jsonLD->language = [];
570
            foreach ($languages as $udb2Language) {
571
                $jsonLD->language[] = $udb2Language->getLanguage();
572
            }
573
            $jsonLD->language = array_unique($jsonLD->language);
574
        }
575
    }
576
577
    /**
578
     * @param \CultureFeed_Cdb_Item_Event $event
579
     * @param \stdClass $jsonLD
580
     */
581
    private function importSeeAlso(
582
        \CultureFeed_Cdb_Item_Event $event,
583
        \stdClass $jsonLD
584
    ) {
585
        if (!property_exists($jsonLD, 'seeAlso')) {
586
            $jsonLD->seeAlso = [];
587
        }
588
589
        // Add contact info url, if it's not for reservations.
590
        if ($contactInfo = $event->getContactInfo()) {
591
            /** @var \CultureFeed_Cdb_Data_Url[] $contactUrls */
592
            $contactUrls = $contactInfo->getUrls();
593
            if (is_array($contactUrls) && count($contactUrls) > 0) {
594
                foreach ($contactUrls as $contactUrl) {
595
                    if (!$contactUrl->isForReservations()) {
596
                        $jsonLD->seeAlso[] = $contactUrl->getUrl();
597
                    }
598
                }
599
            }
600
        }
601
    }
602
603
    /**
604
     * @param \CultureFeed_Cdb_Item_Event $event
605
     * @param SluggerInterface $slugger
606
     * @param \stdClass $jsonLD
607
     */
608
    private function importUitInVlaanderenReference(
609
        \CultureFeed_Cdb_Item_Event $event,
610
        SluggerInterface $slugger,
611
        $jsonLD
612
    ) {
613
614
        // Some events seem to not have a Dutch name, even though this is
615
        // required. If there's no Dutch name, we just leave the slug empty as
616
        // that seems to be the behaviour on http://m.uitinvlaanderen.be
617
        if (isset($jsonLD->name['nl'])) {
618
            $name = $jsonLD->name['nl'];
619
            $slug = $slugger->slug($name);
620
        } else {
621
            $slug = '';
622
        }
623
624
        $reference = 'http://www.uitinvlaanderen.be/agenda/e/' . $slug . '/' . $event->getCdbId();
625
626
627
        if (!property_exists($jsonLD, 'sameAs')) {
628
            $jsonLD->sameAs = [];
629
        }
630
631
        if (!in_array($reference, $jsonLD->sameAs)) {
632
            array_push($jsonLD->sameAs, $reference);
633
        }
634
    }
635
}
636