Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
III-1325: Refactor utility trait to typed Calendar and update test data
Browse files Browse the repository at this point in the history
  • Loading branch information
bramcordie committed Sep 27, 2016
1 parent 3dda641 commit bf086fe
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 82 deletions.
43 changes: 26 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 67 additions & 64 deletions src/Udb2UtilityTrait.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

/**
* @file
* Contains CultuurNet\UDB3\UDB2\Udb2UtilityTrait.
*/

namespace CultuurNet\UDB3\UDB2;

use Broadway\Domain\DomainMessage;
Expand All @@ -27,12 +22,14 @@
use CultureFeed_Cdb_Item_Event;
use CultuurNet\Auth\ConsumerCredentials;
use CultuurNet\Entry\EntryAPI;
use CultuurNet\UDB3\Address;
use CultuurNet\UDB3\Address\Address;
use CultuurNet\UDB3\BookingInfo;
use CultuurNet\UDB3\Calendar;
use CultuurNet\UDB3\CalendarInterface;
use CultuurNet\UDB3\CalendarType;
use CultuurNet\UDB3\ContactPoint;
use DateTime;
use DateTimeInterface;
use Zend\Validator\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -157,71 +154,77 @@ public function setCalendar(CalendarInterface $eventCalendar, CultureFeed_Cdb_It
}
}

// Multiple days.
if ($eventCalendar->getType() == Calendar::MULTIPLE) {
$calendar = new CultureFeed_Cdb_Data_Calendar_TimestampList();
foreach ($eventCalendar->getTimestamps() as $timestamp) {
$startdate = strtotime($timestamp->getStartDate());
$enddate = strtotime($timestamp->getEndDate());
$startHour = date('H:i:s', $startdate);
if ($startHour == '00:00:00') {
$startHour = null;
}
$endHour = date('H:i:s', $enddate);
if ($endHour == '00:00:00') {
$endHour = null;
switch ($eventCalendar->getType()->toNative()) {
case CalendarType::MULTIPLE:
$calendar = new CultureFeed_Cdb_Data_Calendar_TimestampList();
foreach ($eventCalendar->getTimestamps() as $timestamp) {
$this->timestampCalendar(
$timestamp->getStartDate(),
$timestamp->getEndDate(),
$calendar
);
}
$calendar->add(
new CultureFeed_Cdb_Data_Calendar_Timestamp(
date('Y-m-d', $startdate),
$startHour,
$endHour
)
break;
case CalendarType::SINGLE:
$calendar = $this->timestampCalendar(
$eventCalendar->getStartDate(),
$eventCalendar->getEndDate(),
new CultureFeed_Cdb_Data_Calendar_TimestampList()
);
}
// Single day.
} elseif ($eventCalendar->getType() == Calendar::SINGLE) {
$calendar = new CultureFeed_Cdb_Data_Calendar_TimestampList();
$startdate = strtotime($eventCalendar->getStartDate());
$enddate = strtotime($eventCalendar->getEndDate());
$startHour = date('H:i:s', $startdate);
if ($startHour == '00:00:00') {
$startHour = null;
}
$endHour = date('H:i:s', $enddate);
if ($endHour == '00:00:00') {
$endHour = null;
}
$calendar->add(
new CultureFeed_Cdb_Data_Calendar_Timestamp(
date('Y-m-d', $startdate),
$startHour,
$endHour
)
);
// Period.
} elseif ($eventCalendar->getType() == Calendar::PERIODIC) {
$calendar = new CultureFeed_Cdb_Data_Calendar_PeriodList();
$startdate = date('Y-m-d', strtotime($eventCalendar->getStartDate()));
$enddate = date('Y-m-d', strtotime($eventCalendar->getEndDate()));

$period = new CultureFeed_Cdb_Data_Calendar_Period($startdate, $enddate);
if (!empty($weekScheme)) {
$calendar->setWeekScheme($weekscheme);
}
$calendar->add($period);

// Permanent
} elseif ($eventCalendar->getType() == Calendar::PERMANENT) {
$calendar = new CultureFeed_Cdb_Data_Calendar_Permanent();
if (!empty($weekScheme)) {
$calendar->setWeekScheme($weekscheme);
}
break;
case CalendarType::PERIODIC:
$calendar = new CultureFeed_Cdb_Data_Calendar_PeriodList();
$startDate = $eventCalendar->getStartDate()->format('Y-m-d');
$endDate = $eventCalendar->getEndDate()->format('Y-m-d');

$period = new CultureFeed_Cdb_Data_Calendar_Period($startDate, $endDate);
if (!empty($weekScheme)) {
$period->setWeekScheme($weekscheme);
}
$calendar->add($period);
break;
case CalendarType::PERMANENT:
$calendar = new CultureFeed_Cdb_Data_Calendar_Permanent();
if (!empty($weekScheme)) {
$calendar->setWeekScheme($weekscheme);
}
break;
}

$cdbEvent->setCalendar($calendar);
}

/**
* @param DateTimeInterface $startDate
* @param DateTimeInterface $endDate
* @param CultureFeed_Cdb_Data_Calendar_TimestampList $calendar
*
* @return CultureFeed_Cdb_Data_Calendar_TimestampList
*/
private function timestampCalendar(
DateTimeInterface $startDate,
DateTimeInterface $endDate,
CultureFeed_Cdb_Data_Calendar_TimestampList $calendar
) {
$startHour = $startDate->format('H:i:s');
if ($startHour == '00:00:00') {
$startHour = null;
}
$endHour = $endDate->format('H:i:s');
if ($endHour == '00:00:00') {
$endHour = null;
}
$calendar->add(
new CultureFeed_Cdb_Data_Calendar_Timestamp(
$startDate->format('Y-m-d'),
$startHour,
$endHour
)
);

return $calendar;
}

/**
* Create a physical addres based on a given udb3 address.
* @param Address $address
Expand Down
6 changes: 5 additions & 1 deletion test/EventImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use CultuurNet\UDB3\Address\PostalCode;
use CultuurNet\UDB3\Address\Street;
use CultuurNet\UDB3\Calendar;
use CultuurNet\UDB3\CalendarType;
use CultuurNet\UDB3\Event\Event;
use CultuurNet\UDB3\Event\EventRepository;
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
Expand Down Expand Up @@ -156,7 +157,10 @@ public function it_updates_an_existing_event_with_cdbxml()
Country::fromNative('BE')
)
),
new Calendar('single', '2015-01-26T13:25:21+01:00')
new Calendar(
CalendarType::SINGLE(),
\DateTime::createFromFormat(\DateTime::ATOM, '2015-01-26T13:25:21+01:00')
)
);
$this->repository->save($event);

Expand Down

0 comments on commit bf086fe

Please sign in to comment.