Completed
Push — master ( c18e86...b01b7f )
by Torben
04:02
created

ICalendarService::injectResourceFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use DERHANSEN\SfEventMgt\Exception;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Fluid\View\StandaloneView;
14
15
/**
16
 * ICalenderService
17
 *
18
 * @author Torben Hansen <[email protected]>
19
 */
20
class ICalendarService
21
{
22
    /**
23
     * The object manager
24
     *
25
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
26
     */
27
    protected $objectManager;
28
29
    /**
30
     * The configuration manager
31
     *
32
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
33
     */
34
    protected $configurationManager;
35
36
    /**
37
     * FluidStandaloneService
38
     *
39
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
40
     */
41
    protected $fluidStandaloneService;
42
43
    /**
44
     * DI for $configurationManager
45
     *
46
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
47
     */
48
    public function injectConfigurationManager(
49
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
50
    ) {
51
        $this->configurationManager = $configurationManager;
52
    }
53
54
    /**
55
     * DI for $fluidStandaloneService
56
     *
57
     * @param FluidStandaloneService $fluidStandaloneService
58
     */
59
    public function injectFluidStandaloneService(
60
        \DERHANSEN\SfEventMgt\Service\FluidStandaloneService $fluidStandaloneService
61
    ) {
62
        $this->fluidStandaloneService = $fluidStandaloneService;
63
    }
64
65
    /**
66
     * DI for $objectManager
67
     *
68 4
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
69
     */
70 4
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
71 4
    {
72 2
        $this->objectManager = $objectManager;
73
    }
74 2
75 2
    /**
76 2
     * Initiates the ICS download for the given event
77 2
     *
78 2
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
79 2
     * @throws Exception Exception
80
     * @return void
81
     */
82
    public function downloadiCalendarFile(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
83
    {
84
        $content = $this->getICalendarContent($event);
85
        header('Content-Disposition: attachment; filename="event' . $event->getUid() . '.ics"');
86
        header('Content-Type: text/calendar');
87
        header('Content-Length: ' . strlen($content));
88
        header('Expires: 0');
89 2
        header('Cache-Control: must-revalidate');
90
        header('Pragma: no-cache');
91
        echo $content;
92 2
    }
93 2
94 2
    /**
95 2
     * Returns the rendered iCalendar entry for the given event
96
     * according to RFC 2445
97 2
     *
98 2
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
99 2
     *
100 2
     * @return string
101 2
     */
102 2
    public function getiCalendarContent(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
103 2
    {
104
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $icalView */
105 2
        $icalView = $this->objectManager->get(StandaloneView::class);
106
        $icalView->setFormat('txt');
107 2
        $templateRootPaths = $this->fluidStandaloneService->getTemplateFolders('template');
108 2
        $layoutRootPaths = $this->fluidStandaloneService->getTemplateFolders('layout');
109
        $partialRootPaths = $this->fluidStandaloneService->getTemplateFolders('partial');
110
        $icalView->setTemplateRootPaths($templateRootPaths);
111
        $icalView->setLayoutRootPaths($layoutRootPaths);
112
        $icalView->setPartialRootPaths($partialRootPaths);
113
        $icalView->setTemplate('Event/ICalendar.txt');
114
        $icalView->assignMultiple([
115
            'event' => $event,
116
            'typo3Host' => GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')
117
        ]);
118
        // Render view and remove empty lines
119
        $icalContent = preg_replace('/^\h*\v+/m', '', $icalView->render());
120
        // Finally replace new lines with CRLF
121
        $icalContent = str_replace(chr(10), chr(13) . chr(10), $icalContent);
122
123
        return $icalContent;
124
    }
125
}
126