Issues (3936)

Classes/Services/Document/DocumentCleaner.php (1 issue)

Labels
Severity
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
     * @TYPO3\CMS\Extbase\Annotation\Inject
25
     */
26
    protected $documentRepository = null;
27
28
    /**
29
     * editingLockService
30
     *
31
     * @var \EWW\Dpf\Services\Document\EditingLockService
32
     * @TYPO3\CMS\Extbase\Annotation\Inject
33
     */
34
    protected $editingLockService = null;
35
36
    /**
37
     * persistence manager
38
     *
39
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
40
     * @TYPO3\CMS\Extbase\Annotation\Inject
41
     */
42
    protected $persistenceManager;
43
44
    /**
45
     * security
46
     *
47
     * @var \EWW\Dpf\Security\Security
48
     * @TYPO3\CMS\Extbase\Annotation\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
                'duplicateAction'
68
            ],
69
            \EWW\Dpf\Controller\DocumentFormBackofficeController::class => [
70
                'newAction',
71
                'editAction',
72
                'cancelEditAction',
73
                'updateAction',
74
                'updateLocallyAction',
75
                'updateRemoteAction',
76
                'createSuggestionDocumentAction'
77
            ]
78
        ];
79
80
        $this->cleanUpOutdatedTemporaryDocuments();
81
82
        if (
83
            !array_key_exists($controllerClass, $excludeActions) ||
84
            !in_array($actionMethodName, $excludeActions[$controllerClass])
85
        ) {
86
            // Remove all locked temporary documents of the current user.
87
            $feUserUid = $this->security->getUser()->getUid();
88
            $documents = $this->documentRepository->findByTemporary(TRUE);
0 ignored issues
show
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

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