Passed
Pull Request — master (#195)
by
unknown
19:02
created

Notifier   F

Complexity

Total Complexity 77

Size/Duplication

Total Lines 672
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 329
c 4
b 2
f 0
dl 0
loc 672
rs 2.24
wmc 77

15 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSuggestionDeclineNotification() 0 26 4
A sendChangedDocumentNotification() 0 26 4
A sendEmbargoNotification() 0 36 5
B sendMyPublicationUpdateNotification() 0 44 6
B getMailMarkerArray() 0 81 8
A sendAdminNewSuggestionNotification() 0 36 5
A replaceMarkers() 0 8 3
A sendSuggestionAcceptNotification() 0 26 4
B sendMyPublicationNewNotification() 0 44 6
B sendDepositLicenseNotification() 0 38 6
A sendRegisterNotification() 0 37 5
A sendAdminEmbargoExpiredNotification() 0 36 5
A sendIngestNotification() 0 36 5
C sendNewDocumentNotification() 0 70 10
A sendMail() 0 10 1

How to fix   Complexity   

Complex Class

Complex classes like Notifier often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Notifier, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace EWW\Dpf\Services\Email;
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 EWW\Dpf\Domain\Model\Document;
18
use \TYPO3\CMS\Core\Log\LogLevel;
19
use \TYPO3\CMS\Core\Log\LogManager;
20
use \TYPO3\CMS\Core\Utility\GeneralUtility;
21
use EWW\Dpf\Domain\Model\FrontendUser;
22
use EWW\Dpf\Domain\Model\Client;
23
use \Httpful\Request;
24
25
class Notifier
26
{
27
28
    /**
29
     * clientRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
32
     * @inject
33
     */
34
    protected $clientRepository = null;
35
36
    /**
37
     * documentTypeRepository
38
     *
39
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
40
     * @inject
41
     */
42
    protected $documentTypeRepository = null;
43
44
    /**
45
     * depositLicenseRepository
46
     *
47
     * @var \EWW\Dpf\Domain\Repository\DepositLicenseRepository
48
     * @inject
49
     */
50
    protected $depositLicenseRepository = null;
51
52
    /**
53
     * security
54
     *
55
     * @var \EWW\Dpf\Security\Security
56
     * @inject
57
     */
58
    protected $security = null;
59
60
61
    public function sendAdminNewSuggestionNotification(\EWW\Dpf\Domain\Model\Document $document) {
62
        try {
63
            /** @var $client \EWW\Dpf\Domain\Model\Client */
64
            $client = $this->clientRepository->findAll()->current();
65
            $clientAdminEmail = $client->getAdminEmail();
66
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
0 ignored issues
show
Bug introduced by
The method findOneByUid() does not exist on EWW\Dpf\Domain\Repository\DocumentTypeRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            /** @scrutinizer ignore-call */ 
67
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
Loading history...
67
68
            $args = $this->getMailMarkerArray($document, $client, $documentType);
69
70
            // Notify client admin
71
            if ($clientAdminEmail) {
72
                $subject = $client->getAdminNewSuggestionSubject();
73
                $body = $client->getAdminNewSuggestionBody();
74
                $mailType = 'text/html';
75
76
                if (empty($subject)) {
77
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.subject', 'dpf');
78
                }
79
80
                if (empty($body)) {
81
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newSuggestion.admin.body', 'dpf');
82
                    $mailType = 'text/plain';
83
                }
84
85
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
86
87
            }
88
89
        } catch (\Exception $e) {
90
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
91
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
92
93
            $logger->log(
94
                LogLevel::ERROR, "sendAdminNewSuggestionNotification failed",
95
                array(
96
                    'document' => $document
97
                )
98
            );
99
        }
100
    }
101
102
103
    public function sendAdminEmbargoExpiredNotification(\EWW\Dpf\Domain\Model\Document $document) {
104
        try {
105
            /** @var $client \EWW\Dpf\Domain\Model\Client */
106
            $client = $this->clientRepository->findAll()->current();
107
            $clientAdminEmail = $client->getAdminEmail();
108
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
109
110
            $args = $this->getMailMarkerArray($document, $client, $documentType);
111
112
            // Notify client admin
113
            if ($clientAdminEmail) {
114
                $subject = $client->getAdminEmbargoSubject();
115
                $body = $client->getAdminEmbargoBody();
116
                $mailType = 'text/html';
117
118
                if (empty($subject)) {
119
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.subject', 'dpf');
120
                }
121
122
                if (empty($body)) {
123
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.embargoExpired.admin.body', 'dpf');
124
                    $mailType = 'text/plain';
125
                }
126
127
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
128
129
            }
130
131
        } catch (\Exception $e) {
132
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
133
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
134
135
            $logger->log(
136
                LogLevel::ERROR, "sendAdminEmbargoExpiredNotification failed",
137
                array(
138
                    'document' => $document
139
                )
140
            );
141
        }
142
    }
143
144
145
    public function getMailMarkerArray(Document $document, $client, $documentType) {
146
147
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
148
149
        /** @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager */
150
        $configurationManager = $objectManager->get('TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface');
151
        $settings = $configurationManager->getConfiguration(
152
            \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT,
153
            'dpf',
154
            'backoffice'
155
        );
156
157
        $args['###CLIENT###'] = $client->getClient();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.
Loading history...
158
        $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
159
160
        $args['###DOCUMENT_IDENTIFIER###'] = $document->getObjectIdentifier();
161
162
        if ($documentType) {
163
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
164
        } else {
165
            $args['###DOCUMENT_TYPE###'] = '';
166
        }
167
168
        $args['###TITLE###'] = $document->getTitle();
169
170
        $author = array_shift($document->getAuthors());
0 ignored issues
show
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
        $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
171
        $args['###AUTHOR###'] = $author['name'];
172
173
        $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
174
        $args['###SUBMITTER_NAME###'] = $internalFormat->getSubmitterName();
175
        $args['###SUBMITTER_EMAIL###'] = $internalFormat->getSubmitterEmail();
176
        $args['###SUBMITTER_NOTICE###'] = $internalFormat->getSubmitterNotice();
177
178
        $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
179
        $args['###URN###'] = $internalFormat->getQucosaUrn();
180
        $args['###URL###'] = 'http://nbn-resolving.de/' . $internalFormat->getQucosaUrn();
0 ignored issues
show
Bug introduced by
Are you sure $internalFormat->getQucosaUrn() of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

180
        $args['###URL###'] = 'http://nbn-resolving.de/' . /** @scrutinizer ignore-type */ $internalFormat->getQucosaUrn();
Loading history...
181
182
        $host = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
183
        $backofficePageId = $settings['plugin.']['tx_dpf.']['settings.']['backofficePluginPage'];
184
185
        /** @var \EWW\Dpf\Domain\Model\DepositLicense $depositLicense */
186
        $depositLicense = $this->depositLicenseRepository->findOneByUri($document->getDepositLicense());
0 ignored issues
show
Bug introduced by
The method findOneByUri() does not exist on EWW\Dpf\Domain\Repository\DepositLicenseRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        /** @scrutinizer ignore-call */ 
187
        $depositLicense = $this->depositLicenseRepository->findOneByUri($document->getDepositLicense());
Loading history...
187
        if ($depositLicense instanceof \EWW\Dpf\Domain\Model\DepositLicense) {
0 ignored issues
show
introduced by
$depositLicense is always a sub-type of EWW\Dpf\Domain\Model\DepositLicense.
Loading history...
188
            $args['###LICENSE_URI###'] = $depositLicense->getUri();
189
            $args['###LICENSE_TEXT###'] = $depositLicense->getText();
190
        }
191
192
        $args['###LICENSE_USERNAME###'] = $this->security->getUser()->getUsername();
193
194
        if ($document->isSuggestion()) {
195
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
196
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $document->getUid();
197
            $detailUrl .= '&tx_dpf_backoffice[action]=showSuggestionDetails';
198
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Änderungsvorschlag</a>';
199
        } else {
200
            $documentIdentifier = $document->getProcessNumber();
201
            if (empty($documentIdentifier)) {
202
                $documentIdentifier = $document->getDocumentIdentifier();
203
            }
204
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
205
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $documentIdentifier;
206
            $detailUrl .= '&tx_dpf_backoffice[action]=showDetails';
207
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Dokument</a>';
208
        }
209
210
        $args['###DETAIL_URL###'] = $detailUrl;
211
212
        $args['###HAS_FILES###'] = 'Metadata only';
213
214
        if ($document->getFileData()) {
215
            $args['###HAS_FILES###'] = 'Attachment';
216
            $fileList = [];
217
            foreach ($document->getFile() as $file) {
218
                if (!$file->isFileGroupDeleted()) {
219
                    $fileList[] = $file->getTitle();
220
                }
221
            }
222
            $args['###FILE_LIST###'] .= implode(", ", $fileList);
223
        }
224
225
        return $args;
226
    }
227
228
    public function sendSuggestionAcceptNotification(\EWW\Dpf\Domain\Model\Document $document) {
229
230
        try {
231
            /** @var Client $client */
232
            $client = $this->clientRepository->findAll()->current();
233
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
234
235
            $args = $this->getMailMarkerArray($document, $client, $documentType);
236
237
            // Active messaging: Suggestion accept
238
            if ($client->getActiveMessagingSuggestionAcceptUrl()) {
239
                $request = Request::post($client->getActiveMessagingSuggestionAcceptUrl());
240
                if ($body = $client->getActiveMessagingSuggestionAcceptUrlBody()) {
241
                    $request->body($this->replaceMarkers($body,$args));
242
                }
243
                $request->send();
244
            }
245
246
        } catch (\Exception $e) {
247
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
248
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
249
250
            $logger->log(
251
                LogLevel::ERROR, "sendSuggestionAcceptNotification failed",
252
                array(
253
                    'document' => $document
254
                )
255
            );
256
        }
257
    }
258
259
    public function sendSuggestionDeclineNotification(\EWW\Dpf\Domain\Model\Document $document) {
260
261
        try {
262
            /** @var Client $client */
263
            $client = $this->clientRepository->findAll()->current();
264
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
265
266
            $args = $this->getMailMarkerArray($document, $client, $documentType);
267
268
            // Active messaging: Suggestion accept
269
            if ($client->getActiveMessagingSuggestionDeclineUrl()) {
270
                $request = Request::post($client->getActiveMessagingSuggestionDeclineUrl());
271
                if ($body = $client->getActiveMessagingSuggestionDeclineUrlBody()) {
272
                    $request->body($this->replaceMarkers($body,$args));
273
                }
274
                $request->send();
275
            }
276
277
        } catch (\Exception $e) {
278
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
279
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
280
281
            $logger->log(
282
                LogLevel::ERROR, "sendSuggestionDeclineNotification failed",
283
                array(
284
                    'document' => $document
285
                )
286
            );
287
        }
288
    }
289
290
    public function sendChangedDocumentNotification(\EWW\Dpf\Domain\Model\Document $document) {
291
292
        try {
293
            /** @var Client $client */
294
            $client = $this->clientRepository->findAll()->current();
295
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
296
297
            $args = $this->getMailMarkerArray($document, $client, $documentType);
298
299
            // Active messaging: Suggestion accept
300
            if ($client->getActiveMessagingChangedDocumentUrl()) {
301
                $request = Request::post($client->getActiveMessagingChangedDocumentUrl());
302
                if ($body = $client->getActiveMessagingChangedDocumentUrlBody()) {
303
                    $request->body($this->replaceMarkers($body,$args));
304
                }
305
                $request->send();
306
            }
307
308
        } catch (\Exception $e) {
309
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
310
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
311
312
            $logger->log(
313
                LogLevel::ERROR, "sendChangedDocumentNotification failed",
314
                array(
315
                    'document' => $document
316
                )
317
            );
318
        }
319
    }
320
321
    public function sendNewDocumentNotification(\EWW\Dpf\Domain\Model\Document $document)
322
    {
323
324
        try {
325
            /** @var Client $client */
326
            $client = $this->clientRepository->findAll()->current();
327
            $clientAdminEmail = $client->getAdminEmail();
328
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
329
            $submitterEmail = $internalFormat->getSubmitterEmail();
330
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
331
            $authors = $document->getAuthors();
0 ignored issues
show
Unused Code introduced by
The assignment to $authors is dead and can be removed.
Loading history...
332
333
            $args = $this->getMailMarkerArray($document, $client, $documentType);
334
335
            // Notify client admin
336
            if ($clientAdminEmail) {
337
                $subject = $client->getAdminNewDocumentNotificationSubject();
338
                $body = $client->getAdminNewDocumentNotificationBody();
339
                $mailType = 'text/html';
340
341
                if (empty($subject)) {
342
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.subject', 'dpf');
343
                }
344
345
                if (empty($body)) {
346
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.body', 'dpf');
347
                    $mailType = 'text/plain';
348
                }
349
350
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
351
352
            }
353
354
355
            // Notify submitter
356
            if ($submitterEmail) {
357
                $subject = $client->getSubmitterNewDocumentNotificationSubject();
358
                $body = $client->getSubmitterNewDocumentNotificationBody();
359
                $mailType = 'text/html';
360
361
                if (empty($subject)) {
362
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.subject', 'dpf');
363
                }
364
365
                if (empty($body)) {
366
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.body', 'dpf');
367
                    $mailType = 'text/plain';
368
                }
369
370
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
371
            }
372
373
            // Active messaging: New document
374
            if ($client->getActiveMessagingNewDocumentUrl()) {
375
                $request = Request::post($client->getActiveMessagingNewDocumentUrl());
376
                if ($body = $client->getActiveMessagingNewDocumentUrlBody()) {
377
                    $request->body($this->replaceMarkers($body,$args));
378
                }
379
                $request->send();
380
            }
381
382
383
        } catch (\Exception $e) {
384
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
385
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
386
387
            $logger->log(
388
                LogLevel::ERROR, "sendNewDocumentNotification failed",
389
                array(
390
                    'document' => $document
391
                )
392
            );
393
        }
394
395
    }
396
397
    public function sendIngestNotification(\EWW\Dpf\Domain\Model\Document $document)
398
    {
399
400
        try {
401
            $client = $this->clientRepository->findAll()->current();
402
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
403
            $submitterEmail = $internalFormat->getSubmitterEmail();
404
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
405
406
            $args = $this->getMailMarkerArray($document, $client, $documentType);
407
408
            // Notify submitter
409
            if ($submitterEmail) {
410
                $subject = $client->getSubmitterIngestNotificationSubject();
411
                $body = $client->getSubmitterIngestNotificationBody();
412
                $mailType = 'text/html';
413
414
                if (empty($subject)) {
415
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.subject', 'dpf');
416
                }
417
418
                if (empty($body)) {
419
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.body', 'dpf');
420
                    $mailType = 'text/plain';
421
                }
422
423
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
424
            }
425
        } catch (\Exception $e) {
426
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
427
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
428
429
            $logger->log(
430
                LogLevel::ERROR, "sendIngestNotification failed",
431
                array(
432
                    'document' => $document
433
                )
434
            );
435
        }
436
437
    }
438
439
    public function sendEmbargoNotification(\EWW\Dpf\Domain\Model\Document $document) {
440
        try {
441
            $client = $this->clientRepository->findAllByPid($document->getPid())->current();
442
            $clientAdminEmail = $client->getAdminEmail();
443
444
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
445
446
            $args = $this->getMailMarkerArray($document, $client, $documentType);
447
448
            // Notify client admin
449
            if ($clientAdminEmail) {
450
                $subject = $client->getAdminEmbargoSubject();
451
                $body = $client->getAdminEmbargoBody();
452
                $mailType = 'text/html';
453
454
                if (empty($subject)) {
455
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.subject', 'dpf');
456
                }
457
458
                if (empty($body)) {
459
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.body', 'dpf');
460
                    $mailType = 'text/plain';
461
                }
462
463
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
464
465
            }
466
467
        } catch (\Exception $e) {
468
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
469
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
470
471
            $logger->log(
472
                LogLevel::ERROR, "sendRegisterNotification failed",
473
                array(
474
                    'document' => $document
475
                )
476
            );
477
        }
478
    }
479
480
    public function sendRegisterNotification(\EWW\Dpf\Domain\Model\Document $document)
481
    {
482
483
        try {
484
            $client = $this->clientRepository->findAll()->current();
485
            $clientAdminEmail = $client->getAdminEmail();
486
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
487
488
            $args = $this->getMailMarkerArray($document, $client, $documentType);
489
490
            // Notify client admin
491
            if ($clientAdminEmail) {
492
                $subject = $client->getAdminRegisterDocumentNotificationSubject();
493
                $body = $client->getAdminRegisterDocumentNotificationBody();
494
                $mailType = 'text/html';
495
496
                if (empty($subject)) {
497
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.subject', 'dpf');
498
                }
499
500
                if (empty($body)) {
501
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.body', 'dpf');
502
                    $mailType = 'text/plain';
503
                }
504
505
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
506
507
            }
508
509
        } catch (\Exception $e) {
510
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
511
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
512
513
            $logger->log(
514
                LogLevel::ERROR, "sendRegisterNotification failed",
515
                array(
516
                    'document' => $document
517
                )
518
            );
519
        }
520
521
    }
522
523
    /**
524
     * @param \EWW\Dpf\Domain\Model\Document $document
525
     * @param array $recipients
526
     */
527
    public function sendMyPublicationUpdateNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
528
    {
529
530
        try {
531
            /** @var Client $client */
532
            $client = $this->clientRepository->findAll()->current();
533
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
534
            $author = array_shift($document->getAuthors());
0 ignored issues
show
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

534
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
535
536
            $args = $this->getMailMarkerArray($document, $client, $documentType);
537
538
            // Notify client admin
539
            /** @var FrontendUser $recipient */
540
            foreach ($recipients as $recipient) {
541
542
                if ($recipient->getEmail()) {
543
544
                    $subject = $client->getMypublicationsUpdateNotificationSubject();
545
                    $body = $client->getMypublicationsUpdateNotificationBody();
546
                    $mailType = 'text/html';
547
548
                    if (empty($subject)) {
549
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.subject',
550
                            'dpf');
551
                    }
552
553
                    if (empty($body)) {
554
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.body',
555
                            'dpf');
556
                        $mailType = 'text/plain';
557
                    }
558
559
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
560
                }
561
            }
562
563
        } catch (\Exception $e) {
564
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
565
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
566
567
            $logger->log(
568
                LogLevel::ERROR, "sendRegisterNotification failed",
569
                array(
570
                    'document' => $document
571
                )
572
            );
573
        }
574
575
    }
576
577
578
    /**
579
     * @param \EWW\Dpf\Domain\Model\Document $document
580
     * @param array $recipients
581
     */
582
    public function sendMyPublicationNewNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
583
    {
584
585
        try {
586
            /** @var Client $client */
587
            $client = $this->clientRepository->findAll()->current();
588
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
589
            $author = array_shift($document->getAuthors());
0 ignored issues
show
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
Bug introduced by
$document->getAuthors() cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

589
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
590
591
            $args = $this->getMailMarkerArray($document, $client, $documentType);
592
593
            // Notify client admin
594
            /** @var FrontendUser $recipient */
595
            foreach ($recipients as $recipient) {
596
597
                if ($recipient->getEmail()) {
598
599
                    $subject = $client->getMypublicationsNewNotificationSubject();
600
                    $body = $client->getMypublicationsNewNotificationBody();
601
                    $mailType = 'text/html';
602
603
                    if (empty($subject)) {
604
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.subject',
605
                            'dpf');
606
                    }
607
608
                    if (empty($body)) {
609
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.body',
610
                            'dpf');
611
                        $mailType = 'text/plain';
612
                    }
613
614
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
615
                }
616
            }
617
618
        } catch (\Exception $e) {
619
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
620
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
621
622
            $logger->log(
623
                LogLevel::ERROR, "sendRegisterNotification failed",
624
                array(
625
                    'document' => $document
626
                )
627
            );
628
        }
629
630
    }
631
632
    public function sendDepositLicenseNotification(\EWW\Dpf\Domain\Model\Document $document)
633
    {
634
635
        try {
636
            /** @var Client $client */
637
            $client = $this->clientRepository->findAll()->current();
638
            $clientAdminEmail = $client->getAdminEmail();
639
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
640
641
            $args = $this->getMailMarkerArray($document, $client, $documentType);
642
643
            // Notify client admin
644
            if ($clientAdminEmail && $client->isSendAdminDepositLicenseNotification()) {
645
646
                $subject = $client->getAdminDepositLicenseNotificationSubject();
647
                $body = $client->getAdminDepositLicenseNotificationBody();
648
                $mailType = 'text/html';
649
650
                if (empty($subject)) {
651
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.subject', 'dpf');
652
                }
653
654
                if (empty($body)) {
655
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.body', 'dpf');
656
                    $mailType = 'text/plain';
657
                }
658
659
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
660
            }
661
662
        } catch (\Exception $e) {
663
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
664
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
665
666
            $logger->log(
667
                LogLevel::ERROR, "sendDepositLicenseNotification failed",
668
                array(
669
                    'document' => $document
670
                )
671
            );
672
        }
673
674
    }
675
676
    protected function replaceMarkers($message, $args)
677
    {
678
        if (is_array($args)) {
679
            foreach ($args as $key => $value) {
680
                $message = str_replace($key, $value, $message);
681
            }
682
        }
683
        return $message;
684
    }
685
686
687
    protected function sendMail($reveiver, $subject, $body, $args, $mailType)
688
    {
689
        $emailReceiver = array();
690
        $emailReceiver[$reveiver] = $reveiver;
691
        $message = (new \TYPO3\CMS\Core\Mail\MailMessage())
692
            ->setFrom(array('[email protected]' => '[email protected]'))
693
            ->setTo($emailReceiver)
694
            ->setSubject($this->replaceMarkers($subject,$args))
695
            ->setBody($this->replaceMarkers($body,$args),$mailType);
696
        $message->send();
697
    }
698
699
}
700