Passed
Pull Request — master (#166)
by
unknown
14:46 queued 04:32
created

cleanUpTemporaryDocumentsByFeUser()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 26
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 38
rs 9.1928

1 Method

Rating   Name   Duplication   Size   Complexity  
A DocumentCleaner::cleanUpOutdatedTemporaryDocuments() 0 9 2
1
<?php
2
namespace EWW\Dpf\Services\Document;
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
class DocumentCleaner
18
{
19
20
    /**
21
     * clientRepository
22
     *
23
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
24
     * @inject
25
     */
26
    protected $documentRepository = null;
27
28
    /**
29
     * editingLockService
30
     *
31
     * @var \EWW\Dpf\Services\Document\EditingLockService
32
     * @inject
33
     */
34
    protected $editingLockService = null;
35
36
    /**
37
     * persistence manager
38
     *
39
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
40
     * @inject
41
     */
42
    protected $persistenceManager;
43
44
    /**
45
     * security
46
     *
47
     * @var \EWW\Dpf\Security\Security
48
     * @inject
49
     */
50
    protected $security = null;
51
52
    /**
53
     * @param string $actionMethodName
54
     * @param string $controllerClass
55
     * @param \EWW\Dpf\Domain\Model\Document $openedDocument
56
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
57
     */
58
    public function cleanUpDocuments($actionMethodName, $controllerClass)
59
    {
60
        $excludeActions = [
61
            \EWW\Dpf\Controller\DocumentController::class => [
62
                'showDetailsAction',
63
                'postponeAction',
64
                'discardAction',
65
                'releaseActivateAction',
66
                'suggestModificationAction'
67
            ],
68
            \EWW\Dpf\Controller\DocumentFormBackofficeController::class => [
69
                'editAction',
70
                'cancelEditAction',
71
                'updateAction',
72
                'updateLocallyAction',
73
                'updateRemoteAction',
74
                'createSuggestionDocumentAction'
75
            ]
76
        ];
77
78
        $this->cleanUpOutdatedTemporaryDocuments();
79
80
        if (
81
            !array_key_exists($controllerClass, $excludeActions) ||
82
            !in_array($actionMethodName, $excludeActions[$controllerClass])
83
        ) {
84
            // Remove all locked temporary documents of the current user.
85
            $feUserUid = $this->security->getUser()->getUid();
86
            $documents = $this->documentRepository->findByTemporary(TRUE);
0 ignored issues
show
Bug introduced by
The method findByTemporary() does not exist on EWW\Dpf\Domain\Repository\DocumentRepository. 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

86
            /** @scrutinizer ignore-call */ 
87
            $documents = $this->documentRepository->findByTemporary(TRUE);
Loading history...
87
            $docIdentifiers = $this->editingLockService->getLockedDocumentIdentifiersByUserUid($feUserUid);
88
89
            foreach ($documents as $document) {
90
                /** @var  \EWW\Dpf\Domain\Model\Document $document */
91
                if (in_array($document->getDocumentIdentifier(), $docIdentifiers)) {
92
                    $this->documentRepository->remove($document);
93
                }
94
            }
95
        }
96
97
        $this->cleanUpEditingLocks($actionMethodName, $controllerClass);
98
    }
99
100
    /**
101
     * Removes all outdated temporary documents and unlocks all outdated editing locks.
102
     *
103
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
104
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
105
     */
106
    protected function cleanUpOutdatedTemporaryDocuments()
107
    {
108
        // Remove outdated temporary documents from the document table.
109
        $outdatedTemporaryDocuments = $this->documentRepository->findOutdatedTemporaryDocuments(3600);
110
        foreach ($outdatedTemporaryDocuments as $outdatedTemporaryDocument) {
111
            /** @var  \EWW\Dpf\Domain\Model\Document $outdatedTemporaryDocument */
112
            $this->documentRepository->remove($outdatedTemporaryDocument);
113
        }
114
        $this->persistenceManager->persistAll();
115
    }
116
117
    /**
118
     * Unlocks all editing locks of the current user.
119
     */
120
    protected function cleanUpEditingLocks($controllerClass, $actionMethodName)
121
    {
122
        $excludeActions = [
123
            \EWW\Dpf\Controller\DocumentController::class => [
124
            ],
125
            \EWW\Dpf\Controller\DocumentFormBackofficeController::class => [
126
            ]
127
        ];
128
129
        // Unlock outdated editing locks.
130
        $this->editingLockService->unlockOutdatedLocks(3600);
131
132
        if (
133
            !array_key_exists($controllerClass, $excludeActions) ||
134
            !in_array($actionMethodName, $excludeActions[$controllerClass])
135
        ) {
136
            $feUserUid = $this->security->getUser()->getUid();
137
            $this->editingLockService->unlockAllByEditor($feUserUid);
138
        }
139
    }
140
141
}
142