Completed
Push — development ( 71c1f6...1923e7 )
by Torben
47:02 queued 02:14
created

AdministrationController::indexNotifyAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 1
    /**
92
     * DI for $beUserSessionService
93 1
     *
94 1
     * @param Service\BeUserSessionService $beUserSessionService
95
     */
96
    public function injectBeUserSessionService(\DERHANSEN\SfEventMgt\Service\BeUserSessionService $beUserSessionService)
97
    {
98
        $this->beUserSessionService = $beUserSessionService;
99
    }
100
101 2
    /**
102
     * Initialize action
103 2
     *
104 1
     * @return void
105 1
     */
106 2
    public function initializeAction()
107 2
    {
108 2
        $this->pid = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('id');
109 2
    }
110 2
111 2
    /**
112 2
     * Set date format for fields startDate and endDate
113 2
     *
114 2
     * @return void
115 2
     */
116 2
    public function initializeListAction()
117 2
    {
118 2
        if ($this->settings === null) {
119 2
            $this->redirect('settingsError');
120 2
        }
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 4
            ->setTypeConverterOption(
131
                DateTimeConverter::class,
132
                DateTimeConverter::CONFIGURATION_DATE_FORMAT,
133 4
                $this->settings['search']['dateFormat']
134
            );
135 4
    }
136 3
137 3
    /**
138 4
     * List action for backend module
139
     *
140 4
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand
141 2
     * @param int $messageId MessageID
142 2
     *
143
     * @return void
144 4
     */
145 1
    public function listAction(SearchDemand $searchDemand = null, $messageId = null)
146 1
    {
147 1
        /** @var EventDemand $demand */
148 1
        $demand = $this->objectManager->get(EventDemand::class);
149
150 4
        if ($searchDemand !== null) {
151 4
            $searchDemand->setFields($this->settings['search']['fields']);
152 4
153 4
            $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 1
163
        if ($this->pid > 0) {
164 1
            $demand->setStoragePage($this->pid);
165 1
        }
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 1
174
        $variables['events'] = $this->eventRepository->findDemanded($demand);
175 1
        $variables['searchDemand'] = $searchDemand;
176 1
        $variables['csvExportPossible'] = $this->getBackendUser()->getDefaultUploadTemporaryFolder() !== null;
177 1
        $this->view->assignMultiple($variables);
178
    }
179
180
    /**
181
     * Export registrations for a given event
182
     *
183
     * @param int $eventUid Event UID
184
     *
185
     * @return bool Always FALSE, since no view should be rendered
186 1
     */
187
    public function exportAction($eventUid)
188 1
    {
189 1
        $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport']);
190 1
191 1
        return false;
192 1
    }
193 1
194 1
    /**
195 1
     * Calls the handleExpiredRegistrations Service
196
     *
197
     * @return void
198
     */
199
    public function handleExpiredRegistrationsAction()
200
    {
201
        $this->registrationService->handleExpiredRegistrations($this->settings['registration']['deleteExpiredRegistrations']);
202
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 1]);
203
    }
204
205 1
    /**
206
     * The index notify action
207 1
     *
208 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
209 1
     *
210 1
     * @return void
211 1
     */
212
    public function indexNotifyAction(Event $event)
213 1
    {
214 1
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
215 1
        $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...
216
        $this->view->assignMultiple([
217
            'event' => $event,
218
            'customNotifications' => $customNotifications,
219
            'logEntries' => $logEntries,
220
        ]);
221
    }
222
223
    /**
224
     * Notify action
225
     *
226
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
227
     * @param string $customNotification CustomNotification
228
     *
229
     * @return void
230
     */
231
    public function notifyAction(Event $event, $customNotification)
232
    {
233
        $customNotifications = $this->settingsService->getCustomNotifications($this->settings);
234
        $result = $this->notificationService->sendCustomNotification($event, $customNotification, $this->settings);
235
        $this->notificationService->createCustomNotificationLogentry(
236
            $event,
237
            $customNotifications[$customNotification],
238
            $result
239
        );
240
        $this->redirect('list', 'Administration', 'SfEventMgt', ['demand' => null, 'messageId' => 2]);
241
    }
242
243
    /**
244
     * Shows the settings error view
245
     *
246
     * @return void
247
     */
248
    public function settingsErrorAction()
249
    {
250
    }
251
252
    /**
253
     * Suppress default validation messages
254
     *
255
     * @return bool
256
     */
257
    protected function getErrorFlashMessage()
258
    {
259
        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...
260
    }
261
262
    /**
263
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
264
     */
265
    protected function getBackendUser()
266
    {
267
        return $GLOBALS['BE_USER'];
268
    }
269
}
270