Passed
Pull Request — master (#195)
by
unknown
07:40
created

DocumentManager   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 692
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 260
dl 0
loc 692
rs 2
c 3
b 0
f 0
wmc 103

15 Methods

Rating   Name   Duplication   Size   Complexity  
B resolveIdentifier() 0 35 7
A read() 0 29 5
A getDocumentBookmarkUsers() 0 12 2
A getNewlyAssignedUsers() 0 12 3
A getDocumentTransferManager() 0 11 1
B addSuggestion() 0 60 7
A getCreatorUser() 0 2 1
A updateFiles() 0 17 4
B updateRemotely() 0 47 10
A removeDocument() 0 7 2
F getUpdateNotificationRecipients() 0 96 29
D update() 0 106 18
A getAssignedUsers() 0 13 3
A hasActiveEmbargo() 0 8 2
B getNewPublicationNotificationRecipients() 0 27 9

How to fix   Complexity   

Complex Class

Complex classes like DocumentManager 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 DocumentManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace EWW\Dpf\Services\Document;
3
4
use EWW\Dpf\Domain\Model\Bookmark;
5
use EWW\Dpf\Domain\Model\Document;
6
use EWW\Dpf\Domain\Model\File;
7
use EWW\Dpf\Domain\Model\FrontendUser;
8
use EWW\Dpf\Security\Security;
9
use EWW\Dpf\Services\ElasticSearch\ElasticSearch;
10
use EWW\Dpf\Services\Transfer\FedoraRepository;
11
use EWW\Dpf\Services\Transfer\DocumentTransferManager;
12
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
13
use EWW\Dpf\Controller\AbstractController;
14
use EWW\Dpf\Services\Email\Notifier;
15
use Symfony\Component\Workflow\Workflow;
16
use Httpful\Request;
17
18
class DocumentManager
19
{
20
    /**
21
     * objectManager
22
     *
23
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
24
     * @inject
25
     */
26
    protected $objectManager = null;
27
28
    /**
29
     * documentRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
32
     * @inject
33
     */
34
    protected $documentRepository = null;
35
36
    /**
37
     * fileRepository
38
     *
39
     * @var \EWW\Dpf\Domain\Repository\FileRepository
40
     * @inject
41
     */
42
    protected $fileRepository = null;
43
44
    /**
45
     * bookmarkRepository
46
     *
47
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
48
     * @inject
49
     */
50
    protected $bookmarkRepository = null;
51
52
    /**
53
     * persistence manager
54
     *
55
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
56
     * @inject
57
     */
58
    protected $persistenceManager;
59
60
    /**
61
     * signalSlotDispatcher
62
     *
63
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
64
     * @inject
65
     */
66
    protected $signalSlotDispatcher = null;
67
68
    /**
69
     * notifier
70
     *
71
     * @var \EWW\Dpf\Services\Email\Notifier
72
     * @inject
73
     */
74
    protected $notifier = null;
75
76
    /**
77
     * security
78
     *
79
     * @var \EWW\Dpf\Security\Security
80
     * @inject
81
     */
82
    protected $security = null;
83
84
    /**
85
     * frontendUserRepository
86
     *
87
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
88
     * @inject
89
     */
90
    protected $frontendUserRepository = null;
91
92
    /**
93
     * elasticSearch
94
     *
95
     * @var \EWW\Dpf\Services\ElasticSearch\ElasticSearch
96
     * @inject
97
     */
98
    protected $elasticSearch = null;
99
100
    /**
101
     * queryBuilder
102
     *
103
     * @var \EWW\Dpf\Services\ElasticSearch\QueryBuilder
104
     * @inject
105
     */
106
    protected $queryBuilder = null;
107
108
    /**
109
     * Returns the localized document identifiers (uid/objectIdentifier).
110
     *
111
     * @param $identifier
112
     * @return array
113
     */
114
    public function resolveIdentifier($identifier) {
115
116
        $localizedIdentifiers = [];
117
118
        $document = $this->documentRepository->findByIdentifier($identifier);
119
120
        if ($document instanceof Document) {
0 ignored issues
show
introduced by
$document is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
121
122
            if ($document->getObjectIdentifier()) {
123
                $localizedIdentifiers['objectIdentifier'] = $document->getObjectIdentifier();
124
            }
125
126
            if ($document->getUid()) {
127
                $localizedIdentifiers['uid'] = $document->getUid();
128
            }
129
        } else {
130
131
            $query = $this->queryBuilder->buildQuery(
132
                1, [], 0,
133
                [], [], [], null, null,
134
                'identifier:"'.$identifier.'"'
135
            );
136
137
            try {
138
                $results =  $this->elasticSearch->search($query, 'object');
139
                if (is_array($results) && $results['hits']['total']['value'] > 0) {
140
                    $localizedIdentifiers['objectIdentifier'] = $results['hits']['hits'][0]['_id'];
141
                }
142
            } catch (\Exception $e) {
143
                return [];
144
            }
145
146
        }
147
148
        return $localizedIdentifiers;
149
    }
150
151
152
    /**
153
     * Returns a document specified by repository object identifier, a typo3 uid or a process number.
154
     *
155
     * @param string $identifier
156
     * @return Document|null
157
     */
158
    public function read($identifier)
159
    {
160
        if (!$identifier) {
161
            return null;
162
        }
163
164
        $localizedIdentifiers = $this->resolveIdentifier($identifier);
165
166
        if (array_key_exists('uid', $localizedIdentifiers)) {
167
            return $this->documentRepository->findByUid($localizedIdentifiers['uid']);
168
        }
169
170
        if (array_key_exists('objectIdentifier', $localizedIdentifiers)) {
171
            try {
172
                /** @var \EWW\Dpf\Domain\Model\Document $document */
173
                $document = $this->getDocumentTransferManager()->retrieve($localizedIdentifiers['objectIdentifier']);
174
175
                // index the document
176
                $this->signalSlotDispatcher->dispatch(
177
                    AbstractController::class, 'indexDocument', [$document]
178
                );
179
180
                return $document;
181
            } catch (\Exception $exception) {
182
                return null;
183
            }
184
        }
185
186
        return null;
187
    }
188
189
    /**
190
     * Updates a document locally or remotely.
191
     *
192
     * @param Document $document
193
     * @param string $workflowTransition
194
     * @param array $deletedFiles
195
     * @param array $newFiles
196
     * @param bool $addedFisIdOnly
197
     * @return string|false
198
     * @throws \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException
199
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
200
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
201
     */
202
    public function update(
203
        Document $document, $workflowTransition = null, $deletedFiles = [], $newFiles = [], $addedFisIdOnly = false
204
    )
205
    {
206
        // xml data fields are limited to 64 KB
207
        if (strlen($document->getXmlData()) >= 64 * 1024) {
208
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
209
        }
210
211
        /** @var \Symfony\Component\Workflow\Workflow $workflow */
212
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
213
214
        if ($workflowTransition) {
215
            if (!$workflow->can($document, $workflowTransition)) {
216
                return false;
217
            }
218
            $workflow->apply($document, $workflowTransition);
219
        }
220
221
        if ($document->isSuggestion()) {
222
223
            // if local suggestion copy
224
            $updateResult = false;
225
226
        } elseif ($document->isTemporaryCopy()) {
227
228
            // if temporary working copy
229
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
230
231
        } elseif (
232
            $document->isWorkingCopy() &&
233
            (
234
                $workflowTransition === DocumentWorkflow::TRANSITION_POSTPONE ||
235
                $workflowTransition === DocumentWorkflow::TRANSITION_DISCARD ||
236
                $workflowTransition === DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE
237
            )
238
        ) {
239
            // if local working copy with state change
240
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
241
242
        } elseif ($document->isWorkingCopy()) {
243
244
            // if local working copy with no state change
245
            $this->updateFiles($document, $deletedFiles, $newFiles);
246
            $this->documentRepository->update($document);
247
            $updateResult = $document->getDocumentIdentifier();
248
249
        } elseif ($workflowTransition == DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) {
250
            // Fedora ingest
251
            if ($ingestedDocument = $this->getDocumentTransferManager()->ingest($document)) {
252
253
                // After ingest all related bookmarks need an update of the identifier into an fedora object identifier.
254
                if ($ingestedDocument instanceof Document) {
0 ignored issues
show
introduced by
$ingestedDocument is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
255
                    /** @var Bookmark $bookmark */
256
                    foreach ($this->bookmarkRepository->findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) {
0 ignored issues
show
Bug introduced by
The method findByDocumentIdentifier() does not exist on EWW\Dpf\Domain\Repository\BookmarkRepository. 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

256
                    foreach ($this->bookmarkRepository->/** @scrutinizer ignore-call */ findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) {
Loading history...
257
                        $bookmark->setDocumentIdentifier($ingestedDocument->getDocumentIdentifier());
258
                        $this->bookmarkRepository->update($bookmark);
259
                    }
260
                    $this->persistenceManager->persistAll();
261
                } else {
262
                    throw \Exception("Logical exception while updating bookmarks.");
0 ignored issues
show
Bug introduced by
The function Exception was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

262
                    throw /** @scrutinizer ignore-call */ \Exception("Logical exception while updating bookmarks.");
Loading history...
263
                }
264
265
                // check embargo
266
                if(!$this->hasActiveEmbargo($document)){
267
                    $this->removeDocument($document);
268
                } else {
269
                    $document->setState(DocumentWorkflow::constructState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS, $document->getRemoteState()));
270
                }
271
                $updateResult = $document->getDocumentIdentifier();
272
            } else {
273
                $updateResult = false;
274
            }
275
        } else {
276
            $this->updateFiles($document, $deletedFiles, $newFiles);
277
            $this->documentRepository->update($document);
278
            $updateResult = $document->getDocumentIdentifier();
279
        }
280
281
        if ($updateResult) {
282
283
            if (DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) {
284
                // delete local document from index
285
                $this->signalSlotDispatcher->dispatch(
286
                    AbstractController::class, 'deleteDocumentFromIndex', [$document->getUid()]
287
                );
288
            }
289
290
            // index the document
291
            $this->signalSlotDispatcher->dispatch(
292
                AbstractController::class, 'indexDocument', [$document]
293
            );
294
295
            // Notify assigned users
296
            $recipients = $this->getUpdateNotificationRecipients($document);
297
            $this->notifier->sendMyPublicationUpdateNotification($document, $recipients);
298
299
            $recipients = $this->getNewPublicationNotificationRecipients($document);
300
            $this->notifier->sendMyPublicationNewNotification($document, $recipients);
301
302
            /** @var Notifier $notifier */
303
            $notifier = $this->objectManager->get(Notifier::class);
304
            $notifier->sendChangedDocumentNotification($document, $addedFisIdOnly);
305
        }
306
307
        return $updateResult;
308
    }
309
310
    /**
311
     * @return DocumentTransferManager
312
     */
313
    protected function getDocumentTransferManager()
314
    {
315
        /** @var DocumentTransferManager $documentTransferManager */
316
        $documentTransferManager = $this->objectManager->get(DocumentTransferManager::class);
317
318
        /** @var  FedoraRepository $remoteRepository */
319
        $remoteRepository = $this->objectManager->get(FedoraRepository::class);
320
321
        $documentTransferManager->setRemoteRepository($remoteRepository);
322
323
        return $documentTransferManager;
324
    }
325
326
327
    /**
328
     * Adds and delete file model objects attached to the document.
329
     *
330
     * @param Document $document
331
     * @param array $deletedFiles
332
     * @param array $newFiles
333
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
334
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
335
     */
336
    protected function updateFiles(Document $document, $deletedFiles, $newFiles)
337
    {
338
        // Delete files
339
        /** @var File $deleteFile */
340
        foreach ($deletedFiles as $deleteFile) {
341
            $deleteFile->setStatus(File::STATUS_DELETED);
342
            $this->fileRepository->update($deleteFile);
343
        }
344
345
        // Add or update files
346
        /** @var File $newFile */
347
        foreach ($newFiles as $newFile) {
348
349
            if ($newFile->getUID()) {
350
                $this->fileRepository->update($newFile);
351
            } else {
352
                $document->addFile($newFile);
353
            }
354
355
        }
356
    }
357
358
    /**
359
     * Removes the document from the local database.
360
     *
361
     * @param $document
362
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
363
     */
364
    protected function removeDocument($document)
365
    {
366
        $files = $this->fileRepository->findByDocument($document->getUid());
0 ignored issues
show
Bug introduced by
The method findByDocument() does not exist on EWW\Dpf\Domain\Repository\FileRepository. 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

366
        /** @scrutinizer ignore-call */ 
367
        $files = $this->fileRepository->findByDocument($document->getUid());
Loading history...
367
        foreach ($files as $file) {
368
            $this->fileRepository->remove($file);
369
        }
370
        $this->documentRepository->remove($document);
371
    }
372
373
374
375
    /**
376
     * @param Document $document
377
     * @param string $workflowTransition
378
     * @param array $deletedFiles
379
     * @param array $newFiles
380
     * @return string
381
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
382
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
383
     */
384
    protected function updateRemotely($document, $workflowTransition = null, $deletedFiles = [], $newFiles = [])
385
    {
386
        $lastModDate = $this->getDocumentTransferManager()->getLastModDate($document->getObjectIdentifier());
387
        $docLastModDate = $document->getRemoteLastModDate();
388
        if ($lastModDate !== $docLastModDate && !empty($docLastModDate)) {
389
            // There is a newer version in the fedora repository.
390
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
391
        }
392
393
        $this->updateFiles($document, $deletedFiles, $newFiles);
394
        $this->documentRepository->update($document);
395
396
        switch ($workflowTransition) {
397
            case DocumentWorkflow::TRANSITION_POSTPONE:
398
                $transferState = DocumentTransferManager::INACTIVATE;
399
                break;
400
401
            case DocumentWorkflow::TRANSITION_DISCARD:
402
                $transferState = DocumentTransferManager::DELETE;
403
                break;
404
405
            case DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE:
406
                $transferState = DocumentTransferManager::REVERT;
407
                break;
408
409
            default:
410
                $transferState = null;
411
                break;
412
        }
413
414
        if ($transferState) {
415
            if (!$this->getDocumentTransferManager()->delete($document, $transferState)) {
416
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
417
            }
418
        }
419
420
        if ($this->getDocumentTransferManager()->update($document)) {
421
422
            if(!$this->hasActiveEmbargo($document)){
423
                $this->removeDocument($document);
424
            } else {
425
                $document->setState(DocumentWorkflow::LOCAL_STATE_IN_PROGRESS . ':' . $document->getRemoteState());
426
            }
427
            return $document->getDocumentIdentifier();
428
        }
429
430
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
431
    }
432
433
    public function addSuggestion($editDocument, $restore = false, $comment = '') {
434
        // add new document
435
        /** @var Document $suggestionDocument */
436
        $suggestionDocument = $this->objectManager->get(Document::class);
437
        $this->documentRepository->add($suggestionDocument);
438
        $this->persistenceManager->persistAll();
439
440
        // copy properties from origin
441
        $suggestionDocument = $suggestionDocument->copy($editDocument);
442
        $suggestionDocument->setCreator($editDocument->getCreator());
443
444
        if ($suggestionDocument->isTemporary()) {
445
            $suggestionDocument->setTemporary(false);
446
        }
447
448
        if (empty($suggestionDocument->getFileData())) {
449
            // no files are linked to the document
450
            $hasFilesFlag = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $hasFilesFlag is dead and can be removed.
Loading history...
451
        }
452
453
        if ($editDocument->getObjectIdentifier()) {
454
            $suggestionDocument->setLinkedUid($editDocument->getObjectIdentifier());
455
        } else {
456
            $suggestionDocument->setLinkedUid($editDocument->getUid());
457
        }
458
459
        $suggestionDocument->setSuggestion(true);
460
        if ($comment) {
461
            $suggestionDocument->setComment($comment);
462
        }
463
464
        if ($restore) {
465
            $suggestionDocument->setTransferStatus("RESTORE");
466
        }
467
468
//        if (!$hasFilesFlag) {
469
//            // Add or update files
470
//            foreach ($documentForm->getNewFiles() as $newFile) {
471
//                if ($newFile->getUID()) {
472
//                    $this->fileRepository->update($newFile);
473
//                } else {
474
//                    $newFile->setDocument($suggestionDocument);
475
//                    $this->fileRepository->add($newFile);
476
//                }
477
//
478
//                $suggestionDocument->addFile($newFile);
479
//            }
480
//        } else {
481
//            // remove files for suggest object
482
//            $suggestionDocument->setFile($this->objectManager->get(ObjectStorage::class));
483
//        }
484
485
        try {
486
//            $suggestionDocument->setCreator($this->security->getUser()->getUid());
487
            $this->documentRepository->add($suggestionDocument);
488
        } catch (\Throwable $t) {
489
            return null;
490
        }
491
492
        return $suggestionDocument;
493
494
    }
495
496
    /**
497
     * @param $document
498
     * @return bool (true: if no embargo is set or embargo is expired, false: embargo is active)
499
     * @throws \Exception
500
     */
501
    protected function hasActiveEmbargo($document)
502
    {
503
        $currentDate = new \DateTime('now');
504
        if($currentDate > $document->getEmbargoDate()){
505
            // embargo is expired
506
            return false;
507
        } else {
508
            return true;
509
        }
510
511
    }
512
513
514
    /**
515
     * @param Document $document
516
     * @return FrontendUser
517
     */
518
    public function getCreatorUser(Document $document) {
519
        return $this->frontendUserRepository->findByUid($document->getCreator());
520
    }
521
522
    /**
523
     * @param Document $document
524
     * @return array
525
     */
526
    public function getAssignedUsers(Document $document)
527
    {
528
        $assignedUsers = [];
529
530
        foreach ($document->getAssignedFobIdentifiers() as $fobId) {
531
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
0 ignored issues
show
Bug introduced by
The method findByFisPersId() does not exist on EWW\Dpf\Domain\Repository\FrontendUserRepository. 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

531
            /** @scrutinizer ignore-call */ 
532
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
Loading history...
532
            foreach ($feUsers as $feUser) {
533
534
                $assignedUsers[$feUser->getUid()] = $feUser;
535
            }
536
        }
537
538
        return $assignedUsers;
539
    }
540
541
    /**
542
     * @param Document $document
543
     * @return array
544
     */
545
    public function getNewlyAssignedUsers(Document $document)
546
    {
547
        $assignedUsers = [];
548
549
        foreach ($document->getNewlyAssignedFobIdentifiers() as $fobId) {
550
            $feUsers = $this->frontendUserRepository->findByFisPersId($fobId);
551
            foreach ($feUsers as $feUser) {
552
                $assignedUsers[$feUser->getUid()] = $feUser;
553
            }
554
        }
555
556
        return $assignedUsers;
557
    }
558
559
    /**
560
     * @param Document $document
561
     * @return array
562
     */
563
    public function getDocumentBookmarkUsers(Document $document) {
564
565
        $users = [];
566
567
        /** @var Bookmark $bookmark */
568
        $bookmarks = $this->bookmarkRepository->findByDocumentIdentifier($document->getDocumentIdentifier());
569
        foreach ($bookmarks as $bookmark) {
570
            $feUser = $this->frontendUserRepository->findByUid($bookmark->getFeUserUid());
571
            $users[$feUser->getUid()] = $feUser;
572
        }
573
574
        return $users;
575
    }
576
577
    /**
578
     * @param Document $document
579
     * @return array
580
     */
581
    public function getUpdateNotificationRecipients(Document $document)
582
    {
583
        $users = [];
584
585
        if ($document->getCreator()) {
586
            $users[$this->getCreatorUser($document)->getUid()] = $this->getCreatorUser($document);
587
        }
588
589
        foreach ($this->getAssignedUsers($document) as $user) {
590
            $users[$user->getUid()] = $user;
591
        }
592
593
        foreach ($this->getDocumentBookmarkUsers($document) as $user) {
594
            $users[$user->getUid()] = $user;
595
        }
596
597
        $recipients = [];
598
599
        /** @var FrontendUser $recipient */
600
        foreach ($users as $recipient) {
601
            if (
602
                $recipient->getUid() !== $this->security->getUser()->getUid() &&
603
                $document->getState() !== DocumentWorkflow::STATE_NEW_NONE &&
604
                !(
605
                    in_array(
606
                        $recipient->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
607
                    ) ||
608
                    $document->isStateChange() &&
609
                    $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
610
                )
611
            ) {
612
613
                if ($recipient->isNotifyOnChanges()) {
614
615
                    if (
616
                        $recipient->isNotifyPersonalLink() ||
617
                        $recipient->isNotifyStatusChange() ||
618
                        $recipient->isNotifyFulltextPublished()
619
                    ) {
620
                        if (
621
                            $recipient->isNotifyPersonalLink() &&
622
                            in_array(
623
                                $recipient->getFisPersId(), $document->getAssignedFobIdentifiers()
624
                            ) &&
625
                            !(
626
                                $recipient->isNotifyNewPublicationMyPublication() &&
627
                                (
628
                                    in_array(
629
                                        $recipient->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
630
                                    ) ||
631
                                    $document->isStateChange() &&
632
                                    $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
633
                                )
634
                            )
635
                        ) {
636
                            $recipients[$recipient->getUid()] = $recipient;
637
                        }
638
639
                        if ($recipient->isNotifyStatusChange() && $document->isStateChange()) {
640
                            $recipients[$recipient->getUid()] = $recipient;
641
                        }
642
643
                        if ($recipient->isNotifyFulltextPublished()) {
644
645
                            $embargoDate = $document->getEmbargoDate();
646
                            $currentDate = new \DateTime('now');
647
648
                            $fulltextPublished = false;
649
                            foreach ($document->getFile() as $file) {
650
                                if ($file->getStatus() != 'added') {
651
                                    $fulltextPublished = false;
652
                                    break;
653
                                } else {
654
                                    $fulltextPublished = true;
655
                                }
656
                            }
657
658
                            if (
659
                                $document->getState() === DocumentWorkflow::STATE_NONE_ACTIVE &&
660
                                $fulltextPublished &&
661
                                (
662
                                   empty($embargoDate) ||
663
                                   $embargoDate < $currentDate
664
                                )
665
                            ) {
666
                                $recipients[$recipient->getUid()] = $recipient;
667
                            }
668
                        }
669
670
                    } else {
671
                       $recipients[$recipient->getUid()] = $recipient;
672
                    }
673
                }
674
            }
675
        }
676
        return $recipients;
677
    }
678
679
    /**
680
     * @param Document $document
681
     * @return array
682
     */
683
    public function getNewPublicationNotificationRecipients(Document $document)
684
    {
685
        $users = [];
686
687
        /** @var FrontendUser $user */
688
        foreach ($this->getAssignedUsers($document) as $user) {
689
            if (
690
                $user->getUid() !== $this->security->getUser()->getUid() &&
691
                $document->getState() !== DocumentWorkflow::STATE_NEW_NONE &&
692
                $user->getUid() !== $document->getCreator()
693
            ) {
694
                if (
695
                    $user->isNotifyNewPublicationMyPublication() &&
696
                    (
697
                        in_array(
698
                            $user->getFisPersId(), $document->getNewlyAssignedFobIdentifiers()
699
                        ) ||
700
                        $document->isStateChange() &&
701
                        $document->getState() === DocumentWorkflow::STATE_REGISTERED_NONE
702
                    )
703
                ) {
704
                    $users[$user->getUid()] = $user;
705
                }
706
            }
707
        }
708
709
        return $users;
710
    }
711
}
712
713