Passed
Push — master ( 4b779c...fcd908 )
by Ralf
11:19 queued 05:35
created

Notifier::sendEmbargoNotification()   A

Complexity

Conditions 5
Paths 21

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 24
c 1
b 1
f 0
nc 21
nop 1
dl 0
loc 39
rs 9.2248
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
22
class Notifier
23
{
24
25
    /**
26
     * clientRepository
27
     *
28
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
29
     * @inject
30
     */
31
    protected $clientRepository = null;
32
33
34
    /**
35
     * documentTypeRepository
36
     *
37
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
38
     * @inject
39
     */
40
    protected $documentTypeRepository = null;
41
42
43
    public function sendAdminNewSuggestionNotification(\EWW\Dpf\Domain\Model\Document $document) {
44
        try {
45
            /** @var $client \EWW\Dpf\Domain\Model\Client */
46
            $client = $this->clientRepository->findAll()->current();
47
            $clientAdminEmail = $client->getAdminEmail();
48
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
49
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
50
            $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

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

153
        $args['###AUTHOR###'] = array_shift(/** @scrutinizer ignore-type */ $document->getAuthors());
Loading history...
154
155
        $args['###SUBMITTER_NAME###'] = $slub->getSubmitterName();
156
        $args['###SUBMITTER_EMAIL###'] = $slub->getSubmitterEmail();
157
        $args['###SUBMITTER_NOTICE###'] = $slub->getSubmitterNotice();
158
159
        $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
160
        $args['###URN###'] = $mods->getQucosaUrn();
161
        $args['###URL###'] = 'http://nbn-resolving.de/' . $mods->getQucosaUrn();
162
163
        $host = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
164
        $backofficePageId = $settings['plugin.']['tx_dpf.']['settings.']['backofficePluginPage'];
165
166
        if ($document->isSuggestion()) {
167
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
168
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $document->getUid();
169
            $detailUrl .= '&tx_dpf_backoffice[action]=showSuggestionDetails';
170
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Änderungsvorschlag</a>';
171
        } else {
172
            $detailUrl = '<a href="' . $host . '/index.php?id=' . $backofficePageId;
173
            $detailUrl .= '&tx_dpf_backoffice[document]=' . $document->getUid();
174
            $detailUrl .= '&tx_dpf_backoffice[action]=showDetails';
175
            $detailUrl .= '&tx_dpf_backoffice[controller]=Document">Link zum Dokument</a>';
176
        }
177
178
        $args['###DETAIL_URL###'] = $detailUrl;
179
180
        $args['###HAS_FILES###'] = 'Metadata only';
181
182
        if ($document->getFileData()) {
183
            $args['###HAS_FILES###'] = 'Attachment';
184
            foreach ($document->getFileData() as $fileSection) {
185
                foreach ($fileSection as $file) {
186
                    $args['###FILE_LIST###'] .= $file['title'];
187
                }
188
            }
189
        }
190
191
        return $args;
192
    }
193
194
    public function sendNewDocumentNotification(\EWW\Dpf\Domain\Model\Document $document)
195
    {
196
197
        try {
198
            $client = $this->clientRepository->findAll()->current();
199
            $clientAdminEmail = $client->getAdminEmail();
200
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
201
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
202
            $submitterEmail = $slub->getSubmitterEmail();
203
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
204
            $authors = $document->getAuthors();
0 ignored issues
show
Unused Code introduced by
The assignment to $authors is dead and can be removed.
Loading history...
205
206
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);
207
208
            // Notify client admin
209
            if ($clientAdminEmail) {
210
                $subject = $client->getAdminNewDocumentNotificationSubject();
211
                $body = $client->getAdminNewDocumentNotificationBody();
212
                $mailType = 'text/html';
213
214
                if (empty($subject)) {
215
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.subject', 'dpf');
216
                }
217
218
                if (empty($body)) {
219
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.body', 'dpf');
220
                    $mailType = 'text/plain';
221
                }
222
223
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
224
225
            }
226
227
228
            // Notify submitter
229
            if ($submitterEmail) {
230
                $subject = $client->getSubmitterNewDocumentNotificationSubject();
231
                $body = $client->getSubmitterNewDocumentNotificationBody();
232
                $mailType = 'text/html';
233
234
                if (empty($subject)) {
235
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.subject', 'dpf');
236
                }
237
238
                if (empty($body)) {
239
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.body', 'dpf');
240
                    $mailType = 'text/plain';
241
                }
242
243
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
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, "sendNewDocumentNotification failed",
252
                array(
253
                    'document' => $document
254
                )
255
            );
256
        }
257
258
    }
259
260
    public function sendIngestNotification(\EWW\Dpf\Domain\Model\Document $document)
261
    {
262
263
        try {
264
            $client = $this->clientRepository->findAll()->current();
265
            $clientAdminEmail = $client->getAdminEmail();
0 ignored issues
show
Unused Code introduced by
The assignment to $clientAdminEmail is dead and can be removed.
Loading history...
266
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
267
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
268
            $submitterEmail = $slub->getSubmitterEmail();
269
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
270
271
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);
272
273
            // Notify submitter
274
            if ($submitterEmail) {
275
                $subject = $client->getSubmitterIngestNotificationSubject();
276
                $body = $client->getSubmitterIngestNotificationBody();
277
                $mailType = 'text/html';
278
279
                if (empty($subject)) {
280
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.subject', 'dpf');
281
                }
282
283
                if (empty($body)) {
284
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.body', 'dpf');
285
                    $mailType = 'text/plain';
286
                }
287
288
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
289
            }
290
        } catch (\Exception $e) {
291
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
292
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
293
294
            $logger->log(
295
                LogLevel::ERROR, "sendIngestNotification failed",
296
                array(
297
                    'document' => $document
298
                )
299
            );
300
        }
301
302
    }
303
304
    public function sendEmbargoNotification(\EWW\Dpf\Domain\Model\Document $document) {
305
        try {
306
            $client = $this->clientRepository->findAllByPid($document->getPid())->current();
307
            $clientAdminEmail = $client->getAdminEmail();
308
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
309
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
310
            $submitterEmail = $slub->getSubmitterEmail();
0 ignored issues
show
Unused Code introduced by
The assignment to $submitterEmail is dead and can be removed.
Loading history...
311
312
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
313
314
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);
315
316
            // Notify client admin
317
            if ($clientAdminEmail) {
318
                $subject = $client->getAdminEmbargoSubject();
319
                $body = $client->getAdminEmbargoBody();
320
                $mailType = 'text/html';
321
322
                if (empty($subject)) {
323
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.subject', 'dpf');
324
                }
325
326
                if (empty($body)) {
327
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.body', 'dpf');
328
                    $mailType = 'text/plain';
329
                }
330
331
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
332
333
            }
334
335
        } catch (\Exception $e) {
336
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
337
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
338
339
            $logger->log(
340
                LogLevel::ERROR, "sendRegisterNotification failed",
341
                array(
342
                    'document' => $document
343
                )
344
            );
345
        }
346
    }
347
348
    public function sendRegisterNotification(\EWW\Dpf\Domain\Model\Document $document)
349
    {
350
351
        try {
352
            $client = $this->clientRepository->findAll()->current();
353
            $clientAdminEmail = $client->getAdminEmail();
354
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
355
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
356
            $submitterEmail = $slub->getSubmitterEmail();
0 ignored issues
show
Unused Code introduced by
The assignment to $submitterEmail is dead and can be removed.
Loading history...
357
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
358
359
            $args = $this->getMailMarkerArray($document, $client, $documentType, $slub, $mods);
360
361
            // Notify client admin
362
            if ($clientAdminEmail) {
363
                $subject = $client->getAdminRegisterDocumentNotificationSubject();
364
                $body = $client->getAdminRegisterDocumentNotificationBody();
365
                $mailType = 'text/html';
366
367
                if (empty($subject)) {
368
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.subject', 'dpf');
369
                }
370
371
                if (empty($body)) {
372
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.body', 'dpf');
373
                    $mailType = 'text/plain';
374
                }
375
376
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
377
378
            }
379
380
        } catch (\Exception $e) {
381
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
382
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
383
384
            $logger->log(
385
                LogLevel::ERROR, "sendRegisterNotification failed",
386
                array(
387
                    'document' => $document
388
                )
389
            );
390
        }
391
392
    }
393
394
    protected function replaceMarkers($message, $args)
395
    {
396
        if (is_array($args)) {
397
            foreach ($args as $key => $value) {
398
                $message = str_replace($key, $value, $message);
399
            }
400
        }
401
        return $message;
402
    }
403
404
405
    protected function sendMail($reveiver, $subject, $body, $args, $mailType)
406
    {
407
        $emailReceiver = array();
408
        $emailReceiver[$reveiver] = $reveiver;
409
        $message = (new \TYPO3\CMS\Core\Mail\MailMessage())
410
            ->setFrom(array('[email protected]' => '[email protected]'))
411
            ->setTo($emailReceiver)
412
            ->setSubject($this->replaceMarkers($subject,$args))
413
            ->setBody($this->replaceMarkers($body,$args),$mailType);
414
        $message->send();
415
    }
416
417
}
418