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

ICSImporter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A importFromFile() 0 4 1
A importFromString() 0 24 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