| 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)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.