ICSImporter::importFromString()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 8.7857
c 0
b 0
f 0
cc 6
nc 8
nop 1
crap 6
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 8
    public function importFromFile(string $csvFile)
15
    {
16 8
        return $this->importFromString(file_get_contents($csvFile));
17
    }
18
19 8
    public function importFromString(string $string)
20
    {
21 8
        $vcalendar = VObject\Reader::read($string);
22
23 8
        $calendar = new CalendarModel();
24
25 8
        if (!isset($vcalendar->VEVENT)) {
26 1
            return $calendar;
27
        }
28
29 7
        foreach ($vcalendar->VEVENT as $vevent) {
30 7
            $event = new Event($vevent->DTSTART->getDateTime(), $vevent->DTEND->getDateTime());
31
            $event
32 7
                ->setSummary($vevent->SUMMARY->getValue())
33 7
                ->setDescription($vevent->DESCRIPTION->getValue())
34
            ;
35
36 7
            if (isset($vevent->RRULE)) {
37 5
                $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 7
            if (isset($vevent->CLASS)) {
43 2
                $event->setClassification($vevent->CLASS->getValue());
44
            }
45
46 7
            $calendar->addEvent($event);
47
        }
48
49 7
        return $calendar;
50
    }
51
}
52