Passed
Pull Request — master (#155)
by
unknown
08:39
created

findWorkingCopyByObjectIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
1
<?php
2
namespace EWW\Dpf\Domain\Repository;
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\Workflow\DocumentWorkflow;
18
use \EWW\Dpf\Security\Security;
19
20
/**
21
 * The repository for Documents
22
 */
23
class DocumentRepository extends \EWW\Dpf\Domain\Repository\AbstractRepository
24
{
25
26
    /**
27
     * Finds all documents of the given user role filtered by owner uid
28
     *
29
     * @param string role : The kitodo user role (Security::ROLE_LIBRARIAN, Security::ROLE_RESEARCHER)
0 ignored issues
show
Bug introduced by
The type EWW\Dpf\Domain\Repository\role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     * @param int $ownerUid
31
     * @param array $stateFilters
32
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
33
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
34
     */
35
    public function findAllByRole($role, $ownerUid, $stateFilters = array())
36
    {
37
        $query = $this->createQuery();
38
        $constraintsOr = array();
39
        $constraintsAnd = array();
40
41
        switch ($role) {
42
43
            case Security::ROLE_LIBRARIAN:
44
45
                $constraintsOr[] = $query->logicalAnd(
46
                array(
47
                $query->equals('state', DocumentWorkflow::STATE_NEW_NONE),
48
                $query->equals('owner', $ownerUid)
49
                )
50
                );
51
52
                $constraintsOr[] = $query->logicalAnd(
53
                $query->logicalNot(
54
                $query->equals('state', DocumentWorkflow::STATE_NEW_NONE)
55
                )
56
                );
57
58
                $constraintsAnd[] = $query->logicalOr($constraintsOr);
59
60
                if ($stateFilters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $stateFilters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
                $constraintsAnd[] = $query->in('state', $stateFilters);
62
                }
63
64
                $constraintsAnd[] = $query->equals('suggestion', false);
65
66
                break;
67
68
            case Security::ROLE_RESEARCHER:
69
70
                $constraintsAnd = array(
71
                    $query->equals('owner', $ownerUid),
72
                    $query->equals('suggestion', 0)
73
                );
74
75
                if ($stateFilters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $stateFilters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76
                    $constraintsAnd[] = $query->in('state', $stateFilters);
77
                }
78
79
                break;
80
        }
81
82
        $constraintsAnd[] = $query->equals('temporary', false);
83
        $query->matching($query->logicalAnd($constraintsAnd));
84
85
        $query->setOrderings(
86
            array('transfer_date' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
87
        );
88
89
        return $query->execute();
90
    }
91
92
    /**
93
     * Finds all suggestion documents of the given user role filtered by owner uid
94
     *
95
     * @param string $role : The kitodo user role (Security::ROLE_LIBRARIAN, Security::ROLE_RESEARCHER)
96
     * @param int $ownerUid
97
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
98
     */
99
    public function findAllDocumentSuggestions($role, $ownerUid) {
100
        $query = $this->createQuery();
101
102
        switch ($role) {
103
104
            case Security::ROLE_LIBRARIAN:
105
                $query->matching(
106
                    $query->equals('suggestion', true)
107
                );
108
                break;
109
110
            case Security::ROLE_RESEARCHER:
111
                $query->matching(
112
                    $query->logicalAnd(
113
                        array(
114
                            $query->equals('suggestion', true),
115
                            $query->equals('owner', $ownerUid)
116
                        )
117
                    )
118
                );
119
                break;
120
        }
121
        return $query->execute();
122
    }
123
124
    /**
125
     * @param boolean $temporary
126
     * @return array
127
     */
128
    public function getObjectIdentifiers($temporary = FALSE)
0 ignored issues
show
Unused Code introduced by
The parameter $temporary is not used and could be removed. ( Ignorable by Annotation )

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

128
    public function getObjectIdentifiers(/** @scrutinizer ignore-unused */ $temporary = FALSE)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
    {
130
        $query = $this->createQuery();
131
132
        $constraints = array(
133
            $query->logicalNot($query->equals('object_identifier', '')),
134
            $query->logicalNot($query->equals('object_identifier', NULL)));
135
136
        if (count($constraints)) {
137
            $constraints[] = $query->logicalAnd(
138
                $query->logicalNot(
139
                    $query->logicalOr(
140
                        $query->equals('temporary', TRUE),
141
                        $query->equals('suggestion', TRUE)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...yInterface::logicalOr() has too many arguments starting with $query->equals('suggestion', TRUE). ( Ignorable by Annotation )

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

141
                    $query->/** @scrutinizer ignore-call */ 
142
                            logicalOr(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
142
                    )
143
                )
144
            );
145
            $query->matching($query->logicalAnd($constraints));
146
        }
147
148
        $result = $query->execute();
149
150
        $objectIdentifiers = array();
151
152
        foreach ($result as $document) {
153
            $objectIdentifiers[$document->getObjectIdentifier()] = $document->getObjectIdentifier();
154
        }
155
156
        return $objectIdentifiers;
157
    }
158
159
    /**
160
     * Finds all documents without a process number,
161
     * storagePID will be ignored.
162
     *
163
     * @return array The found Document Objects
164
     */
165
    public function findDocumentsWithoutProcessNumber()
166
    {
167
        $query = $this->createQuery();
168
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
169
170
        $constraints = array();
171
        $constraints[] =  $query->equals('process_number', '');
172
        $constraints[] =  $query->equals('process_number', NULL);
173
174
        if (count($constraints)) {
175
            $query->matching(
176
                $query->logicalAnd(
177
                    $query->equals('temporary', FALSE),
178
                    $query->logicalOr($constraints)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->logicalOr($constraints). ( Ignorable by Annotation )

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

178
                $query->/** @scrutinizer ignore-call */ 
179
                        logicalAnd(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
179
                )
180
            );
181
        }
182
183
        return $query->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute() also could return the type TYPO3\CMS\Extbase\Persistence\QueryResultInterface which is incompatible with the documented return type array.
Loading history...
184
    }
185
186
    /**
187
     * Finds all outdated temporary documents,
188
     *
189
     * @param integer $timeout : Time interval (in seconds) in which documents are not outdated.
190
     * @return array The found Document Objects
191
     */
192
    public function findOutdatedTemporaryDocuments($timeout = 3600)
193
    {
194
        $query = $this->createQuery();
195
196
        $dateTimeObj= new \DateTime();
197
        $dateTimeObj->sub(new \DateInterval("PT".$timeout."S"));
198
199
        $constraints = array();
200
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
201
202
        $query->matching(
203
            $query->logicalAnd(
204
                $query->equals('temporary', TRUE),
205
                $query->logicalOr($constraints)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->logicalOr($constraints). ( Ignorable by Annotation )

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

205
            $query->/** @scrutinizer ignore-call */ 
206
                    logicalAnd(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
206
            )
207
        );
208
209
        return $query->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute() also could return the type TYPO3\CMS\Extbase\Persistence\QueryResultInterface which is incompatible with the documented return type array.
Loading history...
210
    }
211
212
    /**
213
     * Finds all outdated locked documents,
214
     *
215
     * @param integer $timeout : Time interval (in seconds) in which documents are not outdated.
216
     * @return array The found Document Objects
217
     */
218
    public function findOutdatedLockedDocuments($timeout = 3600)
219
    {
220
        $query = $this->createQuery();
221
222
        $dateTimeObj= new \DateTime();
223
        $dateTimeObj->sub(new \DateInterval("PT".$timeout."S"));
224
225
        $constraints = array();
226
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
227
228
        $query->matching(
229
            $query->logicalAnd(
230
                $query->logicalNot($query->equals('editor_uid', 0)),
231
                $query->logicalOr($constraints)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->logicalOr($constraints). ( Ignorable by Annotation )

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

231
            $query->/** @scrutinizer ignore-call */ 
232
                    logicalAnd(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
232
            )
233
        );
234
235
        return $query->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute() also could return the type TYPO3\CMS\Extbase\Persistence\QueryResultInterface which is incompatible with the documented return type array.
Loading history...
236
    }
237
238
239
    /**
240
     * @param string $objectIdentifier
241
     * @return array
242
     */
243
    public function findWorkingCopyByObjectIdentifier($objectIdentifier)
244
    {
245
        $query = $this->createQuery();
246
247
        $constraints = array(
248
            $query->equals('object_identifier', $objectIdentifier),
249
            $query->logicalNot($query->equals('temporary', TRUE)),
250
            $query->logicalNot($query->equals('suggestion', TRUE))
251
        );
252
253
        $query->matching($query->logicalAnd($constraints));
254
255
        return $query->execute()->getFirst();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute()->getFirst() returns the type object which is incompatible with the documented return type array.
Loading history...
256
    }
257
258
    /**
259
     * @param string $identifier
260
     * @return array
261
     */
262
    public function findWorkingCopy($identifier)
263
    {
264
        $query = $this->createQuery();
265
266
        if (is_numeric($identifier)) {
267
            $constraints = [
268
                $query->equals('uid', $identifier)
269
            ];
270
        } else {
271
            $constraints = [
272
                $query->equals('object_identifier', $identifier)
273
            ];
274
        }
275
276
        $constraints[] = $query->logicalNot($query->equals('temporary', TRUE));
277
        $constraints[] = $query->logicalNot($query->equals('suggestion', TRUE));
278
279
        $query->matching($query->logicalAnd($constraints));
280
281
        return $query->execute()->getFirst();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute()->getFirst() returns the type object which is incompatible with the documented return type array.
Loading history...
282
    }
283
284
}
285