Completed
Pull Request — master (#262)
by Luc
05:26
created

CdbXmlContactInfoImporter::importBookingInfo()   F

Complexity

Conditions 14
Paths 1836

Size

Total Lines 62
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 2.9107
c 0
b 0
f 0
cc 14
eloc 37
nc 1836
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CultuurNet\UDB3\Offer\ReadModel\JSONLD;
4
5
class CdbXmlContactInfoImporter implements CdbXmlContactInfoImporterInterface
6
{
7
    /**
8
     * @inheritdoc
9
     */
10
    public function importBookingInfo(
11
        \stdClass $jsonLD,
12
        \CultureFeed_Cdb_Data_ContactInfo $contactInfo,
13
        \CultureFeed_Cdb_Data_Price $price = null,
14
        \CultureFeed_Cdb_Data_Calendar_BookingPeriod $bookingPeriod = null
15
    ) {
16
        $bookingInfo = array();
17
18
        if ($price) {
19
            if ($price->getDescription()) {
20
                $bookingInfo['description'] = $price->getDescription();
21
            }
22
            if ($price->getTitle()) {
23
                $bookingInfo['name'] = $price->getTitle();
24
            }
25
            if ($price->getValue() !== null) {
26
                $bookingInfo['priceCurrency'] = 'EUR';
27
                $bookingInfo['price'] = floatval($price->getValue());
28
            }
29
            if ($bookingPeriod) {
30
                $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom());
31
                $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill());
32
33
                $bookingInfo['availabilityStarts'] = $startDate->format('c');
34
                $bookingInfo['availabilityEnds'] = $endDate->format('c');
35
            }
36
        }
37
38
        // Add reservation contact data.
39
        /** @var \CultureFeed_Cdb_Data_Url[] $urls */
40
        $urls = $contactInfo->getUrls();
41
        foreach ($urls as $url) {
42
            if ($url->isForReservations()) {
43
                $bookingInfo['url'] = $url->getUrl();
44
                break;
45
            }
46
        }
47
48
        if (array_key_exists('url', $bookingInfo)) {
49
            $bookingInfo['urlLabel'] = 'Reserveer plaatsen';
50
        }
51
52
        /** @var \CultureFeed_Cdb_Data_Phone[] $phones */
53
        $phones = $contactInfo->getPhones();
54
        foreach ($phones as $phone) {
55
            if ($phone->isForReservations()) {
56
                $bookingInfo['phone'] = $phone->getNumber();
57
                break;
58
            }
59
        }
60
61
        foreach ($contactInfo->getMails() as $mail) {
62
            if ($mail->isForReservations()) {
63
                $bookingInfo['email'] = $mail->getMailAddress();
64
                break;
65
            }
66
        }
67
68
        if (!empty($bookingInfo)) {
69
            $jsonLD->bookingInfo = $bookingInfo;
70
        }
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function importContactPoint(
77
        \stdClass $jsonLD,
78
        \CultureFeed_Cdb_Data_ContactInfo $contactInfo
79
    ) {
80
        $notForReservations = function ($item) {
81
            /** @var \CultureFeed_Cdb_Data_Url|\CultureFeed_Cdb_Data_Phone|\CultureFeed_Cdb_Data_Mail $item */
82
            return !$item->isForReservations();
83
        };
84
85
        $contactPoint = array();
86
87
        $emails = array_filter($contactInfo->getMails(), $notForReservations);
88
89 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...
90
            $contactPoint['email'] = array_map(
91
                function (\CultureFeed_Cdb_Data_Mail $email) {
92
                    return $email->getMailAddress();
93
                },
94
                $emails
95
            );
96
            $contactPoint['email'] = array_values($contactPoint['email']);
97
        }
98
99
        $phones = array_filter($contactInfo->getPhones(), $notForReservations);
100
101 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...
102
            $contactPoint['phone'] = array_map(
103
                function (\CultureFeed_Cdb_Data_phone $phone) {
104
                    return $phone->getNumber();
105
                },
106
                $phones
107
            );
108
            $contactPoint['phone'] = array_values($contactPoint['phone']);
109
        }
110
111
        $urls = array_filter($contactInfo->getUrls(), $notForReservations);
112
113 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...
114
            $contactPoint['url'] = array_map(
115
                function (\CultureFeed_Cdb_Data_Url $url) {
116
                    return $url->getUrl();
117
                },
118
                $urls
119
            );
120
            $contactPoint['url'] = array_values($contactPoint['url']);
121
        }
122
123
        array_filter($contactPoint);
124
        if (!empty($contactPoint)) {
125
            $jsonLD->contactPoint = $contactPoint;
126
        }
127
    }
128
129
    /**
130
     * @param int $unixTime
131
     * @return \DateTime
132
     */
133
    private function dateFromUdb2UnixTime($unixTime)
134
    {
135
        $dateTime = new \DateTime(
136
            '@' . $unixTime,
137
            new \DateTimeZone('Europe/Brussels')
138
        );
139
140
        return $dateTime;
141
    }
142
}
143