Passed
Pull Request — master (#155)
by
unknown
09:50
created

DocumentRepository::findAllOfAResearcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 21
rs 9.9
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
19
/**
20
 * The repository for Documents
21
 */
22
class DocumentRepository extends \EWW\Dpf\Domain\Repository\AbstractRepository
23
{
24
25
    /**
26
     *
27
     * Finds all documents filtered by owner uid for user role librarian
28
     *
29
     * @param int $ownerUid
30
     * @param array $stateFilters
31
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
32
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
33
     */
34
    public function findAllOfALibrarian($ownerUid, $stateFilters = array())
35
    {
36
        $query = $this->createQuery();
37
        $constraintsOr = array();
38
        $constraintsAnd = array();
39
40
        $constraintsOr[] = $query->logicalAnd(
41
            array(
42
                $query->equals('state', DocumentWorkflow::STATE_NEW_NONE),
43
                $query->equals('owner', $ownerUid)
44
            )
45
        );
46
47
        $constraintsOr[] = $query->logicalAnd(
48
            $query->logicalNot(
49
                $query->equals('state', DocumentWorkflow::STATE_NEW_NONE)
50
            )
51
        );
52
53
        $constraintsAnd[] = $query->logicalOr($constraintsOr);
54
55
        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...
56
            $constraintsAnd[] = $query->in('state', $stateFilters);
57
        }
58
59
        $constraintsAnd[] = $query->equals('suggestion', false);
60
61
        $constraintsAnd[] = $query->equals('temporary', FALSE);
62
        $query->matching($query->logicalAnd($constraintsAnd));
63
64
        $query->setOrderings(
65
            array('transfer_date' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
66
        );
67
68
        return $query->execute();
69
    }
70
71
    public function findAllLibrarianDocumentSuggestions($ownerUid) {
0 ignored issues
show
Unused Code introduced by
The parameter $ownerUid 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

71
    public function findAllLibrarianDocumentSuggestions(/** @scrutinizer ignore-unused */ $ownerUid) {

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...
72
        $query = $this->createQuery();
73
74
        $query->matching(
75
            $query->equals('suggestion', true)
76
        );
77
78
        return $query->execute();
79
    }
80
81
    public function findAllResearcherDocumentSuggestions($ownerUid) {
82
        $query = $this->createQuery();
83
        $query->matching(
84
            $query->logicalAnd(
85
                array(
86
                    $query->equals('suggestion', true),
87
                    $query->equals('owner', $ownerUid)
88
                )
89
            )
90
        );
91
92
        return $query->execute();
93
    }
94
95
    /**
96
     *
97
     * Finds all documents filtered by owner uid for user role researcher
98
     *
99
     * @param int $ownerUid
100
     * @param array $stateFilters
101
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
102
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
103
     */
104
    public function findAllOfAResearcher($ownerUid, $stateFilters = array())
105
    {
106
        $query = $this->createQuery();
107
108
        $constraintsAnd = array(
109
            $query->equals('owner', $ownerUid),
110
            $query->equals('suggestion', 0)
111
        );
112
113
        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...
114
            $constraintsAnd[] = $query->in('state', $stateFilters);
115
        }
116
117
        $constraintsAnd[] = $query->equals('temporary', FALSE);
118
        $query->matching($query->logicalAnd($constraintsAnd));
119
120
        $query->setOrderings(
121
            array('transfer_date' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
122
        );
123
124
        return $query->execute();
125
    }
126
127
    /**
128
     * @param boolean $temporary
129
     * @return array
130
     */
131
    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

131
    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...
132
    {
133
        $query = $this->createQuery();
134
135
        $constraints = array(
136
            $query->logicalNot($query->equals('object_identifier', '')),
137
            $query->logicalNot($query->equals('object_identifier', NULL)));
138
139
        if (count($constraints)) {
140
            $constraints[] = $query->logicalAnd(
141
                $query->logicalNot(
142
                    $query->logicalOr(
143
                        $query->equals('temporary', TRUE),
144
                        $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

144
                    $query->/** @scrutinizer ignore-call */ 
145
                            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...
145
                    )
146
                )
147
            );
148
            $query->matching($query->logicalAnd($constraints));
149
        }
150
151
        $result = $query->execute();
152
153
        $objectIdentifiers = array();
154
155
        foreach ($result as $document) {
156
            $objectIdentifiers[$document->getObjectIdentifier()] = $document->getObjectIdentifier();
157
        }
158
159
        return $objectIdentifiers;
160
    }
161
162
    /**
163
     * Finds all documents without a process number,
164
     * storagePID will be ignored.
165
     *
166
     * @return array The found Document Objects
167
     */
168
    public function findDocumentsWithoutProcessNumber()
169
    {
170
        $query = $this->createQuery();
171
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
172
173
        $constraints = array();
174
        $constraints[] =  $query->equals('process_number', '');
175
        $constraints[] =  $query->equals('process_number', NULL);
176
177
        if (count($constraints)) {
178
            $query->matching(
179
                $query->logicalAnd(
180
                    $query->equals('temporary', FALSE),
181
                    $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

181
                $query->/** @scrutinizer ignore-call */ 
182
                        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...
182
                )
183
            );
184
        }
185
186
        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...
187
    }
188
189
    /**
190
     * Finds all outdated temporary documents,
191
     *
192
     * @param integer $timeout
193
     * @return array The found Document Objects
194
     */
195
    public function findOutdatedTemporaryDocuments($timeout = 3600)
196
    {
197
        $query = $this->createQuery();
198
199
        $dateTimeObj= new \DateTime();
200
        $dateTimeObj->sub(new \DateInterval("PT".$timeout."S"));
201
202
        $constraints = array();
203
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
204
205
        $query->matching(
206
            $query->logicalAnd(
207
                $query->equals('temporary', TRUE),
208
                $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

208
            $query->/** @scrutinizer ignore-call */ 
209
                    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...
209
            )
210
        );
211
212
        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...
213
    }
214
215
    /**
216
     * Finds all outdated locked documents,
217
     *
218
     * @param integer $timeout
219
     * @return array The found Document Objects
220
     */
221
    public function findOutdatedLockedDocuments($timeout = 3600)
222
    {
223
        $query = $this->createQuery();
224
225
        $dateTimeObj= new \DateTime();
226
        $dateTimeObj->sub(new \DateInterval("PT".$timeout."S"));
227
228
        $constraints = array();
229
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
230
231
        $query->matching(
232
            $query->logicalAnd(
233
                $query->logicalNot($query->equals('editor_uid', 0)),
234
                $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

234
            $query->/** @scrutinizer ignore-call */ 
235
                    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...
235
            )
236
        );
237
238
        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...
239
    }
240
241
242
    /**
243
     * @param string $objectIdentifier
244
     * @return array
245
     */
246
    public function findWorkingCopyByObjectIdentifier($objectIdentifier)
247
    {
248
        $query = $this->createQuery();
249
250
        $constraints = array(
251
            $query->equals('object_identifier', $objectIdentifier),
252
            $query->logicalNot($query->equals('temporary', TRUE)),
253
            $query->logicalNot($query->equals('suggestion', TRUE))
254
        );
255
256
        $query->matching($query->logicalAnd($constraints));
257
258
        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...
259
    }
260
261
    /**
262
     * @param string $identifier
263
     * @return array
264
     */
265
    public function findWorkingCopy($identifier)
266
    {
267
        $query = $this->createQuery();
268
269
        if (is_integer($identifier)) {
0 ignored issues
show
introduced by
The condition is_integer($identifier) is always false.
Loading history...
270
            $constraints = [
271
                $query->equals('uid', $identifier)
272
            ];
273
        } else {
274
            $constraints = [
275
                $query->equals('object_identifier', $identifier)
276
            ];
277
        }
278
279
        $constraints[] = $query->logicalNot($query->equals('temporary', TRUE));
280
        $constraints[] = $query->logicalNot($query->equals('suggestion', TRUE));
281
282
        $query->matching($query->logicalAnd($constraints));
283
284
        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...
285
    }
286
287
}
288