Completed
Push — development ( 20d4b5...1fcece )
by Torben
07:08
created

NotificationService::injectObjectManager()   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 4
    ) {
96
        $this->customNotificationLogRepository = $customNotificationLogRepository;
97 4
    }
98 1
99
    /**
100 3
     * DI for $emailService
101
     *
102 3
     * @param EmailService $emailService
103 3
     */
104 3
    public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService)
105
    {
106 3
        $this->emailService = $emailService;
107 1
    }
108 1
109 1
    /**
110 1
     * DI for $fluidStandaloneService
111 1
     *
112
     * @param FluidStandaloneService $fluidStandaloneService
113 1
     */
114 1
    public function injectFluidStandaloneService(
115 1
        \DERHANSEN\SfEventMgt\Service\FluidStandaloneService $fluidStandaloneService
116 1
    ) {
117 1
        $this->fluidStandaloneService = $fluidStandaloneService;
118 3
    }
119 3
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 4
     * DI for $objectManager
132
     *
133 4
     * @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 1
    public function injectRegistrationRepository(
146
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
147 1
    ) {
148 1
        $this->registrationRepository = $registrationRepository;
149 1
    }
150 1
151 1
    /**
152 1
     * DI for $signalSlotDispatcher
153 1
     *
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 17
     * @param string $customNotification CustomNotification
167
     * @param array $settings Settings
168 17
     *
169
     * @return int Number of notifications sent
170 17
     */
171 2
    public function sendCustomNotification($event, $customNotification, $settings)
172
    {
173
        if ($this->cantSendCustomNotification($event, $settings, $customNotification)) {
174 15
            return 0;
175 10
        }
176 10
        $count = 0;
177 10
178 10
        $constraints = $settings['notification']['customNotifications'][$customNotification]['constraints'];
179 10
        $registrations = $this->registrationRepository->findNotificationRegistrations($event, $constraints);
180
        foreach ($registrations as $registration) {
181 10
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
182 10
            if ($registration->isConfirmed() && !$registration->isIgnoreNotifications()) {
183 10
                $result = $this->sendUserMessage(
184 10
                    $event,
185 10
                    $registration,
186 10
                    $settings,
187 10
                    MessageType::CUSTOM_NOTIFICATION,
188
                    $customNotification
189 10
                );
190
                if ($result) {
191 5
                    $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 17
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
203
     * @param array $settings
204 17
     * @param string $customNotification
205 17
     *
206
     * @return bool
207 17
     */
208 3
    protected function cantSendCustomNotification($event, $settings, $customNotification)
209 3
    {
210 3
        return is_null($event) || $customNotification == '' || $settings == '' || !is_array($settings);
211 14
    }
212 3
213 3
    /**
214 3
     * Adds a logentry to the custom notification log
215 11
     *
216 3
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
217 3
     * @param string $details Details
218 3
     * @param int $emailsSent E-Mails sent
219 8
     *
220
     * @return void
221
     */
222
    public function createCustomNotificationLogentry($event, $details, $emailsSent)
223 8
    {
224 1
        $notificationlogEntry = new \DERHANSEN\SfEventMgt\Domain\Model\CustomNotificationLog();
225 1
        $notificationlogEntry->setEvent($event);
226 1
        $notificationlogEntry->setDetails($details);
227 7
        $notificationlogEntry->setEmailsSent($emailsSent);
228 7
        $notificationlogEntry->setCruserId($GLOBALS['BE_USER']->user['uid']);
229 7
        $this->customNotificationLogRepository->add($notificationlogEntry);
230
    }
231 17
232
    /**
233 17
     * 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 26
247
        if (is_null($event) || is_null($registration) || !is_array($settings) || (substr($template, -5) != '.html')) {
248 26
            return false;
249
        }
250
251 26
        if (!$registration->isIgnoreNotifications()) {
252 25
            $body = $this->getNotificationBody($event, $registration, $template, $settings);
253 26
            $subject = $this->fluidStandaloneService->parseStringFluid(
254 6
                $subject,
255
                [
256
                    'event' => $event,
257 20
                    'registration' => $registration
258 20
                ]
259 20
            );
260 20
            $attachments = $this->attachmentService->getAttachments(
261 20
                $settings,
262 20
                $registration,
263
                $type,
264 20
                MessageRecipient::USER
265 20
            );
266 15
267 15
            // Get iCal attachment if configured
268 15
            $iCalAttachment = $this->attachmentService->getICalAttachment(
269 15
                $settings,
270 15
                $registration,
271 15
                $type,
272 15
                MessageRecipient::USER
273 15
            );
274
275 15
            if ($iCalAttachment !== '') {
276 15
                $attachments[] = $iCalAttachment;
277 15
            }
278 20
279 5
            $result = $this->emailService->sendEmailMessage(
280 5
                $settings['notification']['senderEmail'],
281 5
                $registration->getEmail(),
282 5
                $subject,
283 5
                $body,
284 5
                $settings['notification']['senderName'],
285
                $attachments
286 5
            );
287 5
288 20
            // Cleanup iCal attachment if available
289
            if ($iCalAttachment !== '') {
290
                \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($iCalAttachment);
291
            }
292
293
            return $result;
294
        }
295
296
        return false;
297
    }
298 26
299
    /**
300 26
     * Returns an array with template and subject for the user message
301 26
     *
302
     * @param array $settings
303 26
     * @param int $type Type
304 5
     * @param string $customNotification
305 5
     * @return array
306 5
     */
307 21
    protected function getUserMessageTemplateSubject($settings, $type, $customNotification)
308 5
    {
309 5
        $template = 'Notification/User/RegistrationNew.html';
310 5
        $subject = $settings['notification']['registrationNew']['userSubject'];
311 16
        switch ($type) {
312 5
            case MessageType::REGISTRATION_WAITLIST_NEW:
313 5
                $template = 'Notification/User/RegistrationWaitlistNew.html';
314 5
                $subject = $settings['notification']['registrationWaitlistNew']['userSubject'];
315 11
                break;
316
            case MessageType::REGISTRATION_CONFIRMED:
317
                $template = 'Notification/User/RegistrationConfirmed.html';
318
                $subject = $settings['notification']['registrationConfirmed']['userSubject'];
319 11
                break;
320 11
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
321 11
                $template = 'Notification/User/RegistrationWaitlistConfirmed.html';
322
                $subject = $settings['notification']['registrationWaitlistConfirmed']['userSubject'];
323 26
                break;
324
            case MessageType::REGISTRATION_CANCELLED:
325 26
                $template = 'Notification/User/RegistrationCancelled.html';
326
                $subject = $settings['notification']['registrationCancelled']['userSubject'];
327
                break;
328
            case MessageType::CUSTOM_NOTIFICATION:
329
                $template = 'Notification/User/Custom/' . $settings['notification']['customNotifications'][$customNotification]['template'];
330
                $subject = $settings['notification']['customNotifications'][$customNotification]['subject'];
331
                break;
332
            case MessageType::REGISTRATION_NEW:
333
            default:
334
        }
335
336
        return [
337
            $template,
338 30
            $subject
339
        ];
340
    }
341 30
342 30
    /**
343 30
     * Sends a message to the admin based on the given type
344 30
     *
345
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
346 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
347
     * @param array $settings Settings
348
     * @param int $type Type
349
     *
350
     * @return bool TRUE if successful, else FALSE
351
     */
352 30
    public function sendAdminMessage($event, $registration, $settings, $type)
353 30
    {
354 30
        list($template, $subject) = $this->getAdminMessageTemplateSubject($settings, $type);
355 30
356 30
        if (is_null($event) || is_null($registration || !is_array($settings)) ||
357 30
            ($event->getNotifyAdmin() === false && $event->getNotifyOrganisator() === false)
358 30
        ) {
359 30
            return false;
360 30
        }
361 30
362 30
        $allEmailsSent = true;
363 30
        $body = $this->getNotificationBody($event, $registration, $template, $settings);
364
        $subject = $this->fluidStandaloneService->parseStringFluid(
365
            $subject,
366
            [
367
                'event' => $event,
368
                'registration' => $registration
369
            ]
370
        );
371
        $attachments = $this->attachmentService->getAttachments(
372
            $settings,
373
            $registration,
374
            $type,
375
            MessageRecipient::ADMIN
376
        );
377
378
        $senderName = $settings['notification']['senderName'];
379
        $senderEmail = $settings['notification']['senderEmail'];
380
        if ((bool)$settings['notification']['registrationDataAsSenderForAdminEmails']) {
381
            $senderName = $registration->getFullname();
382
            $senderEmail = $registration->getEmail();
383
        }
384
385
        if ($event->getNotifyAdmin()) {
386
            $adminEmailArr = GeneralUtility::trimExplode(',', $settings['notification']['adminEmail'], true);
387
            foreach ($adminEmailArr as $adminEmail) {
388
                $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
389
                    $senderEmail,
390
                    $adminEmail,
391
                    $subject,
392
                    $body,
393
                    $senderName,
394
                    $attachments
395
                );
396
            }
397
        }
398
399
        if ($event->getNotifyOrganisator() && $event->getOrganisator()) {
400
            $allEmailsSent = $allEmailsSent && $this->emailService->sendEmailMessage(
401
                $senderEmail,
402
                $event->getOrganisator()->getEmail(),
403
                $subject,
404
                $body,
405
                $senderName,
406
                $attachments
407
            );
408
        }
409
410
        $this->signalSlotDispatcher->dispatch(
411
            __CLASS__,
412
            __FUNCTION__ . 'AfterNotificationsSent',
413
            [$registration, $body, $subject, $attachments, $senderName, $senderEmail, $this]
414
        );
415
416
        return $allEmailsSent;
417
    }
418
419
    /**
420
     * Returns an array with template and subject for the admin message
421
     *
422
     * @param array $settings
423
     * @param int $type Type
424
     * @return array
425
     */
426
    protected function getAdminMessageTemplateSubject($settings, $type)
427
    {
428
        $template = 'Notification/Admin/RegistrationNew.html';
429
        $subject = $settings['notification']['registrationNew']['adminSubject'];
430
        switch ($type) {
431
            case MessageType::REGISTRATION_WAITLIST_NEW:
432
                $template = 'Notification/Admin/RegistrationWaitlistNew.html';
433
                $subject = $settings['notification']['registrationWaitlistNew']['adminSubject'];
434
                break;
435
            case MessageType::REGISTRATION_CONFIRMED:
436
                $template = 'Notification/Admin/RegistrationConfirmed.html';
437
                $subject = $settings['notification']['registrationConfirmed']['adminSubject'];
438
                break;
439
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
440
                $template = 'Notification/Admin/RegistrationWaitlistConfirmed.html';
441
                $subject = $settings['notification']['registrationWaitlistConfirmed']['adminSubject'];
442
                break;
443
            case MessageType::REGISTRATION_CANCELLED:
444
                $template = 'Notification/Admin/RegistrationCancelled.html';
445
                $subject = $settings['notification']['registrationCancelled']['adminSubject'];
446
                break;
447
            case MessageType::REGISTRATION_NEW:
448
            default:
449
        }
450
451
        return [
452
            $template,
453
            $subject
454
        ];
455
    }
456
457
    /**
458
     * Returns the rendered HTML for the given template
459
     *
460
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
461
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
462
     * @param string $template Template
463
     * @param array $settings Settings
464
     *
465
     * @return string
466
     */
467
    protected function getNotificationBody($event, $registration, $template, $settings)
468
    {
469
        if (TYPO3_MODE === 'BE' && $registration->getLanguage() !== '') {
470
            // Temporary set Language of current BE user to given language
471
            $GLOBALS['BE_USER']->uc['lang'] = $registration->getLanguage();
472
        }
473
        $variables = [
474
            'event' => $event,
475
            'registration' => $registration,
476
            'settings' => $settings,
477
            'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()),
478
            'reghmac' => $this->hashService->appendHmac((string)$registration->getUid())
479
        ];
480
481
        return $this->fluidStandaloneService->renderTemplate($template, $variables);
482
    }
483
}
484