Schedule: Simplify & cleanup data classes

This commit is contained in:
Igor Scheller 2021-12-28 14:43:23 +01:00 committed by xuwhite
parent e3e0fb33a2
commit b63eb44b39
11 changed files with 53 additions and 89 deletions

View File

@ -498,13 +498,13 @@ class ScheduleController extends BaseController
$locations = $this->getAllLocations();
$eventTimeZone = Carbon::now()->timezone;
foreach ($schedule->getDay() as $day) {
foreach ($day->getRoom() as $room) {
foreach ($schedule->getDays() as $day) {
foreach ($day->getRooms() as $room) {
if (!$scheduleModel->activeLocations->where('name', $room->getName())->count()) {
continue;
}
foreach ($room->getEvent() as $event) {
foreach ($room->getEvents() as $event) {
$scheduleEvents[$event->getGuid()] = $event;
$event->getDate()->timezone($eventTimeZone)->subMinutes($minutesBefore);

View File

@ -8,9 +8,6 @@ class Conference
{
use CalculatesTime;
/**
* Event constructor.
*/
public function __construct(
protected string $title,
protected string $acronym,

View File

@ -8,38 +8,16 @@ use Carbon\Carbon;
class Day
{
/** @var string required */
protected string $date;
/** @var Carbon required */
protected Carbon $start;
/** @var Carbon required */
protected Carbon $end;
/** @var int required */
protected int $index;
/** @var Room[] */
protected array $room;
/**
* Day constructor.
*
* @param Room[] $rooms
*/
public function __construct(
string $date,
Carbon $start,
Carbon $end,
int $index,
array $rooms = []
protected string $date,
protected Carbon $start,
protected Carbon $end,
protected int $index,
protected array $rooms = []
) {
$this->date = $date;
$this->start = $start;
$this->end = $end;
$this->index = $index;
$this->room = $rooms;
}
public function getDate(): string
@ -65,8 +43,8 @@ class Day
/**
* @return Room[]
*/
public function getRoom(): array
public function getRooms(): array
{
return $this->room;
return $this->rooms;
}
}

View File

@ -20,7 +20,7 @@ class Event
* @param string $duration (h?h:mm:ss || h?h:mm)
* @param string $slug globally unique
* @param string[] $persons id => name
* @param string|null $language two letter code
* @param string|null $language two-letter code
* @param string|null $recording license (and opt out in XML, null if not recorded, empty if no license defined)/
* @param array $links href => title
* @param array $attachments href => title

View File

@ -6,23 +6,13 @@ namespace Engelsystem\Helpers\Schedule;
class Room
{
/** @var string required */
protected string $name;
/** @var Event[] */
protected array $event;
/**
* Room constructor.
*
* @param Event[] $events
*/
public function __construct(
string $name,
array $events = []
protected string $name,
protected array $events = []
) {
$this->name = $name;
$this->event = $events;
}
public function getName(): string
@ -33,16 +23,16 @@ class Room
/**
* @return Event[]
*/
public function getEvent(): array
public function getEvents(): array
{
return $this->event;
return $this->events;
}
/**
* @param Event[] $event
* @param Event[] $events
*/
public function setEvent(array $event): void
public function setEvents(array $events): void
{
$this->event = $event;
$this->events = $events;
}
}

View File

@ -8,18 +8,14 @@ use Carbon\Carbon;
class Schedule
{
/** @var Day[] */
protected array $day;
/**
* @param Day[] $days
*/
public function __construct(
protected string $version,
protected Conference $conference,
array $days
protected array $days
) {
$this->day = $days;
}
public function getVersion(): string
@ -35,9 +31,9 @@ class Schedule
/**
* @return Day[]
*/
public function getDay(): array
public function getDays(): array
{
return $this->day;
return $this->days;
}
/**
@ -46,8 +42,8 @@ class Schedule
public function getRooms(): array
{
$rooms = [];
foreach ($this->day as $day) {
foreach ($day->getRoom() as $room) {
foreach ($this->days as $day) {
foreach ($day->getRooms() as $room) {
$name = $room->getName();
$rooms[$name] = $room;
}
@ -60,7 +56,7 @@ class Schedule
public function getStartDateTime(): ?Carbon
{
$start = null;
foreach ($this->day as $day) {
foreach ($this->days as $day) {
$time = $day->getStart();
if ($time > $start && $start) {
continue;
@ -75,7 +71,7 @@ class Schedule
public function getEndDateTime(): ?Carbon
{
$end = null;
foreach ($this->day as $day) {
foreach ($this->days as $day) {
$time = $day->getEnd();
if ($time < $end && $end) {
continue;

View File

@ -28,7 +28,9 @@ class XmlParser
/**
* Parse the predefined XML content
*
* According to https://github.com/voc/voctosched/blob/master/schema/basic.xsd
* See also https://c3voc.de/wiki/schedule
*
* According to https://github.com/voc/schedule/blob/master/validator/xsd/schedule.xml.xsd
*/
protected function parseXml(): void
{
@ -53,7 +55,7 @@ class XmlParser
);
$events = $this->parseEvents($roomElement->xpath('event'), $room);
$room->setEvent($events);
$room->setEvents($events);
$rooms[] = $room;
}

View File

@ -17,7 +17,7 @@ class DayTest extends TestCase
* @covers \Engelsystem\Helpers\Schedule\Day::getStart
* @covers \Engelsystem\Helpers\Schedule\Day::getEnd
* @covers \Engelsystem\Helpers\Schedule\Day::getIndex
* @covers \Engelsystem\Helpers\Schedule\Day::getRoom
* @covers \Engelsystem\Helpers\Schedule\Day::getRooms
*/
public function testCreate(): void
{
@ -31,7 +31,7 @@ class DayTest extends TestCase
$this->assertEquals('2000-01-01T03:00:00+01:00', $day->getStart()->format(Carbon::RFC3339));
$this->assertEquals('2000-01-02T05:59:00+00:00', $day->getEnd()->format(Carbon::RFC3339));
$this->assertEquals(1, $day->getIndex());
$this->assertEquals([], $day->getRoom());
$this->assertEquals([], $day->getRooms());
$rooms = [
new Room('Foo'),
@ -44,6 +44,6 @@ class DayTest extends TestCase
1,
$rooms
);
$this->assertEquals($rooms, $day->getRoom());
$this->assertEquals($rooms, $day->getRooms());
}
}

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Test\Unit\Helpers\Schedule;
use Engelsystem\Helpers\Schedule\Event;
use Engelsystem\Helpers\Schedule\Room;
use Engelsystem\Helpers\Uuid;
use Engelsystem\Test\Unit\TestCase;
class RoomTest extends TestCase
@ -13,21 +14,21 @@ class RoomTest extends TestCase
/**
* @covers \Engelsystem\Helpers\Schedule\Room::__construct
* @covers \Engelsystem\Helpers\Schedule\Room::getName
* @covers \Engelsystem\Helpers\Schedule\Room::getEvent
* @covers \Engelsystem\Helpers\Schedule\Room::setEvent
* @covers \Engelsystem\Helpers\Schedule\Room::getEvents
* @covers \Engelsystem\Helpers\Schedule\Room::setEvents
*/
public function testCreate(): void
{
$room = new Room('Test');
$this->assertEquals('Test', $room->getName());
$this->assertEquals([], $room->getEvent());
$this->assertEquals([], $room->getEvents());
$events = [$this->createMock(Event::class), $this->createMock(Event::class)];
$events2 = [$this->createMock(Event::class)];
$room = new Room('Test2', $events);
$this->assertEquals($events, $room->getEvent());
$this->assertEquals($events, $room->getEvents());
$room->setEvent($events2);
$this->assertEquals($events2, $room->getEvent());
$room->setEvents($events2);
$this->assertEquals($events2, $room->getEvents());
}
}

View File

@ -20,7 +20,7 @@ class ScheduleTest extends TestCase
* @covers \Engelsystem\Helpers\Schedule\Schedule::__construct
* @covers \Engelsystem\Helpers\Schedule\Schedule::getVersion
* @covers \Engelsystem\Helpers\Schedule\Schedule::getConference
* @covers \Engelsystem\Helpers\Schedule\Schedule::getDay
* @covers \Engelsystem\Helpers\Schedule\Schedule::getDays
*/
public function testCreate(): void
{
@ -30,7 +30,7 @@ class ScheduleTest extends TestCase
$this->assertEquals('Foo\'ing stuff 1.0', $schedule->getVersion());
$this->assertEquals($conference, $schedule->getConference());
$this->assertEquals($days, $schedule->getDay());
$this->assertEquals($days, $schedule->getDays());
}
/**

View File

@ -46,14 +46,14 @@ class XmlParserTest extends TestCase
$this->assertEquals('Rooming', $room->getName());
/** @var Day $day */
$day = Arr::first($schedule->getDay());
$day = Arr::first($schedule->getDays());
$this->assertEquals('2042-01-01', $day->getDate());
$this->assertEquals(1, $day->getIndex());
/** @var Room $room */
$room = Arr::first($day->getRoom());
$room = Arr::first($day->getRooms());
/** @var Event $event */
$event = Arr::first($room->getEvent());
$event = Arr::first($room->getEvents());
$this->assertEquals('Foo Bar Test', $event->getTitle());
$this->assertEquals('WTFPL', $event->getRecording());