Completed
Pull Request — master (#107)
by Kristof
38:33 queued 15:13
created

CdbXMLImporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\Place\ReadModel\JSONLD;
7
8
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\CdbXMLItemBaseImporter;
9
10
/**
11
 * Takes care of importing actors in the CdbXML format (UDB2) that represent
12
 * a place, into a UDB3 JSON-LD document.
13
 */
14
class CdbXMLImporter
15
{
16
    /**
17
     * @var CdbXMLItemBaseImporter
18
     */
19
    private $cdbXMLItemBaseImporter;
20
21
    /**
22
     * @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter
23
     */
24
    public function __construct(CdbXMLItemBaseImporter $dbXMLItemBaseImporter)
25
    {
26
        $this->cdbXMLItemBaseImporter = $dbXMLItemBaseImporter;
27
    }
28
29
    /**
30
     * Imports a UDB2 organizer actor into a UDB3 JSON-LD document.
31
     *
32
     * @param \stdClass $base
33
     *   The JSON-LD document object to start from.
34
     * @param \CultureFeed_Cdb_Item_Actor $actor
35
     *   The actor data from UDB2 to import.
36
     *
37
     * @return \stdClass
38
     *   A new JSON-LD document object with the UDB2 actor data merged in.
39
     */
40
    public function documentWithCdbXML(
41
        $base,
42
        \CultureFeed_Cdb_Item_Actor $actor
43
    ) {
44
        $jsonLD = clone $base;
45
        
46
        $detail = null;
47
48
        /** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */
49
        $details = $actor->getDetails();
50
51
        foreach ($details as $languageDetail) {
52
            // The first language detail found will be used to retrieve
53
            // properties from which in UDB3 are not any longer considered
54
            // to be language specific.
55
            if (!$detail) {
56
                $detail = $languageDetail;
57
            }
58
        }
59
60
        $descriptions = [
61
            trim($detail->getShortDescription()),
62
            trim($detail->getLongDescription())
63
        ];
64
        $descriptions = array_filter($descriptions);
65
        if (count($descriptions) > 0) {
66
            $jsonLD->description = implode('<br/>', $descriptions);
67
        }
68
69
        $jsonLD->name = $detail->getTitle();
70
71
        $this->cdbXMLItemBaseImporter->importPublicationInfo($actor, $jsonLD);
72
        $this->cdbXMLItemBaseImporter->importAvailable($actor, $jsonLD);
73
        $this->cdbXMLItemBaseImporter->importExternalId($actor, $jsonLD);
74
75
        // Address
76
        $contact_cdb = $actor->getContactInfo();
77
        if ($contact_cdb) {
78
            $addresses = $contact_cdb->getAddresses();
79
80 View Code Duplication
            foreach ($addresses as $address) {
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...
81
                $address = $address->getPhysicalAddress();
82
83
                if ($address) {
84
                    $jsonLD->address = array(
85
                        'addressCountry' => $address->getCountry(),
86
                        'addressLocality' => $address->getCity(),
87
                        'postalCode' => $address->getZip(),
88
                        'streetAddress' =>
89
                            $address->getStreet() . ' ' .
90
                            $address->getHouseNumber(),
91
                    );
92
93
                    break;
94
                }
95
            }
96
        }
97
98
        // Booking info.
99
        $bookingInfo = array(
100
            'description' => '',
101
            'name' => 'standard price',
102
            'price' => 0.0,
103
            'priceCurrency' => 'EUR',
104
        );
105
        $price = $detail->getPrice();
106
107
        if ($price) {
108
            $bookingInfo['description'] = floatval($price->getDescription());
109
            $bookingInfo['name'] = floatval($price->getTitle());
110
            $bookingInfo['price'] = floatval($price->getValue());
111
        }
112
        $jsonLD->bookingInfo = $bookingInfo;
113
114
        // Image.
115
        $images = $detail->getMedia()->byMediaType(
116
            \CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO
117
        );
118
        $images->rewind();
119
        $image = count($images) > 0 ? $images->current() : null;
120
        if ($image) {
121
            $jsonLD->image = $image->getHLink();
122
        }
123
124
        $this->importTerms($actor, $jsonLD);
125
126
        return $jsonLD;
127
    }
128
129
    /**
130
     * @param \CultureFeed_Cdb_Item_Actor $actor
131
     * @param \stdClass $jsonLD
132
     */
133
    private function importTerms(\CultureFeed_Cdb_Item_Actor $actor, $jsonLD)
134
    {
135
        $themeBlacklist = [];
136
        $categories = array();
137 View Code Duplication
        foreach ($actor->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...
138
            /* @var \Culturefeed_Cdb_Data_Category $category */
139
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
140
                $categories[] = array(
141
                    'label' => $category->getName(),
142
                    'domain' => $category->getType(),
143
                    'id' => $category->getId(),
144
                );
145
            }
146
        }
147
        $jsonLD->terms = $categories;
148
    }
149
}
150