Completed
Push — master ( b10e41...04d25a )
by Torben
07:05 queued 05:12
created

ICalendarService::getiCalendarContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.3142
cc 1
eloc 14
nc 1
nop 1
crap 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use \TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * ICalenderService
21
 *
22
 * @author Torben Hansen <[email protected]>
23
 */
24
class ICalendarService
25
{
26
27
    /**
28
     * The object manager
29
     *
30
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
31
     * @inject
32
     */
33
    protected $objectManager;
34
35
    /**
36
     * The configuration manager
37
     *
38
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
39
     * @inject
40
     */
41
    protected $configurationManager;
42
43
    /**
44
     * ResourceFactory
45
     *
46
     * @var \TYPO3\CMS\Core\Resource\ResourceFactory
47
     * @inject
48
     */
49
    protected $resourceFactory = null;
50
51
    /**
52
     * FluidStandaloneService
53
     *
54
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
55
     * @inject
56
     */
57
    protected $fluidStandaloneService;
58
59
    /**
60
     * Initiates the ICS download for the given event
61
     *
62
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
63
     *
64
     * @throws \RuntimeException Exception
65
     *
66
     * @return void
67
     */
68 2
    public function downloadiCalendarFile(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
69
    {
70 2
        $storage = $this->resourceFactory->getDefaultStorage();
71 2
        if ($storage === null) {
72 1
            throw new \RuntimeException('Could not get the default storage', 1475590001);
73
        }
74 1
        $icalContent = $this->getICalendarContent($event);
75 1
        $tempFolder = $storage->getFolder('_temp_');
76 1
        $tempFile = $storage->createFile('event.ics', $tempFolder);
77 1
        $tempFile->setContents($icalContent);
78 1
        $storage->dumpFileContents($tempFile, true, 'event_' . $event->getUid() . '.ics');
79 1
    }
80
81
    /**
82
     * Returns the rendered iCalendar entry for the given event
83
     * according to RFC 2445
84
     *
85
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
86
     *
87
     * @return string
88
     */
89 1
    public function getiCalendarContent(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
90
    {
91
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $icalView */
92 1
        $icalView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
93 1
        $icalView->setFormat('txt');
94 1
        $layoutRootPaths = $this->fluidStandaloneService->getTemplateFolders('layout');
95 1
        $partialRootPaths = $this->fluidStandaloneService->getTemplateFolders('partial');
96
97 1
        $icalView->setLayoutRootPaths($layoutRootPaths);
98 1
        $icalView->setPartialRootPaths($partialRootPaths);
99 1
        $icalView->setTemplatePathAndFilename($this->fluidStandaloneService->getTemplatePath('Event/ICalendar.txt'));
100 1
        $icalView->assignMultiple([
101 1
            'event' => $event,
102 1
            'typo3Host' => GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')
103 1
        ]);
104
        // Render view and remove empty lines
105 1
        $icalContent = preg_replace('/^\h*\v+/m', '', $icalView->render());
106
        // Finally replace new lines with CRLF
107 1
        $icalContent = str_replace(chr(10), chr(13) . chr(10), $icalContent);
108 1
        return $icalContent;
109
    }
110
}