Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

getUserMessageTemplateSubject()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

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