Completed
Pull Request — master (#443)
by
unknown
03:08
created

VObjectICalService::getEvents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\Service\Ical;
6
7
use HDNET\Calendarize\Exception\UnableToGetEventsException;
8
use HDNET\Calendarize\Ical\VObjectEventAdapter;
9
use HDNET\Calendarize\Service\AbstractService;
10
use Sabre\VObject\Component\VEvent;
11
use Sabre\VObject\ParseException;
12
use Sabre\VObject\Reader;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
15
class VObjectICalService extends AbstractService implements ICalServiceInterface
16
{
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function getEvents(string $filename): array
22
    {
23
        $content = GeneralUtility::getUrl($filename);
24
        if ($content === false) {
25
            throw new UnableToGetEventsException('Unable to get "' . $filename . '".', 1603307743);
26
        }
27
28
        try {
29
            $vcalendar = Reader::read(
30
                $content,
31
                Reader::OPTION_FORGIVING
32
            );
33
        } catch (ParseException $e) {
34
            // Rethrow the exception to abstract the type
35
            throw new UnableToGetEventsException('Unable to parse invalid object.', 1603309056, $e);
36
        }
37
38
        /** @var VEvent[] $events */
39
        $events = [];
40
41
        foreach ($vcalendar->VEVENT as $event) {
42
            $events[] = new VObjectEventAdapter($event);
43
        }
44
45
        return $events;
46
    }
47
}
48