Completed
Push — master ( 555010...ab7f11 )
by Torben
04:40 queued 03:06
created

hasWriteAccessToTempFolder()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 0
crap 3
1
<?php
2
namespace DERHANSEN\SfEventMgt\Controller;
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\Domain\Model\Dto\EventDemand;
12
use DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand;
13
use DERHANSEN\SfEventMgt\Domain\Model\Event;
14
use DERHANSEN\SfEventMgt\Service;
15
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
16
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
17
18
/**
19
 * AdministrationController
20
 *
21
 * @author Torben Hansen <[email protected]>
22
 */
23
class AdministrationController extends AbstractController
24
{
25
    /**
26
     * CustomNotificationLogRepository
27
     *
28
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository
29
     */
30
    protected $customNotificationLogRepository = null;
31
32
    /**
33
     * ExportService
34
     *
35
     * @var \DERHANSEN\SfEventMgt\Service\ExportService
36
     */
37
    protected $exportService = null;
38
39
    /**
40
     * SettingsService
41
     *
42
     * @var \DERHANSEN\SfEventMgt\Service\SettingsService
43
     */
44
    protected $settingsService = null;
45
46
    /**
47
     * Backend User Session Service
48
     *
49
     * @var \DERHANSEN\SfEventMgt\Service\BeUserSessionService
50
     */
51
    protected $beUserSessionService = null;
52
53
    /**
54
     * The current page uid
55
     *
56
     * @var int
57
     */
58
    protected $pid = 0;
59
60
    /**
61
     * DI for $customNotificationLogRepository
62
     *
63
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
64
     */
65
    public function injectCustomNotificationLogRepository(
66
        \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
67
    ) {
68
        $this->customNotificationLogRepository = $customNotificationLogRepository;
69
    }
70
71
    /**
72
     * DI for $exportService
73
     *
74
     * @param Service\ExportService $exportService
75
     */
76
    public function injectExportService(\DERHANSEN\SfEventMgt\Service\ExportService $exportService)
77
    {
78
        $this->exportService = $exportService;
79
    }
80
81
    /**
82
     * DI for $settingsService
83
     *
84
     * @param Service\SettingsService $settingsService
85
     */
86
    public function injectSettingsService(\DERHANSEN\SfEventMgt\Service\SettingsService $settingsService)
87
    {
88
        $this->settingsService = $settingsService;
89
    }
90
91 2
    /**
92
     * DI for $beUserSessionService
93 2
     *
94 2
     * @param Service\BeUserSessionService $beUserSessionService
95
     */
96
    public function injectBeUserSessionService(\DERHANSEN\SfEventMgt\Service\BeUserSessionService $beUserSessionService)
97
    {
98
        $this->beUserSessionService = $beUserSessionService;
99
    }
100
101 4
    /**
102
     * Initialize action
103 4
     *
104 2
     * @return void
105 2
     */
106 4
    public function initializeAction()
107 4
    {
108 4
        $this->pid = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('id');
109 4
    }
110 4
111 4
    /**
112 4
     * Set date format for fields startDate and endDate
113 4
     *
114 4
     * @return void
115 4
     */
116 4
    public function initializeListAction()
117 4
    {
118 4
        if ($this->settings === null) {
119 4
            $this->redirect('settingsError');
120 4
        }
121
        $this->arguments->getArgument('searchDemand')
122
            ->getPropertyMappingConfiguration()->forProperty('startDate')
123
            ->setTypeConverterOption(
124
                DateTimeConverter::class,
125
                DateTimeConverter::CONFIGURATION_DATE_FORMAT,
126
                $this->settings['search']['dateFormat']
127
            );
128
        $this->arguments->getArgument('searchDemand')
129
            ->getPropertyMappingConfiguration()->forProperty('endDate')
130 8
            ->setTypeConverterOption(
131
                DateTimeConverter::class,
132
                DateTimeConverter::CONFIGURATION_DATE_FORMAT,
133 8
                $this->settings['search']['dateFormat']
134
            );
135 8
    }
136 6
137 6
    /**
138 8
     * List action for backend module
139
     *
140 8
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand
141 4
     * @param int $messageId MessageID
142 4
     *
143
     * @return void
144 8
     */
145 2
    public function listAction(SearchDemand $searchDemand = null, $messageId = null)
146 2
    {
147 2
        /** @var EventDemand $demand */
148 2
        $demand = $this->objectManager->get(EventDemand::class);
149
150 8
        if ($searchDemand !== null) {
151 8
            $searchDemand->setFields($this->settings['search']['fields']);
152 8
153 8
            $sessionData = [];
154
            $sessionData['searchDemand'] = $searchDemand;
155
            $this->beUserSessionService->saveSessionData($sessionData);
156
        } else {
157
            // Try to restore search demand from Session
158
            $searchDemand = $this->beUserSessionService->getSessionDataByKey('searchDemand');
159
        }
160
161
        $demand->setSearchDemand($searchDemand);
162 2
163
        if ($this->pid > 0) {
164 2
            $demand->setStoragePage($this->pid);
165 2
        }
166
167
        $variables = [];
168
        if ($messageId !== null && is_numeric($messageId)) {
169
            $variables['showMessage'] = true;
170
            $variables['messageTitleKey'] = 'administration.message-' . $messageId . '.title';
171
            $variables['messageContentKey'] = 'administration.message-' . $messageId . '.content';
172
        }
173 2
174
        $variables['events'] = $this->eventRepository->findDemanded($demand);
175 2
        $variables['searchDemand'] = $searchDemand;
176 2
        $variables['csvExportPossible'] = $this->hasWriteAccessToTempFolder();
177 2
        $this->view->assignMultiple($variables);
178
    }
179
180
    /**
181
     * Returns, if the user has write access to temp folder and if not, adds a flash message if configured
182
     *
183
     * @return bool
184
     */
185
    protected function hasWriteAccessToTempFolder()
186 2
    {
187
        $hasAccess = $this->exportService->hasWriteAccessToTempFolder();
188 2
        if (!$hasAccess && (bool)$this->settings['csvExport']['showFlashMessageForInsufficientAccessRights']) {
189 2
            $this->addFlashMessage(
190 2
                LocalizationUtility::translate('insufficientAccessToTemp', $this->extensionName),
191 2
                LocalizationUtility::translate('csvExportDisabled', $this->extensionName),
192 2
                \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING
193 2
            );
194 2
        }
195 2
196
        return $hasAccess;
197
    }
198
199
    /**
200
     * Export registrations for a given event
201
     *
202
     * @param int $eventUid Event UID
203
     *
204
     * @return bool Always FALSE, since no view should be rendered
205 2
     */
206
    public function exportAction($eventUid)
207 2
    {
208 2
        $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport']);
209 2
210 2
        return false;
211 2
    }
212
213 2
    /**
214 2
     * Calls the handleExpiredRegistrations Service
215 2
     *
216
     * @return void
217
     */
218
    public function handleExpiredRegistrationsAction()
219
    {
220
        $this->registrationService->handleExpiredRegistrations($this->settings['registration']['deleteExpiredRegistrations']);
221
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 1]);
222
    }
223
224
    /**
225
     * The index notify action
226
     *
227
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
228
     *
229
     * @return void
230
     */
231
    public function indexNotifyAction(Event $event)
232
    {
233
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
234
        $logEntries = $this->customNotificationLogRepository->findByEvent($event);
0 ignored issues
show
Documentation Bug introduced by
The method findByEvent does not exist on object<DERHANSEN\SfEvent...ificationLogRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
235
        $this->view->assignMultiple([
236
            'event' => $event,
237
            'customNotifications' => $customNotifications,
238
            'logEntries' => $logEntries,
239
        ]);
240
    }
241
242
    /**
243
     * Notify action
244
     *
245
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
246
     * @param string $customNotification CustomNotification
247
     *
248
     * @return void
249
     */
250
    public function notifyAction(Event $event, $customNotification)
251
    {
252
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
253
        $result = $this->notificationService->sendCustomNotification($event, $customNotification, $this->settings);
254
        $this->notificationService->createCustomNotificationLogentry(
255
            $event,
256
            $customNotifications[$customNotification],
257
            $result
258
        );
259
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 2]);
260
    }
261
262
    /**
263
     * Shows the settings error view
264
     *
265
     * @return void
266
     */
267
    public function settingsErrorAction()
268
    {
269
    }
270
271
    /**
272
     * Suppress default validation messages
273
     *
274
     * @return bool
275
     */
276
    protected function getErrorFlashMessage()
277
    {
278
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method TYPO3\CMS\Extbase\Mvc\Co...r::getErrorFlashMessage of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
279
    }
280
}
281