Passed
Pull Request — master (#175)
by
unknown
08:11
created

Notifier::sendEmbargoNotification()   A

Complexity

Conditions 5
Paths 28

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
c 0
b 0
f 0
nc 28
nop 1
dl 0
loc 51
rs 9.1128

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 \TYPO3\CMS\Core\Log\LogLevel;
18
use \TYPO3\CMS\Core\Log\LogManager;
19
use \TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
class Notifier
22
{
23
24
    /**
25
     * clientRepository
26
     *
27
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
28
     * @inject
29
     */
30
    protected $clientRepository = null;
31
32
33
    /**
34
     * documentTypeRepository
35
     *
36
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
37
     * @inject
38
     */
39
    protected $documentTypeRepository = null;
40
41
42
    public function sendNewDocumentNotification(\EWW\Dpf\Domain\Model\Document $document)
43
    {
44
45
        try {
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
            $submitterEmail = $slub->getSubmitterEmail();
51
            $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

51
            /** @scrutinizer ignore-call */ 
52
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
Loading history...
52
            $authors = $document->getAuthors();
53
54
            $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...
55
            $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
56
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
0 ignored issues
show
Bug introduced by
The method getDisplayName() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();

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...
57
            $args['###TITLE###'] = $document->getTitle();
58
            $args['###AUTHOR###'] = array_shift($authors);
59
60
            $args['###SUBMITTER_NAME###'] = $slub->getSubmitterName();
61
            $args['###SUBMITTER_EMAIL###'] = $submitterEmail; //
62
            $args['###SUBMITTER_NOTICE###'] = $slub->getSubmitterNotice();
63
64
            $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
65
            $args['###URN###'] = $mods->getQucosaUrn();
66
            $args['###URL###'] = 'http://nbn-resolving.de/' . $mods->getQucosaUrn();
67
68
            // Notify client admin
69
            if ($clientAdminEmail) {
70
                $subject = $client->getAdminNewDocumentNotificationSubject();
71
                $body = $client->getAdminNewDocumentNotificationBody();
72
                $mailType = 'text/html';
73
74
                if (empty($subject)) {
75
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.subject', 'dpf');
76
                }
77
78
                if (empty($body)) {
79
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.admin.body', 'dpf');
80
                    $mailType = 'text/plain';
81
                }
82
83
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
84
85
            }
86
87
88
            // Notify submitter
89
            if ($submitterEmail) {
90
                $subject = $client->getSubmitterNewDocumentNotificationSubject();
91
                $body = $client->getSubmitterNewDocumentNotificationBody();
92
                $mailType = 'text/html';
93
94
                if (empty($subject)) {
95
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.subject', 'dpf');
96
                }
97
98
                if (empty($body)) {
99
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.newDocument.submitter.body', 'dpf');
100
                    $mailType = 'text/plain';
101
                }
102
103
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
104
            }
105
106
        } catch (\Exception $e) {
107
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
108
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
109
110
            $logger->log(
111
                LogLevel::ERROR, "sendNewDocumentNotification failed",
112
                array(
113
                    'document' => $document
114
                )
115
            );
116
        }
117
118
    }
119
120
    public function sendIngestNotification(\EWW\Dpf\Domain\Model\Document $document)
121
    {
122
123
        try {
124
            $client = $this->clientRepository->findAll()->current();
125
            $clientAdminEmail = $client->getAdminEmail();
0 ignored issues
show
Unused Code introduced by
The assignment to $clientAdminEmail is dead and can be removed.
Loading history...
126
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
127
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
128
            $submitterEmail = $slub->getSubmitterEmail();
129
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
130
            $authors = $document->getAuthors();
131
132
            $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...
133
            $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
134
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
135
            $args['###TITLE###'] = $document->getTitle();
136
            $args['###AUTHOR###'] = array_shift($authors);
137
138
            $args['###SUBMITTER_NAME###'] = $slub->getSubmitterName();
139
            $args['###SUBMITTER_EMAIL###'] = $submitterEmail; //
140
            $args['###SUBMITTER_NOTICE###'] = $slub->getSubmitterNotice();
141
142
            $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
143
            $args['###URN###'] = $mods->getQucosaUrn();
144
            $args['###URL###'] = 'http://nbn-resolving.de/' . $mods->getQucosaUrn();
145
146
            // Notify submitter
147
            if ($submitterEmail) {
148
                $subject = $client->getSubmitterIngestNotificationSubject();
149
                $body = $client->getSubmitterIngestNotificationBody();
150
                $mailType = 'text/html';
151
152
                if (empty($subject)) {
153
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.subject', 'dpf');
154
                }
155
156
                if (empty($body)) {
157
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.ingestDocument.submitter.body', 'dpf');
158
                    $mailType = 'text/plain';
159
                }
160
161
                $this->sendMail($submitterEmail, $subject, $body, $args, $mailType);
162
            }
163
        } catch (\Exception $e) {
164
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
165
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
166
167
            $logger->log(
168
                LogLevel::ERROR, "sendIngestNotification failed",
169
                array(
170
                    'document' => $document
171
                )
172
            );
173
        }
174
175
    }
176
177
    public function sendEmbargoNotification(\EWW\Dpf\Domain\Model\Document $document) {
178
        try {
179
            $client = $this->clientRepository->findAllByPid($document->getPid())->current();
180
            $clientAdminEmail = $client->getAdminEmail();
181
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
182
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
183
            $submitterEmail = $slub->getSubmitterEmail();
184
//            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
185
            $authors = $document->getAuthors();
186
187
            $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...
188
            $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
189
//            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
190
            $args['###TITLE###'] = $document->getTitle();
191
            $args['###AUTHOR###'] = array_shift($authors);
192
193
            $args['###SUBMITTER_NAME###'] = $slub->getSubmitterName();
194
            $args['###SUBMITTER_EMAIL###'] = $submitterEmail; //
195
            $args['###SUBMITTER_NOTICE###'] = $slub->getSubmitterNotice();
196
197
            $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
198
            $args['###URN###'] = $mods->getQucosaUrn();
199
            $args['###URL###'] = 'http://nbn-resolving.de/' . $mods->getQucosaUrn();
200
201
202
            // Notify client admin
203
            if ($clientAdminEmail) {
204
205
                $mailType = 'text/html';
206
207
                if (empty($subject)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subject seems to never exist and therefore empty should always be true.
Loading history...
208
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.subject', 'dpf');
209
                }
210
211
                if (empty($body)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $body seems to never exist and therefore empty should always be true.
Loading history...
212
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.document.embargo.admin.body', 'dpf');
213
                    $mailType = 'text/plain';
214
                }
215
216
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
217
218
            }
219
220
        } catch (\Exception $e) {
221
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
222
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
223
224
            $logger->log(
225
                LogLevel::ERROR, "sendRegisterNotification failed",
226
                array(
227
                    'document' => $document
228
                )
229
            );
230
        }
231
    }
232
233
    public function sendRegisterNotification(\EWW\Dpf\Domain\Model\Document $document)
234
    {
235
236
        try {
237
            $client = $this->clientRepository->findAll()->current();
238
            $clientAdminEmail = $client->getAdminEmail();
239
            $mods = new \EWW\Dpf\Helper\Mods($document->getXmlData());
240
            $slub = new \EWW\Dpf\Helper\Slub($document->getSlubInfoData());
241
            $submitterEmail = $slub->getSubmitterEmail();
242
            $documentType = $this->documentTypeRepository->findOneByUid($document->getDocumentType());
243
            $authors = $document->getAuthors();
244
245
            $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...
246
            $args['###PROCESS_NUMBER###'] = $document->getProcessNumber();
247
            $args['###DOCUMENT_TYPE###'] = $documentType->getDisplayName();
248
            $args['###TITLE###'] = $document->getTitle();
249
            $args['###AUTHOR###'] = array_shift($authors);
250
251
            $args['###SUBMITTER_NAME###'] = $slub->getSubmitterName();
252
            $args['###SUBMITTER_EMAIL###'] = $submitterEmail; //
253
            $args['###SUBMITTER_NOTICE###'] = $slub->getSubmitterNotice();
254
255
            $args['###DATE###'] = (new \DateTime)->format("d-m-Y H:i:s");
256
            $args['###URN###'] = $mods->getQucosaUrn();
257
            $args['###URL###'] = 'http://nbn-resolving.de/' . $mods->getQucosaUrn();
258
259
260
            // Notify client admin
261
            if ($clientAdminEmail) {
262
                $subject = $client->getAdminRegisterDocumentNotificationSubject();
263
                $body = $client->getAdminRegisterDocumentNotificationBody();
264
                $mailType = 'text/html';
265
266
                if (empty($subject)) {
267
                    $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.subject', 'dpf');
268
                }
269
270
                if (empty($body)) {
271
                    $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:notification.registerDocument.admin.body', 'dpf');
272
                    $mailType = 'text/plain';
273
                }
274
275
                $this->sendMail($clientAdminEmail, $subject, $body, $args, $mailType);
276
277
            }
278
279
        } catch (\Exception $e) {
280
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
281
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
282
283
            $logger->log(
284
                LogLevel::ERROR, "sendRegisterNotification failed",
285
                array(
286
                    'document' => $document
287
                )
288
            );
289
        }
290
291
    }
292
293
    protected function replaceMarkers($message, $args)
294
    {
295
        if (is_array($args)) {
296
            foreach ($args as $key => $value) {
297
                $message = str_replace($key, $value, $message);
298
            }
299
        }
300
        return $message;
301
    }
302
303
304
    protected function sendMail($reveiver, $subject, $body, $args, $mailType)
305
    {
306
        $emailReceiver = array();
307
        $emailReceiver[$reveiver] = $reveiver;
308
        $message = (new \TYPO3\CMS\Core\Mail\MailMessage())
309
            ->setFrom(array('[email protected]' => '[email protected]'))
310
            ->setTo($emailReceiver)
311
            ->setSubject($this->replaceMarkers($subject,$args))
312
            ->setBody($this->replaceMarkers($body,$args),$mailType);
313
        $message->send();
314
    }
315
316
}
317