Completed
Pull Request — master (#110)
by Kristof
07:14
created

CdbXMLImporter::eventDocumentWithCdbXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 2
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_Base $item
35
     *   The event/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_Base $item
43
    ) {
44
        $jsonLD = clone $base;
45
        
46
        $detail = null;
47
48
        /** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */
49
        $details = $item->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($item, $jsonLD);
72
        $this->cdbXMLItemBaseImporter->importAvailable($item, $jsonLD);
73
        $this->cdbXMLItemBaseImporter->importExternalId($item, $jsonLD);
74
75
        // Address
76
        $contact_cdb = $item->getContactInfo();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CultureFeed_Cdb_Item_Base as the method getContactInfo() does only exist in the following sub-classes of CultureFeed_Cdb_Item_Base: CultureFeed_Cdb_Item_Actor, CultureFeed_Cdb_Item_Event. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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($item, $jsonLD);
125
126
        return $jsonLD;
127
    }
128
129
    public function eventDocumentWithCdbXML(
130
        $base,
131
        \CultureFeed_Cdb_Item_Base $item
132
    ) {
133
        $jsonLD = $this->documentWithCdbXML($base, $item);
134
135
        return $jsonLD;
136
    }
137
138
    /**
139
     * @param \CultureFeed_Cdb_Item_Actor $actor
140
     * @param \stdClass $jsonLD
141
     */
142
    private function importTerms(\CultureFeed_Cdb_Item_Base $actor, $jsonLD)
143
    {
144
        $themeBlacklist = [];
145
        $categories = array();
146 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...
147
            /* @var \Culturefeed_Cdb_Data_Category $category */
148
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
149
                $categories[] = array(
150
                    'label' => $category->getName(),
151
                    'domain' => $category->getType(),
152
                    'id' => $category->getId(),
153
                );
154
            }
155
        }
156
        $jsonLD->terms = $categories;
157
    }
158
}
159