Completed
Pull Request — master (#278)
by Pascale
03:06
created

ExternalTimeTable::injectIcsReaderService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * External service.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Service\TimeTable;
9
10
use HDNET\Calendarize\Domain\Model\Configuration;
11
use HDNET\Calendarize\Service\IcsReaderService;
12
use HDNET\Calendarize\Utility\DateTimeUtility;
13
use HDNET\Calendarize\Utility\HelperUtility;
14
use JMBTechnologyLimited\ICalDissect\ICalEvent;
15
use TYPO3\CMS\Core\Messaging\FlashMessage;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
/**
19
 * External service.
20
 */
21
class ExternalTimeTable extends AbstractTimeTable
22
{
23
    /**
24
     * ICS reader service.
25
     *
26
     * @var \HDNET\Calendarize\Service\IcsReaderService
27
     */
28
    protected $icsReaderService;
29
30
    public function injectIcsReaderService(IcsReaderService $icsReaderService)
31
    {
32
        $this->icsReaderService = $icsReaderService;
33
    }
34
35
    /**
36
     * Modify the given times via the configuration.
37
     *
38
     * @param array         $times
39
     * @param Configuration $configuration
40
     *
41
     * @throws \TYPO3\CMS\Core\Exception
42
     */
43
    public function handleConfiguration(array &$times, Configuration $configuration)
44
    {
45
        $url = $configuration->getExternalIcsUrl();
46
        if (!GeneralUtility::isValidUrl($url)) {
47
            HelperUtility::createFlashMessage(
48
                'Configuration with invalid ICS URL: ' . $url,
49
                'Index ICS URL',
50
                FlashMessage::ERROR
51
            );
52
53
            return;
54
        }
55
56
        $events = $this->icsReaderService->toArray($url);
57
        foreach ($events as $event) {
58
            /** @var $event ICalEvent */
59
            $startTime = DateTimeUtility::getDaySecondsOfDateTime($event->getStart());
60
            $endTime = DateTimeUtility::getDaySecondsOfDateTime($event->getEnd());
61
            if (self::DAY_END === $endTime) {
62
                $endTime = 0;
63
            }
64
65
            $entry = [
66
                'pid' => $configuration->getPid(),
67
                'start_date' => $event->getStart(),
68
                'end_date' => $this->getEventsFixedEndDate($event),
69
                'start_time' => $startTime,
70
                'end_time' => $endTime,
71
                'all_day' => 0 === $endTime,
72
                'state' => $configuration->getState(),
73
            ];
74
            $times[$this->calculateEntryKey($entry)] = $entry;
75
        }
76
    }
77
78
    /**
79
     * Fixes a parser related bug where the DTEND is EXCLUSIVE.
80
     * The parser uses it inclusive so every event is one day
81
     * longer than it should be.
82
     *
83
     * @param ICalEvent $event
84
     *
85
     * @return \DateTime
86
     */
87
    protected function getEventsFixedEndDate(ICalEvent $event)
88
    {
89
        if (!$event->getEnd() instanceof \DateTimeInterface) {
90
            return $event->getStart();
91
        }
92
93
        $end = clone $event->getEnd();
94
        $end->sub(new \DateInterval('P1D'));
95
        if ($end->format('Ymd') === $event->getStart()->format('Ymd')) {
96
            return $end;
97
        }
98
99
        return $event->getEnd();
100
    }
101
}
102