Completed
Push — master ( 7e66ea...6a4de9 )
by Olivier
01:44
created

ICSImporter::importFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Calendar;
6
7
use Sabre\VObject;
8
use Shapin\Calendar\Model\Calendar as CalendarModel;
9
use Shapin\Calendar\Model\Event;
10
use Shapin\Calendar\Model\RecurrenceRule;
11
12
class ICSImporter
13
{
14 6
    public function importFromFile(string $csvFile)
15
    {
16 6
        return $this->importFromString(file_get_contents($csvFile));
17
    }
18
19 6
    public function importFromString(string $string)
20
    {
21 6
        $vcalendar = VObject\Reader::read($string);
22
23 6
        $calendar = new CalendarModel();
24
25 6
        if (!isset($vcalendar->VEVENT)) {
26 1
            return $calendar;
27
        }
28
29 5
        foreach ($vcalendar->VEVENT as $vevent) {
30 5
            $event = new Event($vevent->DTSTART->getDateTime(), $vevent->DTEND->getDateTime());
31
            $event
32 5
                ->setSummary($vevent->SUMMARY->getValue())
33 5
                ->setDescription($vevent->DESCRIPTION->getValue())
34
            ;
35
36 5
            if (isset($vevent->RRULE)) {
37 3
                $event->setRecurrenceRule(RecurrenceRule::createFromArray($vevent->RRULE->getParts()));
38 4
            } elseif (isset($vevent->{'RECURRENCE-ID'})) {
39 2
                $event->setRecurrenceId($vevent->{'RECURRENCE-ID'}->getDateTime());
40
            }
41
42 5
            if (isset($vevent->CLASS)) {
43 2
                $event->setClassification($vevent->CLASS->getValue());
44
            }
45
46 5
            $calendar->addEvent($event);
47
        }
48
49 5
        return $calendar;
50
    }
51
}
52