Passed
Push — master ( 134b43...f02d33 )
by Ralf
09:50
created

DocumentRepository::updateCreator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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\Model\Document;
18
use \EWW\Dpf\Domain\Workflow\DocumentWorkflow;
19
use \EWW\Dpf\Security\Security;
20
use \EWW\Dpf\Services\Identifier\Identifier;
21
22
/**
23
 * The repository for Documents
24
 */
25
class DocumentRepository extends \EWW\Dpf\Domain\Repository\AbstractRepository
26
{
27
    /**
28
     * Finds all suggestion documents of the given user role filtered by creator feuser uid
29
     *
30
     * @param string $role : The kitodo user role (Security::ROLE_LIBRARIAN, Security::ROLE_RESEARCHER)
31
     * @param int $creatorUid
32
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
33
     */
34
    public function findAllDocumentSuggestions($role, $creatorUid)
35
    {
36
        $query = $this->createQuery();
37
38
        switch ($role) {
39
40
            case Security::ROLE_LIBRARIAN:
41
                $query->matching(
42
                    $query->equals('suggestion', true)
43
                );
44
                break;
45
46
            case Security::ROLE_RESEARCHER:
47
                $query->matching(
48
                    $query->logicalAnd(
49
                        array(
50
                            $query->equals('suggestion', true),
51
                            $query->equals('creator', $creatorUid)
52
                        )
53
                    )
54
                );
55
                break;
56
        }
57
        return $query->execute();
58
    }
59
60
    /**
61
     * @param boolean $temporary
62
     * @return array
63
     */
64
    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

64
    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...
65
    {
66
        $query = $this->createQuery();
67
68
        $constraints = array(
69
            $query->logicalNot($query->equals('object_identifier', '')),
70
            $query->logicalNot($query->equals('object_identifier', null))
71
        );
72
73
        if (count($constraints)) {
74
            $constraints[] = $query->logicalAnd(
75
                $query->logicalNot(
76
                    $query->logicalOr(
77
                        $query->equals('temporary', true),
78
                        $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

78
                    $query->/** @scrutinizer ignore-call */ 
79
                            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...
79
                    )
80
                )
81
            );
82
            $query->matching($query->logicalAnd($constraints));
83
        }
84
85
        $result = $query->execute();
86
87
        $objectIdentifiers = array();
88
89
        foreach ($result as $document) {
90
            $objectIdentifiers[$document->getObjectIdentifier()] = $document->getObjectIdentifier();
91
        }
92
93
        return $objectIdentifiers;
94
    }
95
96
    /**
97
     * Finds all documents without a process number,
98
     * storagePID will be ignored.
99
     *
100
     * @return array The found Document Objects
101
     */
102
    public function findDocumentsWithoutProcessNumber()
103
    {
104
        $query = $this->createQuery();
105
        $query->getQuerySettings()->setRespectStoragePage(false);
106
107
        $constraints = array();
108
        $constraints[] = $query->equals('process_number', '');
109
        $constraints[] = $query->equals('process_number', null);
110
111
        if (count($constraints)) {
112
            $query->matching(
113
                $query->logicalAnd(
114
                    $query->equals('temporary', false),
115
                    $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

115
                $query->/** @scrutinizer ignore-call */ 
116
                        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...
116
                )
117
            );
118
        }
119
120
        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...
121
    }
122
123
    /**
124
     * Finds all outdated temporary documents,
125
     *
126
     * @param integer $timeout : Time interval (in seconds) in which documents are not outdated.
127
     * @return array The found Document Objects
128
     */
129
    public function findOutdatedTemporaryDocuments($timeout = 3600)
130
    {
131
        $query = $this->createQuery();
132
133
        $dateTimeObj = new \DateTime();
134
        $dateTimeObj->sub(new \DateInterval("PT" . $timeout . "S"));
135
136
        $constraints = array();
137
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
138
139
        $query->matching(
140
            $query->logicalAnd(
141
                $query->equals('temporary', true),
142
                $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

142
            $query->/** @scrutinizer ignore-call */ 
143
                    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...
143
            )
144
        );
145
146
        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...
147
    }
148
149
    /**
150
     * Finds all outdated locked documents,
151
     *
152
     * @param integer $timeout : Time interval (in seconds) in which documents are not outdated.
153
     * @return array The found Document Objects
154
     */
155
    public function findOutdatedLockedDocuments($timeout = 3600)
156
    {
157
        $query = $this->createQuery();
158
159
        $dateTimeObj = new \DateTime();
160
        $dateTimeObj->sub(new \DateInterval("PT" . $timeout . "S"));
161
162
        $constraints = array();
163
        $constraints[] = $query->lessThan('tstamp', $dateTimeObj->getTimestamp());
164
165
        $query->matching(
166
            $query->logicalAnd(
167
                $query->logicalNot($query->equals('editor_uid', 0)),
168
                $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

168
            $query->/** @scrutinizer ignore-call */ 
169
                    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...
169
            )
170
        );
171
172
        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...
173
    }
174
175
176
    /**
177
     * @param string $objectIdentifier
178
     * @return array
179
     */
180
    public function findWorkingCopyByObjectIdentifier($objectIdentifier)
181
    {
182
        $query = $this->createQuery();
183
184
        $constraints = array(
185
            $query->equals('object_identifier', $objectIdentifier),
186
            $query->logicalNot($query->equals('temporary', true)),
187
            $query->logicalNot($query->equals('suggestion', true))
188
        );
189
190
        $query->matching($query->logicalAnd($constraints));
191
192
        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...
193
    }
194
195
    /**
196
     * @param string $identifier
197
     * @return Document
198
     */
199
    public function findByIdentifier($identifier)
200
    {
201
        $query = $this->createQuery();
202
203
        if (Identifier::isUid($identifier)) {
204
            $constraints = [
205
                $query->equals('uid', $identifier)
206
            ];
207
        } elseif (Identifier::isFedoraPid($identifier)) {
208
            $constraints = [
209
                $query->logicalAnd(
210
                    $query->equals('object_identifier', $identifier),
211
                    $query->equals('suggestion', false)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->equals('suggestion', false). ( Ignorable by Annotation )

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

211
                $query->/** @scrutinizer ignore-call */ 
212
                        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...
212
                )
213
            ];
214
        } elseif (Identifier::isProcessNumber($identifier)) {
215
            $constraints = [
216
                $query->logicalAnd(
217
                    $query->equals('process_number', $identifier),
218
                    $query->equals('suggestion', false)
219
                )
220
            ];
221
        } else {
222
            return null;
223
        }
224
225
        $query->setOrderings(array("tstamp" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
226
        $query->matching($query->logicalAnd($constraints));
227
228
        return $query->execute()->getFirst();
229
    }
230
231
    /**
232
     * @param string $identifier
233
     * @param bool $includeTemporary
234
     * @param bool $includeSuggestion
235
     * @return Document
236
     */
237
    public function findWorkingCopy($identifier, $includeTemporary = false, $includeSuggestion = false)
238
    {
239
        $query = $this->createQuery();
240
241
        if (is_numeric($identifier)) {
242
            $constraints = [
243
                $query->equals('uid', $identifier)
244
            ];
245
        } else {
246
            $constraints = [
247
                $query->equals('object_identifier', $identifier)
248
            ];
249
        }
250
251
        $constraints[] = $query->equals('temporary', $includeTemporary);
252
        $constraints[] = $query->equals('suggestion', $includeSuggestion);
253
254
        $query->matching($query->logicalAnd($constraints));
255
256
        return $query->execute()->getFirst();
257
    }
258
259
260
    /**
261
     * @param object $modifiedObject The modified object
262
     * @throws \Exception
263
     */
264
    public function update($modifiedObject)
265
    {
266
        /** @var Document $document */
267
        $document = $modifiedObject;
268
269
        if (trim($document->getObjectIdentifier()) && !$document->isTemporary() && !$document->isSuggestion()) {
270
            $query = $this->createQuery();
271
            $constraints[] = $query->equals('object_identifier', trim($document->getObjectIdentifier()));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$constraints was never initialized. Although not strictly required by PHP, it is generally a good practice to add $constraints = array(); before regardless.
Loading history...
272
            $constraints[] = $query->equals('temporary', false);
273
            $constraints[] = $query->equals('suggestion', false);
274
            $query->matching($query->logicalAnd($constraints));
275
276
            /** @var Document $workingCopy */
277
            foreach ($query->execute() as $workingCopy) {
278
                if ($workingCopy->getUid() !== $document->getUid()) {
279
                    throw new \Exception(
280
                        "Working copy for " . $document->getObjectIdentifier() . " already exists."
281
                    );
282
                }
283
            }
284
        }
285
286
        parent::update($document);
287
    }
288
289
290
    public function updateCreator()
291
    {
292
293
        $query = $this->createQuery();
294
        $query->statement("update tx_dpf_domain_model_document set creator = owner");
295
        $query->execute();
296
297
    }
298
299
    /**
300
     * Finds all records with embargo date
301
     * @return mixed
302
     */
303
    public function crossClientEmbargoFindAll()
304
    {
305
        /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
306
        $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
307
        $querySettings->setRespectStoragePage(false);
308
        $this->setDefaultQuerySettings($querySettings);
309
        $query = $this->createQuery();
310
        $query->matching(
311
            $query->logicalAnd(
312
                array(
313
                    $query->logicalNot(
314
                        $query->equals('embargo_date', 0)
315
                    )
316
                )
317
            )
318
        );
319
        return $query->execute();
320
    }
321
322
    /**
323
     * @param string $identifier
324
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
325
     */
326
    public function searchForIdentifier(string $identifier)
327
    {
328
        $identifier = str_replace("*", "%", $identifier);
329
330
        $query = $this->createQuery();
331
332
            $constraints[] =
0 ignored issues
show
Comprehensibility Best Practice introduced by
$constraints was never initialized. Although not strictly required by PHP, it is generally a good practice to add $constraints = array(); before regardless.
Loading history...
333
                $query->logicalAnd(
334
                    $query->like('uid', $identifier),
335
                    $query->equals('suggestion', false)
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->equals('suggestion', false). ( Ignorable by Annotation )

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

335
            /** @scrutinizer ignore-call */ 
336
            $constraints[] =

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...
336
                );
337
338
            $constraints[] =
339
                $query->logicalAnd(
340
                    $query->like('object_identifier', $identifier),
341
                    $query->equals('suggestion', false)
342
                );
343
344
            $constraints[] =
345
                $query->logicalAnd(
346
                    $query->like('process_number', $identifier),
347
                    $query->equals('suggestion', false)
348
                );
349
350
        $query->setOrderings(array("tstamp" => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
351
        $query->matching($query->logicalOr($constraints));
352
353
        return $query->execute();
354
    }
355
}
356