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

DocumentManager::addSuggestion()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 60
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 64
nop 3
dl 0
loc 60
rs 8.6186
c 0
b 0
f 0

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\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
        // FIXME: Code duplication should be removed and it should be encapsulated or made configurable.
208
        if (strlen($document->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) {
209
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
210
        }
211
212
        /** @var \Symfony\Component\Workflow\Workflow $workflow */
213
        $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow();
214
215
        if ($workflowTransition) {
216
            if (!$workflow->can($document, $workflowTransition)) {
217
                return false;
218
            }
219
            $workflow->apply($document, $workflowTransition);
220
        }
221
222
        if ($document->isSuggestion()) {
223
224
            // if local suggestion copy
225
            $updateResult = false;
226
227
        } elseif ($document->isTemporaryCopy()) {
228
229
            // if temporary working copy
230
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
231
232
        } elseif (
233
            $document->isWorkingCopy() &&
234
            (
235
                $workflowTransition === DocumentWorkflow::TRANSITION_POSTPONE ||
236
                $workflowTransition === DocumentWorkflow::TRANSITION_DISCARD ||
237
                $workflowTransition === DocumentWorkflow::TRANSITION_RELEASE_ACTIVATE
238
            )
239
        ) {
240
            // if local working copy with state change
241
            $updateResult = $this->updateRemotely($document, $workflowTransition, $deletedFiles, $newFiles);
242
243
        } elseif ($document->isWorkingCopy()) {
244
245
            // if local working copy with no state change
246
            $this->updateFiles($document, $deletedFiles, $newFiles);
247
            $this->documentRepository->update($document);
248
            $updateResult = $document->getDocumentIdentifier();
249
250
        } elseif ($workflowTransition == DocumentWorkflow::TRANSITION_RELEASE_PUBLISH) {
251
            // Fedora ingest
252
            if ($ingestedDocument = $this->getDocumentTransferManager()->ingest($document)) {
253
254
                // After ingest all related bookmarks need an update of the identifier into an fedora object identifier.
255
                if ($ingestedDocument instanceof Document) {
0 ignored issues
show
introduced by
$ingestedDocument is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
256
                    /** @var Bookmark $bookmark */
257
                    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

257
                    foreach ($this->bookmarkRepository->/** @scrutinizer ignore-call */ findByDocumentIdentifier($ingestedDocument->getUid()) as $bookmark) {
Loading history...
258
                        $bookmark->setDocumentIdentifier($ingestedDocument->getDocumentIdentifier());
259
                        $this->bookmarkRepository->update($bookmark);
260
                    }
261
                    $this->persistenceManager->persistAll();
262
                } else {
263
                    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

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

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

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