Passed
Pull Request — master (#166)
by
unknown
21:05
created

EditingLockService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 116
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A lock() 0 14 2
A unlock() 0 6 1
A unlockOutdatedLocks() 0 8 2
A unlockAllByEditor() 0 8 2
A getLockedDocumentIdentifiersByUserUid() 0 11 2
A isLocked() 0 11 3
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
use EWW\Dpf\Domain\Model\EditingLock;
18
19
class EditingLockService
20
{
21
    /**
22
     * editingLockRepository
23
     *
24
     * @var \EWW\Dpf\Domain\Repository\EditingLockRepository
25
     * @inject
26
     */
27
    protected $editingLockRepository = null;
28
29
    /**
30
     * persistence manager
31
     *
32
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
33
     * @inject
34
     */
35
    protected $persistenceManager;
36
37
    /**
38
     * Locks editing for the given document identifier and all users except the given user.
39
     *
40
     * @param string $documentIdentifier
41
     * @param int $userUid
42
     * @return bool
43
     */
44
    public function lock($documentIdentifier, $userUid)
45
    {
46
        if ($this->editingLockRepository->findOneByDocumentIdentifier($documentIdentifier)) {
0 ignored issues
show
Bug introduced by
The method findOneByDocumentIdentifier() does not exist on EWW\Dpf\Domain\Repository\EditingLockRepository. 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

46
        if ($this->editingLockRepository->/** @scrutinizer ignore-call */ findOneByDocumentIdentifier($documentIdentifier)) {
Loading history...
47
            return FALSE;
48
        }
49
50
        /** @var \EWW\Dpf\Domain\Model\EditingLock $editingLock */
51
        $editingLock = new EditingLock();
52
        $editingLock->setEditorUid($userUid);
53
        $editingLock->setDocumentIdentifier($documentIdentifier);
0 ignored issues
show
Bug introduced by
$documentIdentifier of type string is incompatible with the type integer expected by parameter $documentIdentifier of EWW\Dpf\Domain\Model\Edi...setDocumentIdentifier(). ( Ignorable by Annotation )

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

53
        $editingLock->setDocumentIdentifier(/** @scrutinizer ignore-type */ $documentIdentifier);
Loading history...
54
        $this->editingLockRepository->add($editingLock);
55
        $this->persistenceManager->persistAll();
56
57
        return TRUE;
58
    }
59
60
    /**
61
     * Unlocks editing for the given document identifier
62
     *
63
     * @param string $documentIdentifier
64
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
65
     */
66
    public function unlock($documentIdentifier)
67
    {
68
        /** @var \EWW\Dpf\Domain\Model\EditingLock $editingLock */
69
        $editingLock = $this->editingLockRepository->findOneByDocumentIdentifyer($documentIdentifier);
0 ignored issues
show
Bug introduced by
The method findOneByDocumentIdentifyer() does not exist on EWW\Dpf\Domain\Repository\EditingLockRepository. 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

69
        /** @scrutinizer ignore-call */ 
70
        $editingLock = $this->editingLockRepository->findOneByDocumentIdentifyer($documentIdentifier);
Loading history...
70
        $this->editingLockRepository->remove($editingLock);
71
        $this->persistenceManager->persistAll();
72
    }
73
74
    /**
75
     * Checks if editing of the specified document (by its document identifier) is locked for the given user uid.
76
     *
77
     * @param string $documentIdentifier
78
     * @param int $userUid
79
     * @return bool
80
     */
81
    public function isLocked($documentIdentifier, $userUid)
82
    {
83
        $locks = $this->editingLockRepository->findByDocumentIdentifier($documentIdentifier);
0 ignored issues
show
Bug introduced by
The method findByDocumentIdentifier() does not exist on EWW\Dpf\Domain\Repository\EditingLockRepository. 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

83
        /** @scrutinizer ignore-call */ 
84
        $locks = $this->editingLockRepository->findByDocumentIdentifier($documentIdentifier);
Loading history...
84
85
        /** @var  \EWW\Dpf\Domain\Model\EditingLock $lock */
86
        foreach ($locks as $lock) {
87
            if ($lock->getEditorUid() != $userUid) {
88
                return TRUE;
89
            }
90
        }
91
        return FALSE;
92
    }
93
94
    /**
95
     * Unlocks all outdated editing locks.
96
     *
97
     * @param int $timeout : Time interval (in seconds) in which locks are not outdated, default is 1 hour.
98
     */
99
    public function unlockOutdatedLocks($timeout = 3600)
100
    {
101
        // Unlock outdated editing locks.
102
        $outdatedLocks = $this->editingLockRepository->findOutdatedLocks($timeout);
103
        foreach ($outdatedLocks as $outdatedLock) {
104
            $this->editingLockRepository->remove($outdatedLock);
105
        }
106
        $this->persistenceManager->persistAll();
107
    }
108
109
    /**
110
     * Unlocks all editing locks of the given user (editor uid).
111
     *
112
     * @param int $editorUid
113
     */
114
    public function unlockAllByEditor($editorUid)
115
    {
116
        $locks = $this->editingLockRepository->findByEditorUid($editorUid);
0 ignored issues
show
Bug introduced by
The method findByEditorUid() does not exist on EWW\Dpf\Domain\Repository\EditingLockRepository. 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

116
        /** @scrutinizer ignore-call */ 
117
        $locks = $this->editingLockRepository->findByEditorUid($editorUid);
Loading history...
117
        foreach ($locks as $lock) {
118
            $this->editingLockRepository->remove($lock);
119
        }
120
121
        $this->persistenceManager->persistAll();
122
    }
123
124
    public function getLockedDocumentIdentifiersByUserUid($userUid)
125
    {
126
        $identifiers = array();
127
128
        $locks = $this->editingLockRepository->findByEditorUid($userUid);
129
        /** @var  \EWW\Dpf\Domain\Model\EditingLock $lock */
130
        foreach ($locks as $lock) {
131
            $identifiers[] = $lock->getDocumentIdentifier();
132
        }
133
134
        return $identifiers;
135
    }
136
137
}