Completed
Push — development ( f641fb...f9a14f )
by Torben
03:29
created

injectCustomNotificationLogRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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\Backend\View\BackendTemplateView;
16
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
17
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
18
19
/**
20
 * AdministrationController
21
 *
22
 * @author Torben Hansen <[email protected]>
23
 */
24
class AdministrationController extends AbstractController
25
{
26
    /**
27
     * Backend Template Container
28
     *
29
     * @var string
30
     */
31
    protected $defaultViewObjectName = \TYPO3\CMS\Backend\View\BackendTemplateView::class;
32
33
    /**
34
     * CustomNotificationLogRepository
35
     *
36
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository
37
     */
38
    protected $customNotificationLogRepository = null;
39
40
    /**
41
     * ExportService
42
     *
43
     * @var \DERHANSEN\SfEventMgt\Service\ExportService
44
     */
45
    protected $exportService = null;
46
47
    /**
48
     * SettingsService
49
     *
50
     * @var \DERHANSEN\SfEventMgt\Service\SettingsService
51
     */
52
    protected $settingsService = null;
53
54
    /**
55
     * Backend User Session Service
56
     *
57
     * @var \DERHANSEN\SfEventMgt\Service\BeUserSessionService
58
     */
59
    protected $beUserSessionService = null;
60
61
    /**
62
     * The current page uid
63
     *
64
     * @var int
65
     */
66
    protected $pid = 0;
67
68
    /**
69
     * BackendTemplateContainer
70
     *
71
     * @var BackendTemplateView
72
     */
73
    protected $view;
74
75
    /**
76
     * DI for $customNotificationLogRepository
77
     *
78
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
79
     */
80
    public function injectCustomNotificationLogRepository(
81
        \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
82
    ) {
83
        $this->customNotificationLogRepository = $customNotificationLogRepository;
84
    }
85
86
    /**
87
     * DI for $exportService
88
     *
89
     * @param Service\ExportService $exportService
90
     */
91 1
    public function injectExportService(\DERHANSEN\SfEventMgt\Service\ExportService $exportService)
92
    {
93 1
        $this->exportService = $exportService;
94 1
    }
95
96
    /**
97
     * DI for $settingsService
98
     *
99
     * @param Service\SettingsService $settingsService
100
     */
101 2
    public function injectSettingsService(\DERHANSEN\SfEventMgt\Service\SettingsService $settingsService)
102
    {
103 2
        $this->settingsService = $settingsService;
104 1
    }
105 1
106 2
    /**
107 2
     * DI for $beUserSessionService
108 2
     *
109 2
     * @param Service\BeUserSessionService $beUserSessionService
110 2
     */
111 2
    public function injectBeUserSessionService(\DERHANSEN\SfEventMgt\Service\BeUserSessionService $beUserSessionService)
112 2
    {
113 2
        $this->beUserSessionService = $beUserSessionService;
114 2
    }
115 2
116 2
    /**
117 2
     * Set up the doc header properly here
118 2
     *
119 2
     * @param ViewInterface $view
120 2
     */
121
    protected function initializeView(ViewInterface $view)
122
    {
123
        /** @var BackendTemplateView $view */
124
        parent::initializeView($view);
125
        if ($this->actionMethodName === 'listAction'
126
            || $this->actionMethodName === 'indexNotifyAction'
127
            || $this->actionMethodName === 'settingsErrorAction') {
128
            //$this->generateMenu();
129
            //$this->registerDocheaderButtons();
130 4
131
            $pageRenderer = $this->view->getModuleTemplate()->getPageRenderer();
132
            $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DateTimePicker');
133 4
134
            $dateFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ?
135 4
                ['MM-DD-YYYY', 'HH:mm MM-DD-YYYY'] :
136 3
                ['DD-MM-YYYY', 'HH:mm DD-MM-YYYY'];
137 3
            $pageRenderer->addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
138 4
139
            $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
140 4
            if ($view instanceof BackendTemplateView) {
141 2
                $view->getModuleTemplate()->getPageRenderer()->addCssFile(
142 2
                    'EXT:sf_event_mgt/Resources/Public/Css/administration.css'
143
                );
144 4
            }
145 1
        }
146 1
    }
147 1
148 1
    /**
149
     * Initialize action
150 4
     *
151 4
     * @return void
152 4
     */
153 4
    public function initializeAction()
154
    {
155
        $this->pid = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('id');
156
    }
157
158
    /**
159
     * Set date format for fields startDate and endDate
160
     *
161
     * @return void
162 1
     */
163
    public function initializeListAction()
164 1
    {
165 1
        if ($this->settings === null) {
166
            $this->redirect('settingsError');
167
        }
168
        $this->arguments->getArgument('searchDemand')
169
            ->getPropertyMappingConfiguration()->forProperty('startDate')
170
            ->setTypeConverterOption(
171
                DateTimeConverter::class,
172
                DateTimeConverter::CONFIGURATION_DATE_FORMAT,
173 1
                $this->settings['search']['dateFormat']
174
            );
175 1
        $this->arguments->getArgument('searchDemand')
176 1
            ->getPropertyMappingConfiguration()->forProperty('endDate')
177 1
            ->setTypeConverterOption(
178
                DateTimeConverter::class,
179
                DateTimeConverter::CONFIGURATION_DATE_FORMAT,
180
                $this->settings['search']['dateFormat']
181
            );
182
    }
183
184
    /**
185
     * List action for backend module
186 1
     *
187
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand
188 1
     * @param int $messageId MessageID
189 1
     *
190 1
     * @return void
191 1
     */
192 1
    public function listAction(SearchDemand $searchDemand = null, $messageId = null)
193 1
    {
194 1
        /** @var EventDemand $demand */
195 1
        $demand = $this->objectManager->get(EventDemand::class);
196
197
        if ($searchDemand !== null) {
198
            $searchDemand->setFields($this->settings['search']['fields']);
199
200
            $sessionData = [];
201
            $sessionData['searchDemand'] = $searchDemand;
202
            $this->beUserSessionService->saveSessionData($sessionData);
203
        } else {
204
            // Try to restore search demand from Session
205 1
            $searchDemand = $this->beUserSessionService->getSessionDataByKey('searchDemand');
206
        }
207 1
208 1
        $demand->setSearchDemand($searchDemand);
209 1
210 1
        if ($this->pid > 0) {
211 1
            $demand->setStoragePage($this->pid);
212
        }
213 1
214 1
        $variables = [];
215 1
        if ($messageId !== null && is_numeric($messageId)) {
216
            $variables['showMessage'] = true;
217
            $variables['messageTitleKey'] = 'administration.message-' . $messageId . '.title';
218
            $variables['messageContentKey'] = 'administration.message-' . $messageId . '.content';
219
        }
220
221
        $variables['pid'] = $this->pid;
222
        $variables['events'] = $this->eventRepository->findDemanded($demand);
223
        $variables['searchDemand'] = $searchDemand;
224
        $variables['csvExportPossible'] = $this->getBackendUser()->getDefaultUploadTemporaryFolder() !== null;
225
        $this->view->assignMultiple($variables);
226
    }
227
228
    /**
229
     * Export registrations for a given event
230
     *
231
     * @param int $eventUid Event UID
232
     *
233
     * @return void
234
     */
235
    public function exportAction($eventUid)
236
    {
237
        $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport']);
238
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method exportAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
239
    }
240
241
    /**
242
     * Calls the handleExpiredRegistrations Service
243
     *
244
     * @return void
245
     */
246
    public function handleExpiredRegistrationsAction()
247
    {
248
        $this->registrationService->handleExpiredRegistrations($this->settings['registration']['deleteExpiredRegistrations']);
249
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 1]);
250
    }
251
252
    /**
253
     * The index notify action
254
     *
255
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
256
     *
257
     * @return void
258
     */
259
    public function indexNotifyAction(Event $event)
260
    {
261
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
262
        $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...
263
        $this->view->assignMultiple([
264
            'event' => $event,
265
            'customNotifications' => $customNotifications,
266
            'logEntries' => $logEntries,
267
        ]);
268
    }
269
270
    /**
271
     * Notify action
272
     *
273
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
274
     * @param string $customNotification CustomNotification
275
     *
276
     * @return void
277
     */
278
    public function notifyAction(Event $event, $customNotification)
279
    {
280
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
281
        $result = $this->notificationService->sendCustomNotification($event, $customNotification, $this->settings);
282
        $this->notificationService->createCustomNotificationLogentry(
283
            $event,
284
            $customNotifications[$customNotification],
285
            $result
286
        );
287
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 2]);
288
    }
289
290
    /**
291
     * Shows the settings error view
292
     *
293
     * @return void
294
     */
295
    public function settingsErrorAction()
296
    {
297
    }
298
299
    /**
300
     * Suppress default validation messages
301
     *
302
     * @return bool
303
     */
304
    protected function getErrorFlashMessage()
305
    {
306
        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...
307
    }
308
309
    /**
310
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
311
     */
312
    protected function getBackendUser()
313
    {
314
        return $GLOBALS['BE_USER'];
315
    }
316
}
317