Completed
Push — development ( 178cf5...deb2fc )
by Torben
13:04
created

ICalendarService::injectObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
use TYPO3\CMS\Fluid\View\StandaloneView;
19
use \DERHANSEN\SfEventMgt\Exception;
20
21
/**
22
 * ICalenderService
23
 *
24
 * @author Torben Hansen <[email protected]>
25
 */
26
class ICalendarService
27
{
28
    /**
29
     * The object manager
30
     *
31
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
32
     */
33
    protected $objectManager;
34
35
    /**
36
     * The configuration manager
37
     *
38
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
39
     */
40
    protected $configurationManager;
41
42
    /**
43
     * ResourceFactory
44
     *
45
     * @var \TYPO3\CMS\Core\Resource\ResourceFactory
46
     */
47
    protected $resourceFactory = null;
48
49
    /**
50
     * FluidStandaloneService
51
     *
52
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
53
     */
54
    protected $fluidStandaloneService;
55
56
    /**
57
     * DI for $configurationManager
58
     *
59
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
60
     */
61
    public function injectConfigurationManager(
62
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
63
    ) {
64
        $this->configurationManager = $configurationManager;
65
    }
66
67
    /**
68 2
     * DI for $fluidStandaloneService
69
     *
70 2
     * @param FluidStandaloneService $fluidStandaloneService
71 2
     */
72 1
    public function injectFluidStandaloneService(
73
        \DERHANSEN\SfEventMgt\Service\FluidStandaloneService $fluidStandaloneService
74 1
    ) {
75 1
        $this->fluidStandaloneService = $fluidStandaloneService;
76 1
    }
77 1
78 1
    /**
79 1
     * DI for $objectManager
80
     *
81
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
82
     */
83
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
84
    {
85
        $this->objectManager = $objectManager;
86
    }
87
88
    /**
89 1
     * DI for $resourceFactory
90
     *
91
     * @param \TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory
92 1
     */
93 1
    public function injectResourceFactory(\TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory)
94 1
    {
95 1
        $this->resourceFactory = $resourceFactory;
96
    }
97 1
98 1
    /**
99 1
     * Initiates the ICS download for the given event
100 1
     *
101 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
102 1
     * @throws Exception Exception
103 1
     * @return void
104
     */
105 1
    public function downloadiCalendarFile(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
106
    {
107 1
        $storage = $this->resourceFactory->getDefaultStorage();
108 1
        if ($storage === null) {
109
            throw new Exception('Could not get the default storage', 1475590001);
110
        }
111
        $icalContent = $this->getICalendarContent($event);
112
        $tempFolder = $storage->getFolder('_temp_');
113
        $tempFile = $storage->createFile('event.ics', $tempFolder);
114
        $tempFile->setContents($icalContent);
115
        $storage->dumpFileContents($tempFile, true, 'event_' . $event->getUid() . '.ics');
0 ignored issues
show
Bug introduced by
It seems like $tempFile defined by $storage->createFile('event.ics', $tempFolder) on line 113 can also be of type null; however, TYPO3\CMS\Core\Resource\...age::dumpFileContents() does only seem to accept object<TYPO3\CMS\Core\Resource\FileInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
    }
117
118
    /**
119
     * Returns the rendered iCalendar entry for the given event
120
     * according to RFC 2445
121
     *
122
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event The event
123
     *
124
     * @return string
125
     */
126
    public function getiCalendarContent(\DERHANSEN\SfEventMgt\Domain\Model\Event $event)
127
    {
128
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $icalView */
129
        $icalView = $this->objectManager->get(StandaloneView::class);
130
        $icalView->setFormat('txt');
131
        $layoutRootPaths = $this->fluidStandaloneService->getTemplateFolders('layout');
132
        $partialRootPaths = $this->fluidStandaloneService->getTemplateFolders('partial');
133
        $icalView->setLayoutRootPaths($layoutRootPaths);
134
        $icalView->setPartialRootPaths($partialRootPaths);
135
        $icalView->setTemplatePathAndFilename($this->fluidStandaloneService->getTemplatePath('Event/ICalendar.txt'));
136
        $icalView->assignMultiple([
137
            'event' => $event,
138
            'typo3Host' => GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')
139
        ]);
140
        // Render view and remove empty lines
141
        $icalContent = preg_replace('/^\h*\v+/m', '', $icalView->render());
142
        // Finally replace new lines with CRLF
143
        $icalContent = str_replace(chr(10), chr(13) . chr(10), $icalContent);
144
        return $icalContent;
145
    }
146
}
147