Completed
Push — master ( b10e41...04d25a )
by Torben
07:05 queued 05:12
created

NotificationService::getTemplateFolders()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.2488

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 17
cts 21
cp 0.8095
rs 8.439
cc 6
eloc 16
nc 24
nop 1
crap 6.2488
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 DERHANSEN\SfEventMgt\Utility\MessageType;
18
use \TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * NotificationService
22
 *
23
 * @author Torben Hansen <[email protected]>
24
 */
25
class NotificationService
26
{
27
28
    /**
29
     * The object manager
30
     *
31
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
32
     * @inject
33
     */
34
    protected $objectManager;
35
36
    /**
37
     * Registration repository
38
     *
39
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
40
     * @inject
41
     */
42
    protected $registrationRepository = null;
43
44
    /**
45
     * Email Service
46
     *
47
     * @var \DERHANSEN\SfEventMgt\Service\EmailService
48
     * @inject
49
     */
50
    protected $emailService;
51
52
    /**
53
     * Hash Service
54
     *
55
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
56
     * @inject
57
     */
58
    protected $hashService;
59
60
    /**
61
     * FluidStandaloneService
62
     *
63
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
64
     * @inject
65
     */
66
    protected $fluidStandaloneService;
67
68
    /**
69
     * CustomNotificationLogRepository
70
     *
71
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository
72
     * @inject
73
     */
74
    protected $customNotificationLogRepository = null;
75
76
    /**
77
     * Sends a custom notification defined by the given customNotification key
78
     * to all confirmed users of the event
79
     *
80
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
81
     * @param string $customNotification CustomNotification
82
     * @param array $settings Settings
83
     *
84
     * @return int Number of notifications sent
85
     */
86 4
    public function sendCustomNotification($event, $customNotification, $settings)
87
    {
88 4
        if ($this->cantSendCustomNotification($event, $settings, $customNotification)) {
89 1
            return 0;
90
        }
91 3
        $count = 0;
92
93 3
        $constraints = $settings['notification']['customNotifications'][$customNotification]['constraints'];
94 3
        $registrations = $this->registrationRepository->findNotificationRegistrations($event, $constraints);
95 3
        foreach ($registrations as $registration) {
96
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
97 3
            if ($registration->isConfirmed() && !$registration->isIgnoreNotifications()) {
98 1
                $result = $this->sendUserMessage(
99 1
                    $event,
100 1
                    $registration,
101 1
                    $settings,
102 1
                    MessageType::CUSTOM_NOTIFICATION,
103
                    $customNotification
104 1
                );
105 1
                if ($result) {
106 1
                    $count += 1;
107 1
                }
108 1
            }
109 3
        }
110 3
        return $count;
111
    }
112
113
    /**
114
     * Returns true if conditions are not met to send a custom notification
115
     *
116
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
117
     * @param array $settings
118
     * @param string $customNotification
119
     *
120
     * @return bool
121
     */
122 4
    protected function cantSendCustomNotification($event, $settings, $customNotification)
123
    {
124 4
        return is_null($event) || $customNotification == '' || $settings == '' || !is_array($settings);
125
    }
126
127
    /**
128
     * Adds a logentry to the custom notification log
129
     *
130
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
131
     * @param string $details Details
132
     * @param int $emailsSent E-Mails sent
133
     *
134
     * @return void
135
     */
136 1
    public function createCustomNotificationLogentry($event, $details, $emailsSent)
137
    {
138 1
        $notificationlogEntry = new \DERHANSEN\SfEventMgt\Domain\Model\CustomNotificationLog();
139 1
        $notificationlogEntry->setEvent($event);
140 1
        $notificationlogEntry->setDetails($details);
141 1
        $notificationlogEntry->setEmailsSent($emailsSent);
142 1
        $notificationlogEntry->setCruserId($GLOBALS['BE_USER']->user['uid']);
143 1
        $this->customNotificationLogRepository->add($notificationlogEntry);
144 1
    }
145
146
    /**
147
     * Sends a message to the user based on the given type
148
     *
149
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
150
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
151
     * @param array $settings Settings
152
     * @param int $type Type
153
     * @param string $customNotification CustomNotification
154
     *
155
     * @return bool TRUE if successful, else FALSE
156
     */
157 11
    public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '')
158
    {
159 11
        list($template, $subject) = $this->getUserMessageTemplateSubject($settings, $type, $customNotification);
160
161 11
        if (is_null($event) || is_null($registration) || !is_array($settings) || (substr($template, -5) != '.html')) {
162 2
            return false;
163
        }
164
165 9
        if (!$registration->isIgnoreNotifications()) {
166 6
            $body = $this->getNotificationBody($event, $registration, $template, $settings);
167 6
            return $this->emailService->sendEmailMessage(
168 6
                $settings['notification']['senderEmail'],
169 6
                $registration->getEmail(),
170 6
                $subject,
171 6
                $body,
172 6
                $settings['notification']['senderName']
173 6
            );
174
        }
175 3
        return false;
176
    }
177
178
    /**
179
     * Returns an array with template and subject for the user message
180
     *
181
     * @param array $settings
182
     * @param int $type Type
183
     * @param string $customNotification
184
     * @return array
185
     */
186 11
    protected function getUserMessageTemplateSubject($settings, $type, $customNotification)
187
    {
188 11
        $template = 'Notification/User/RegistrationNew.html';
189 11
        $subject = $settings['notification']['registrationNew']['userSubject'];
190
        switch ($type) {
191 11
            case MessageType::REGISTRATION_CONFIRMED:
192 3
                $template = 'Notification/User/RegistrationConfirmed.html';
193 3
                $subject = $settings['notification']['registrationConfirmed']['userSubject'];
194 3
                break;
195 8
            case MessageType::REGISTRATION_CANCELLED:
196
                $template = 'Notification/User/RegistrationCancelled.html';
197
                $subject = $settings['notification']['registrationCancelled']['userSubject'];
198
                break;
199 8
            case MessageType::CUSTOM_NOTIFICATION:
200 1
                $template = 'Notification/User/Custom/' . $settings['notification']['customNotifications'][$customNotification]['template'];
201 1
                $subject = $settings['notification']['customNotifications'][$customNotification]['subject'];
202 1
                break;
203 7
            case MessageType::REGISTRATION_NEW:
204 7
            default:
205 7
        }
206
        return [
207 11
            $template,
208
            $subject
209 11
        ];
210
    }
211
212
    /**
213
     * Sends a message to the admin based on the given type
214
     *
215
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
216
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
217
     * @param array $settings Settings
218
     * @param int $type Type
219
     *
220
     * @return bool TRUE if successful, else FALSE
221
     */
222 16
    public function sendAdminMessage($event, $registration, $settings, $type)
223
    {
224 16
        list($template, $subject) = $this->getAdminMessageTemplateSubject($settings, $type);
225
226
227 16
        if (is_null($event) || is_null($registration || !is_array($settings)) ||
228 15
            ($event->getNotifyAdmin() === false && $event->getNotifyOrganisator() === false)
229 16
        ) {
230 4
            return false;
231
        }
232
233 12
        $allEmailsSent = true;
234 12
        $body = $this->getNotificationBody($event, $registration, $template, $settings);
235 12
        if ($event->getNotifyAdmin()) {
236 9
            $adminEmailArr = GeneralUtility::trimExplode(',', $settings['notification']['adminEmail'], true);
237 9
            foreach ($adminEmailArr as $adminEmail) {
238 9
                $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
239 9
                        $settings['notification']['senderEmail'],
240 9
                        $adminEmail,
241 9
                        $subject,
242 9
                        $body,
243 9
                        $settings['notification']['senderName']
244 9
                    );
245 9
            }
246 9
        }
247 12
        if ($event->getNotifyOrganisator() && $event->getOrganisator()) {
248 3
            $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
249 3
                    $settings['notification']['senderEmail'],
250 3
                    $event->getOrganisator()->getEmail(),
251 3
                    $subject,
252 3
                    $body,
253 3
                    $settings['notification']['senderName']
254 3
                );
255 3
        }
256 12
        return $allEmailsSent;
257
    }
258
259
    /**
260
     * Returns an array with template and subject for the admin message
261
     *
262
     * @param array $settings
263
     * @param int $type Type
264
     * @return array
265
     */
266 16
    protected function getAdminMessageTemplateSubject($settings, $type)
267
    {
268 16
        $template = 'Notification/Admin/RegistrationNew.html';
269 16
        $subject = $settings['notification']['registrationNew']['adminSubject'];
270
        switch ($type) {
271 16
            case MessageType::REGISTRATION_CONFIRMED:
272 5
                $template = 'Notification/Admin/RegistrationConfirmed.html';
273 5
                $subject = $settings['notification']['registrationConfirmed']['adminSubject'];
274 5
                break;
275 11
            case MessageType::REGISTRATION_CANCELLED:
276
                $template = 'Notification/Admin/RegistrationCancelled.html';
277
                $subject = $settings['notification']['registrationCancelled']['adminSubject'];
278
                break;
279 11
            case MessageType::REGISTRATION_NEW:
280 11
            default:
281 11
        }
282
        return [
283 16
            $template,
284
            $subject
285 16
        ];
286
    }
287
288
    /**
289
     * Returns the rendered HTML for the given template
290
     *
291
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
292
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
293
     * @param string $template Template
294
     * @param array $settings Settings
295
     *
296
     * @return string
297
     */
298 18
    protected function getNotificationBody($event, $registration, $template, $settings)
299
    {
300
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
301 18
        $emailView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
302 18
        $emailView->setFormat('html');
303 18
        $layoutRootPaths = $this->fluidStandaloneService->getTemplateFolders('layout');
304 18
        $partialRootPaths = $this->fluidStandaloneService->getTemplateFolders('partial');
305
306 18
        if (TYPO3_MODE === 'BE' && $registration->getLanguage() !== '') {
307
            // Temporary set Language of current BE user to given language
308
            $GLOBALS['BE_USER']->uc['lang'] = $registration->getLanguage();
309
            $emailView->getRequest()->setControllerExtensionName('SfEventMgt');
310
        }
311
312 18
        $emailView->setLayoutRootPaths($layoutRootPaths);
313 18
        $emailView->setPartialRootPaths($partialRootPaths);
314 18
        $emailView->setTemplatePathAndFilename($this->fluidStandaloneService->getTemplatePath($template));
315 18
        $emailView->assignMultiple([
316 18
            'event' => $event,
317 18
            'registration' => $registration,
318 18
            'settings' => $settings,
319 18
            'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()),
320 18
            'reghmac' => $this->hashService->appendHmac((string)$registration->getUid())
321 18
        ]);
322 18
        $emailBody = $emailView->render();
323 18
        return $emailBody;
324
    }
325
}
326