Completed
Push — master ( c18e86...b01b7f )
by Torben
04:02
created

NotificationService::injectSignalSlotDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
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\Utility\MessageRecipient;
12
use DERHANSEN\SfEventMgt\Utility\MessageType;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
15
16
/**
17
 * NotificationService
18
 *
19
 * @author Torben Hansen <[email protected]>
20
 */
21
class NotificationService
22
{
23
    /**
24
     * The object manager
25
     *
26
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
27
     */
28
    protected $objectManager;
29
30
    /**
31
     * Registration repository
32
     *
33
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
34
     */
35
    protected $registrationRepository = null;
36
37
    /**
38
     * Email Service
39
     *
40
     * @var \DERHANSEN\SfEventMgt\Service\EmailService
41
     */
42
    protected $emailService;
43
44
    /**
45
     * Hash Service
46
     *
47
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
48
     */
49
    protected $hashService;
50
51
    /**
52
     * FluidStandaloneService
53
     *
54
     * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService
55
     */
56
    protected $fluidStandaloneService;
57
58
    /**
59
     * CustomNotificationLogRepository
60
     *
61
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository
62
     */
63
    protected $customNotificationLogRepository = null;
64
65
    /**
66
     * AttachmentService
67
     *
68
     * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService
69
     */
70
    protected $attachmentService;
71
72
    /**
73
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
74
     */
75
    protected $signalSlotDispatcher = null;
76
77
    /**
78
     * DI for $attachmentService
79
     *
80
     * @param Notification\AttachmentService $attachmentService
81
     */
82
    public function injectAttachmentService(
83
        \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService $attachmentService
84
    ) {
85
        $this->attachmentService = $attachmentService;
86
    }
87
88
    /**
89
     * DI for $customNotificationLogRepository
90
     *
91
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
92
     */
93
    public function injectCustomNotificationLogRepository(
94
        \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository
95 8
    ) {
96
        $this->customNotificationLogRepository = $customNotificationLogRepository;
97 8
    }
98 2
99
    /**
100 6
     * DI for $emailService
101
     *
102 6
     * @param EmailService $emailService
103 6
     */
104 6
    public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService)
105
    {
106 6
        $this->emailService = $emailService;
107 2
    }
108 2
109 2
    /**
110 2
     * DI for $fluidStandaloneService
111 2
     *
112
     * @param FluidStandaloneService $fluidStandaloneService
113 2
     */
114 2
    public function injectFluidStandaloneService(
115 2
        \DERHANSEN\SfEventMgt\Service\FluidStandaloneService $fluidStandaloneService
116 2
    ) {
117 2
        $this->fluidStandaloneService = $fluidStandaloneService;
118 6
    }
119 6
120
    /**
121
     * DI for $hashService
122
     *
123
     * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
124
     */
125
    public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
126
    {
127
        $this->hashService = $hashService;
128
    }
129
130
    /**
131 8
     * DI for $objectManager
132
     *
133 8
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
134
     */
135
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
136
    {
137
        $this->objectManager = $objectManager;
138
    }
139
140
    /**
141
     * DI for $registrationRepository
142
     *
143
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
144
     */
145 2
    public function injectRegistrationRepository(
146
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
147 2
    ) {
148 2
        $this->registrationRepository = $registrationRepository;
149 2
    }
150 2
151 2
    /**
152 2
     * DI for $signalSlotDispatcher
153 2
     *
154
     * @param Dispatcher $signalSlotDispatcher
155
     */
156
    public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
157
    {
158
        $this->signalSlotDispatcher = $signalSlotDispatcher;
159
    }
160
161
    /**
162
     * Sends a custom notification defined by the given customNotification key
163
     * to all confirmed users of the event
164
     *
165
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
166 34
     * @param string $customNotification CustomNotification
167
     * @param array $settings Settings
168 34
     *
169
     * @return int Number of notifications sent
170 34
     */
171 4
    public function sendCustomNotification($event, $customNotification, $settings)
172
    {
173
        if ($this->cantSendCustomNotification($event, $settings, $customNotification)) {
174 30
            return 0;
175 20
        }
176 20
        $count = 0;
177 20
178 20
        $constraints = $settings['notification']['customNotifications'][$customNotification]['constraints'];
179 20
        $registrations = $this->registrationRepository->findNotificationRegistrations($event, $constraints);
180
        foreach ($registrations as $registration) {
181 20
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
182 20
            if ($registration->isConfirmed() && !$registration->isIgnoreNotifications()) {
183 20
                $result = $this->sendUserMessage(
184 20
                    $event,
185 20
                    $registration,
186 20
                    $settings,
187 20
                    MessageType::CUSTOM_NOTIFICATION,
188
                    $customNotification
189 20
                );
190
                if ($result) {
191 10
                    $count += 1;
192
                }
193
            }
194
        }
195
196
        return $count;
197
    }
198
199
    /**
200
     * Returns true if conditions are not met to send a custom notification
201
     *
202 34
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
203
     * @param array $settings
204 34
     * @param string $customNotification
205 34
     *
206
     * @return bool
207 34
     */
208 6
    protected function cantSendCustomNotification($event, $settings, $customNotification)
209 6
    {
210 6
        return is_null($event) || $customNotification == '' || $settings == '' || !is_array($settings);
211 28
    }
212 6
213 6
    /**
214 6
     * Adds a logentry to the custom notification log
215 22
     *
216 6
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
217 6
     * @param string $details Details
218 6
     * @param int $emailsSent E-Mails sent
219 16
     *
220
     * @return void
221
     */
222
    public function createCustomNotificationLogentry($event, $details, $emailsSent)
223 16
    {
224 2
        $notificationlogEntry = new \DERHANSEN\SfEventMgt\Domain\Model\CustomNotificationLog();
225 2
        $notificationlogEntry->setEvent($event);
226 2
        $notificationlogEntry->setDetails($details);
227 14
        $notificationlogEntry->setEmailsSent($emailsSent);
228 14
        $notificationlogEntry->setCruserId($GLOBALS['BE_USER']->user['uid']);
229 14
        $this->customNotificationLogRepository->add($notificationlogEntry);
230
    }
231 34
232
    /**
233 34
     * Sends a message to the user based on the given type
234
     *
235
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
236
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
237
     * @param array $settings Settings
238
     * @param int $type Type
239
     * @param string $customNotification CustomNotification
240
     *
241
     * @return bool TRUE if successful, else FALSE
242
     */
243
    public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '')
244
    {
245
        list($template, $subject) = $this->getUserMessageTemplateSubject($settings, $type, $customNotification);
246 52
247
        if (is_null($event) || is_null($registration) || !is_array($settings) || (substr($template, -5) != '.html')) {
248 52
            return false;
249
        }
250
251 52
        if (!$registration->isIgnoreNotifications()) {
252 50
            $body = $this->getNotificationBody($event, $registration, $template, $settings);
253 52
            $subject = $this->fluidStandaloneService->parseStringFluid(
254 12
                $subject,
255
                [
256
                    'event' => $event,
257 40
                    'registration' => $registration
258 40
                ]
259 40
            );
260 40
            $attachments = $this->attachmentService->getAttachments(
261 40
                $settings,
262 40
                $registration,
263
                $type,
264 40
                MessageRecipient::USER
265 40
            );
266 30
267 30
            // Get iCal attachment if configured
268 30
            $iCalAttachment = $this->attachmentService->getICalAttachment(
269 30
                $settings,
270 30
                $registration,
271 30
                $type,
272 30
                MessageRecipient::USER
273 30
            );
274
275 30
            if ($iCalAttachment !== '') {
276 30
                $attachments[] = $iCalAttachment;
277 30
            }
278 40
279 10
            $senderName = $settings['notification']['senderName'];
280 10
            $senderEmail = $settings['notification']['senderEmail'];
281 10
            $replyToEmail = $settings['notification']['replyToEmail'];
282 10
283 10
            $this->signalSlotDispatcher->dispatch(
284 10
                __CLASS__,
285
                __FUNCTION__ . 'CustomSenderData',
286 10
                [&$senderName, &$senderEmail, &$replyToEmail, $registration, $type, $this]
287 10
            );
288 40
289
            $result = $this->emailService->sendEmailMessage(
290
                $senderEmail,
291
                $registration->getEmail(),
292
                $subject,
293
                $body,
294
                $senderName,
295
                $attachments,
296
                $replyToEmail
297
            );
298 52
299
            $this->signalSlotDispatcher->dispatch(
300 52
                __CLASS__,
301 52
                __FUNCTION__ . 'AfterNotificationSent',
302
                [$registration, $body, $subject, $attachments, $senderName, $senderEmail, $replyToEmail, $this]
303 52
            );
304 10
305 10
            // Cleanup iCal attachment if available
306 10
            if ($iCalAttachment !== '') {
307 42
                \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($iCalAttachment);
308 10
            }
309 10
310 10
            return $result;
311 32
        }
312 10
313 10
        return false;
314 10
    }
315 22
316
    /**
317
     * Returns an array with template and subject for the user message
318
     *
319 22
     * @param array $settings
320 22
     * @param int $type Type
321 22
     * @param string $customNotification
322
     * @return array
323 52
     */
324
    protected function getUserMessageTemplateSubject($settings, $type, $customNotification)
325 52
    {
326
        $template = 'Notification/User/RegistrationNew.html';
327
        $subject = $settings['notification']['registrationNew']['userSubject'];
328
        switch ($type) {
329
            case MessageType::REGISTRATION_WAITLIST_NEW:
330
                $template = 'Notification/User/RegistrationWaitlistNew.html';
331
                $subject = $settings['notification']['registrationWaitlistNew']['userSubject'];
332
                break;
333
            case MessageType::REGISTRATION_CONFIRMED:
334
                $template = 'Notification/User/RegistrationConfirmed.html';
335
                $subject = $settings['notification']['registrationConfirmed']['userSubject'];
336
                break;
337
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
338 60
                $template = 'Notification/User/RegistrationWaitlistConfirmed.html';
339
                $subject = $settings['notification']['registrationWaitlistConfirmed']['userSubject'];
340
                break;
341 60
            case MessageType::REGISTRATION_CANCELLED:
342 60
                $template = 'Notification/User/RegistrationCancelled.html';
343 60
                $subject = $settings['notification']['registrationCancelled']['userSubject'];
344 60
                break;
345
            case MessageType::CUSTOM_NOTIFICATION:
346 60
                $template = 'Notification/User/Custom/' . $settings['notification']['customNotifications'][$customNotification]['template'];
347
                $subject = $settings['notification']['customNotifications'][$customNotification]['subject'];
348
                break;
349
            case MessageType::REGISTRATION_NEW:
350
            default:
351
        }
352 60
353 60
        return [
354 60
            $template,
355 60
            $subject
356 60
        ];
357 60
    }
358 60
359 60
    /**
360 60
     * Sends a message to the admin based on the given type
361 60
     *
362 60
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
363 60
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
364
     * @param array $settings Settings
365
     * @param int $type Type
366
     *
367
     * @return bool TRUE if successful, else FALSE
368
     */
369
    public function sendAdminMessage($event, $registration, $settings, $type)
370
    {
371
        list($template, $subject) = $this->getAdminMessageTemplateSubject($settings, $type);
372
373
        if (is_null($event) || is_null($registration || !is_array($settings)) ||
374
            ($event->getNotifyAdmin() === false && $event->getNotifyOrganisator() === false)
375
        ) {
376
            return false;
377
        }
378
379
        $allEmailsSent = true;
380
        $body = $this->getNotificationBody($event, $registration, $template, $settings);
381
        $subject = $this->fluidStandaloneService->parseStringFluid(
382
            $subject,
383
            [
384
                'event' => $event,
385
                'registration' => $registration
386
            ]
387
        );
388
        $attachments = $this->attachmentService->getAttachments(
389
            $settings,
390
            $registration,
391
            $type,
392
            MessageRecipient::ADMIN
393
        );
394
395
        $senderName = $settings['notification']['senderName'];
396
        $senderEmail = $settings['notification']['senderEmail'];
397
        if ((bool)$settings['notification']['registrationDataAsSenderForAdminEmails']) {
398
            $senderName = $registration->getFullname();
399
            $senderEmail = $registration->getEmail();
400
        }
401
402
        if ($event->getNotifyAdmin()) {
403
            $adminEmailArr = GeneralUtility::trimExplode(',', $settings['notification']['adminEmail'], true);
404
            foreach ($adminEmailArr as $adminEmail) {
405
                $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
406
                    $senderEmail,
407
                    $adminEmail,
408
                    $subject,
409
                    $body,
410
                    $senderName,
411
                    $attachments
412
                );
413
            }
414
        }
415
416
        if ($event->getNotifyOrganisator() && $event->getOrganisator()) {
417
            $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
418
                $senderEmail,
419
                $event->getOrganisator()->getEmail(),
420
                $subject,
421
                $body,
422
                $senderName,
423
                $attachments
424
            );
425
        }
426
427
        $this->signalSlotDispatcher->dispatch(
428
            __CLASS__,
429
            __FUNCTION__ . 'AfterNotificationSent',
430
            [$registration, $body, $subject, $attachments, $senderName, $senderEmail, $this]
431
        );
432
433
        return $allEmailsSent;
434
    }
435
436
    /**
437
     * Returns an array with template and subject for the admin message
438
     *
439
     * @param array $settings
440
     * @param int $type Type
441
     * @return array
442
     */
443
    protected function getAdminMessageTemplateSubject($settings, $type)
444
    {
445
        $template = 'Notification/Admin/RegistrationNew.html';
446
        $subject = $settings['notification']['registrationNew']['adminSubject'];
447
        switch ($type) {
448
            case MessageType::REGISTRATION_WAITLIST_NEW:
449
                $template = 'Notification/Admin/RegistrationWaitlistNew.html';
450
                $subject = $settings['notification']['registrationWaitlistNew']['adminSubject'];
451
                break;
452
            case MessageType::REGISTRATION_CONFIRMED:
453
                $template = 'Notification/Admin/RegistrationConfirmed.html';
454
                $subject = $settings['notification']['registrationConfirmed']['adminSubject'];
455
                break;
456
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
457
                $template = 'Notification/Admin/RegistrationWaitlistConfirmed.html';
458
                $subject = $settings['notification']['registrationWaitlistConfirmed']['adminSubject'];
459
                break;
460
            case MessageType::REGISTRATION_CANCELLED:
461
                $template = 'Notification/Admin/RegistrationCancelled.html';
462
                $subject = $settings['notification']['registrationCancelled']['adminSubject'];
463
                break;
464
            case MessageType::REGISTRATION_NEW:
465
            default:
466
        }
467
468
        return [
469
            $template,
470
            $subject
471
        ];
472
    }
473
474
    /**
475
     * Returns the rendered HTML for the given template
476
     *
477
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
478
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
479
     * @param string $template Template
480
     * @param array $settings Settings
481
     *
482
     * @return string
483
     */
484
    protected function getNotificationBody($event, $registration, $template, $settings)
485
    {
486
        if (TYPO3_MODE === 'BE' && $registration->getLanguage() !== '') {
487
            // Temporary set Language of current BE user to given language
488
            $GLOBALS['BE_USER']->uc['lang'] = $registration->getLanguage();
489
        }
490
        $variables = [
491
            'event' => $event,
492
            'registration' => $registration,
493
            'settings' => $settings,
494
            'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()),
495
            'reghmac' => $this->hashService->appendHmac((string)$registration->getUid())
496
        ];
497
498
        return $this->fluidStandaloneService->renderTemplate($template, $variables);
499
    }
500
}
501