Completed
Push — master ( a7c16c...d7804d )
by Olivier
01:25
created

ICSImporter::importFromString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 3
    public function importFromFile(string $csvFile)
15
    {
16 3
        return $this->importFromString(file_get_contents($csvFile));
17
    }
18
19 3
    public function importFromString(string $string)
20
    {
21 3
        $vcalendar = VObject\Reader::read($string);
22
23 3
        $calendar = new CalendarModel();
24
25 3
        foreach ($vcalendar->VEVENT as $vevent) {
26 3
            $event = new Event($vevent->DTSTART->getDateTime(), $vevent->DTEND->getDateTime());
27
            $event
28 3
                ->setSummary($vevent->SUMMARY->getValue())
29 3
                ->setDescription($vevent->DESCRIPTION->getValue())
30
            ;
31
32 3
            if (isset($vevent->RRULE)) {
33 3
                $event->setRecurrenceRule(RecurrenceRule::createFromArray($vevent->RRULE->getParts()));
34 2
            } elseif (isset($vevent->{'RECURRENCE-ID'})) {
35 2
                $event->setRecurrenceId($vevent->{'RECURRENCE-ID'}->getDateTime());
36
            }
37
38 3
            $calendar->addEvent($event);
39
        }
40
41 3
        return $calendar;
42
    }
43
}
44