Passed
Pull Request — master (#195)
by
unknown
21:11
created

Notifier::sendMyPublicationUpdateNotification()   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 2
b 0
f 0
nc 25
nop 2
dl 0
loc 44
rs 8.9137
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
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
25
26
class Notifier
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
    public function sendAdminNewSuggestionNotification(\EWW\Dpf\Domain\Model\Document $document) {
61
        try {
62
            /** @var $client \EWW\Dpf\Domain\Model\Client */
63
            $client = $this->clientRepository->findAll()->current();
64
            $clientAdminEmail = $client->getAdminEmail();
65
            $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

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

168
        $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
169
        $args['###AUTHOR###'] = $author['name'];
170
171
        $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
172
        $args['###SUBMITTER_NAME###'] = $internalFormat->getSubmitterName();
173
        $args['###SUBMITTER_EMAIL###'] = $internalFormat->getSubmitterEmail();
174
        $args['###SUBMITTER_NOTICE###'] = $internalFormat->getSubmitterNotice();
175
176
        $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
177
        $args['###URN###'] = $internalFormat->getQucosaUrn();
178
        $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

178
        $args['###URL###'] = 'http://nbn-resolving.de/' . /** @scrutinizer ignore-type */ $internalFormat->getQucosaUrn();
Loading history...
179
180
        $args['###REASON###'] = $reason;
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
                if ($slub->getFisId()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $slub seems to be never defined.
Loading history...
240
                    $request = Request::post($client->getActiveMessagingSuggestionAcceptUrl());
241
                    if ($body = $client->getActiveMessagingSuggestionAcceptUrlBody()) {
242
                        $request->body($this->replaceMarkers($body, $args));
243
                    }
244
                    $request->send();
245
                }
246
            }
247
248
        } catch (\Exception $e) {
249
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
250
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
251
252
            $logger->log(
253
                LogLevel::ERROR, "sendSuggestionAcceptNotification failed",
254
                array(
255
                    'document' => $document
256
                )
257
            );
258
        }
259
    }
260
261
    /**
262
     * @param Document $document
263
     * @param string $reason
264
     */
265
    public function sendSuggestionDeclineNotification(\EWW\Dpf\Domain\Model\Document $document, $reason = "") {
266
267
        try {
268
            /** @var Client $client */
269
            $client = $this->clientRepository->findAll()->current();
270
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
271
272
            $args = $this->getMailMarkerArray($document, $client, $documentType, $reason);
273
274
            // Active messaging: Suggestion accept
275
            if ($client->getActiveMessagingSuggestionDeclineUrl()) {
276
                if ($slub->getFisId()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $slub seems to be never defined.
Loading history...
277
                    $request = Request::post($client->getActiveMessagingSuggestionDeclineUrl());
278
                    if ($body = $client->getActiveMessagingSuggestionDeclineUrlBody()) {
279
                        $request->body($this->replaceMarkers($body, $args));
280
                    }
281
                    $request->send();
282
                }
283
            }
284
285
        } catch (\Exception $e) {
286
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
287
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
288
289
            $logger->log(
290
                LogLevel::ERROR, "sendSuggestionDeclineNotification failed",
291
                array(
292
                    'document' => $document
293
                )
294
            );
295
        }
296
    }
297
298
    public function sendChangedDocumentNotification(\EWW\Dpf\Domain\Model\Document $document, $addedFisIdOnly = false) {
299
300
        try {
301
            /** @var Client $client */
302
            $client = $this->clientRepository->findAll()->current();
303
304
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
305
306
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
307
308
            $args = $this->getMailMarkerArray($document, $client, $documentType);
309
310
            // Active messaging: Suggestion accept
311
            if (!$addedFisIdOnly && $client->getActiveMessagingChangedDocumentUrl()) {
312
                if ($internalFormat->getFisId()) {
0 ignored issues
show
Bug introduced by
The method getFisId() does not exist on EWW\Dpf\Helper\InternalFormat. ( Ignorable by Annotation )

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

312
                if ($internalFormat->/** @scrutinizer ignore-call */ getFisId()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
                    $request = Request::post($client->getActiveMessagingChangedDocumentUrl());
314
                    if ($body = $client->getActiveMessagingChangedDocumentUrlBody()) {
315
                        $request->body($this->replaceMarkers($body,$args));
316
                    }
317
                    $request->send();
318
                }
319
            }
320
321
        } catch (\Exception $e) {
322
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
323
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
324
325
            $logger->log(
326
                LogLevel::ERROR, "sendChangedDocumentNotification failed",
327
                array(
328
                    'document' => $document
329
                )
330
            );
331
        }
332
    }
333
334
335
    public function sendReleasePublishNotification(\EWW\Dpf\Domain\Model\Document $document)
336
    {
337
        try {
338
            /** @var Client $client */
339
            $client = $this->clientRepository->findAll()->current();
340
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
341
            /**  @var \EWW\Dpf\Helper\Slub $slub */
342
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
343
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
344
345
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Services\Email\N...r::getMailMarkerArray() has too many arguments starting with $mods. ( Ignorable by Annotation )

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

345
            /** @scrutinizer ignore-call */ 
346
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
346
347
            // Active messaging: New document (Release publish)
348
            if ($client->getActiveMessagingNewDocumentUrl()) {
349
                $fisId = $slub->getFisId();
350
                if (empty($fisId)) {
351
                    $request = Request::post($client->getActiveMessagingNewDocumentUrl());
352
                    if ($body = $client->getActiveMessagingNewDocumentUrlBody()) {
353
                        $request->body($this->replaceMarkers($body, $args));
354
                    }
355
                    $request->send();
356
                }
357
            }
358
359
        } catch (\Exception $e) {
360
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
361
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
362
363
            $logger->log(
364
                LogLevel::ERROR, "sendReleasePublishNotification failed",
365
                array(
366
                    'document' => $document
367
                )
368
            );
369
        }
370
    }
371
372
373
    public function sendNewDocumentNotification(\EWW\Dpf\Domain\Model\Document $document)
374
    {
375
376
        try {
377
            /** @var Client $client */
378
            $client = $this->clientRepository->findAll()->current();
379
            $clientAdminEmail = $client->getAdminEmail();
380
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
381
            $submitterEmail = $internalFormat->getSubmitterEmail();
382
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
383
            $authors = $document->getAuthors();
0 ignored issues
show
Unused Code introduced by
The assignment to $authors is dead and can be removed.
Loading history...
384
385
            $args = $this->getMailMarkerArray($document, $client, $documentType);
386
387
            // Notify client admin
388
            if ($clientAdminEmail) {
389
                $subject = $client->getAdminNewDocumentNotificationSubject();
390
                $body = $client->getAdminNewDocumentNotificationBody();
391
                $mailType = 'text/html';
392
393
                if (empty($subject)) {
394
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.subject', 'dpf');
395
                }
396
397
                if (empty($body)) {
398
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.body', 'dpf');
399
                    $mailType = 'text/plain';
400
                }
401
402
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
403
404
            }
405
406
407
            // Notify submitter
408
            if ($submitterEmail) {
409
                $subject = $client->getSubmitterNewDocumentNotificationSubject();
410
                $body = $client->getSubmitterNewDocumentNotificationBody();
411
                $mailType = 'text/html';
412
413
                if (empty($subject)) {
414
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.subject', 'dpf');
415
                }
416
417
                if (empty($body)) {
418
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.body', 'dpf');
419
                    $mailType = 'text/plain';
420
                }
421
422
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
423
            }
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, "sendNewDocumentNotification failed",
431
                array(
432
                    'document' => $document
433
                )
434
            );
435
        }
436
437
    }
438
439
    public function sendIngestNotification(\EWW\Dpf\Domain\Model\Document $document)
440
    {
441
442
        try {
443
            $client = $this->clientRepository->findAll()->current();
444
            $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData());
445
            $submitterEmail = $internalFormat->getSubmitterEmail();
446
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
447
448
            $args = $this->getMailMarkerArray($document, $client, $documentType);
449
450
            // Notify submitter
451
            if ($submitterEmail) {
452
                $subject = $client->getSubmitterIngestNotificationSubject();
453
                $body = $client->getSubmitterIngestNotificationBody();
454
                $mailType = 'text/html';
455
456
                if (empty($subject)) {
457
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.subject', 'dpf');
458
                }
459
460
                if (empty($body)) {
461
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.body', 'dpf');
462
                    $mailType = 'text/plain';
463
                }
464
465
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
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, "sendIngestNotification failed",
473
                array(
474
                    'document' => $document
475
                )
476
            );
477
        }
478
479
    }
480
481
    public function sendEmbargoNotification(\EWW\Dpf\Domain\Model\Document $document) {
482
        try {
483
            $client = $this->clientRepository->findAllByPid($document->getPid())->current();
484
            $clientAdminEmail = $client->getAdminEmail();
485
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->getAdminEmbargoSubject();
493
                $body = $client->getAdminEmbargoBody();
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.document.embargo.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.document.embargo.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
    public function sendRegisterNotification(\EWW\Dpf\Domain\Model\Document $document)
523
    {
524
525
        try {
526
            $client = $this->clientRepository->findAll()->current();
527
            $clientAdminEmail = $client->getAdminEmail();
528
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
529
530
            $args = $this->getMailMarkerArray($document, $client, $documentType);
531
532
            // Notify client admin
533
            if ($clientAdminEmail) {
534
                $subject = $client->getAdminRegisterDocumentNotificationSubject();
535
                $body = $client->getAdminRegisterDocumentNotificationBody();
536
                $mailType = 'text/html';
537
538
                if (empty($subject)) {
539
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.subject', 'dpf');
540
                }
541
542
                if (empty($body)) {
543
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.body', 'dpf');
544
                    $mailType = 'text/plain';
545
                }
546
547
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
548
549
            }
550
551
        } catch (\Exception $e) {
552
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
553
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
554
555
            $logger->log(
556
                LogLevel::ERROR, "sendRegisterNotification failed",
557
                array(
558
                    'document' => $document
559
                )
560
            );
561
        }
562
563
    }
564
565
    /**
566
     * @param \EWW\Dpf\Domain\Model\Document $document
567
     * @param array $recipients
568
     */
569
    public function sendMyPublicationUpdateNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
570
    {
571
572
        try {
573
            /** @var Client $client */
574
            $client = $this->clientRepository->findAll()->current();
575
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
576
            $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

576
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
577
578
            $args = $this->getMailMarkerArray($document, $client, $documentType);
579
580
            // Notify client admin
581
            /** @var FrontendUser $recipient */
582
            foreach ($recipients as $recipient) {
583
584
                if ($recipient->getEmail()) {
585
586
                    $subject = $client->getMypublicationsUpdateNotificationSubject();
587
                    $body = $client->getMypublicationsUpdateNotificationBody();
588
                    $mailType = 'text/html';
589
590
                    if (empty($subject)) {
591
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.subject',
592
                            'dpf');
593
                    }
594
595
                    if (empty($body)) {
596
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.updatePublication.mypublications.body',
597
                            'dpf');
598
                        $mailType = 'text/plain';
599
                    }
600
601
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
602
                }
603
            }
604
605
        } catch (\Exception $e) {
606
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
607
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
608
609
            $logger->log(
610
                LogLevel::ERROR, "sendRegisterNotification failed",
611
                array(
612
                    'document' => $document
613
                )
614
            );
615
        }
616
617
    }
618
619
620
    /**
621
     * @param \EWW\Dpf\Domain\Model\Document $document
622
     * @param array $recipients
623
     */
624
    public function sendMyPublicationNewNotification(\EWW\Dpf\Domain\Model\Document $document, $recipients)
625
    {
626
627
        try {
628
            /** @var Client $client */
629
            $client = $this->clientRepository->findAll()->current();
630
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
631
            $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

631
            $author = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
Unused Code introduced by
The assignment to $author is dead and can be removed.
Loading history...
632
633
            $args = $this->getMailMarkerArray($document, $client, $documentType);
634
635
            // Notify client admin
636
            /** @var FrontendUser $recipient */
637
            foreach ($recipients as $recipient) {
638
639
                if ($recipient->getEmail()) {
640
641
                    $subject = $client->getMypublicationsNewNotificationSubject();
642
                    $body = $client->getMypublicationsNewNotificationBody();
643
                    $mailType = 'text/html';
644
645
                    if (empty($subject)) {
646
                        $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.subject',
647
                            'dpf');
648
                    }
649
650
                    if (empty($body)) {
651
                        $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newPublication.mypublications.body',
652
                            'dpf');
653
                        $mailType = 'text/plain';
654
                    }
655
656
                    $this->sendMail($recipient->getEmail(), $subject, $body, $args, $mailType);
657
                }
658
            }
659
660
        } catch (\Exception $e) {
661
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
662
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
663
664
            $logger->log(
665
                LogLevel::ERROR, "sendRegisterNotification failed",
666
                array(
667
                    'document' => $document
668
                )
669
            );
670
        }
671
672
    }
673
674
    public function sendDepositLicenseNotification(\EWW\Dpf\Domain\Model\Document $document)
675
    {
676
677
        try {
678
            /** @var Client $client */
679
            $client = $this->clientRepository->findAll()->current();
680
            $clientAdminEmail = $client->getAdminEmail();
681
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
682
683
            $args = $this->getMailMarkerArray($document, $client, $documentType);
684
685
            // Notify client admin
686
            if ($clientAdminEmail && $client->isSendAdminDepositLicenseNotification()) {
687
688
                $subject = $client->getAdminDepositLicenseNotificationSubject();
689
                $body = $client->getAdminDepositLicenseNotificationBody();
690
                $mailType = 'text/html';
691
692
                if (empty($subject)) {
693
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.subject', 'dpf');
694
                }
695
696
                if (empty($body)) {
697
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.depositLicense.admin.body', 'dpf');
698
                    $mailType = 'text/plain';
699
                }
700
701
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
702
            }
703
704
        } catch (\Exception $e) {
705
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
706
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
707
708
            $logger->log(
709
                LogLevel::ERROR, "sendDepositLicenseNotification failed",
710
                array(
711
                    'document' => $document
712
                )
713
            );
714
        }
715
716
    }
717
718
    protected function replaceMarkers($message, $args)
719
    {
720
        if (is_array($args)) {
721
            foreach ($args as $key => $value) {
722
                $message = str_replace($key, $value, $message);
723
            }
724
        }
725
        return $message;
726
    }
727
728
729
    protected function sendMail($reveiver, $subject, $body, $args, $mailType)
730
    {
731
        $emailReceiver = array();
732
        $emailReceiver[$reveiver] = $reveiver;
733
        $message = (new \TYPO3\CMS\Core\Mail\MailMessage())
734
            ->setFrom(array('[email protected]' => '[email protected]'))
735
            ->setTo($emailReceiver)
736
            ->setSubject($this->replaceMarkers($subject,$args))
737
            ->setBody($this->replaceMarkers($body,$args),$mailType);
738
        $message->send();
739
    }
740
741
}
742