Passed
Pull Request — master (#175)
by
unknown
08:11
created

DocumentRepository::crossClientEmbargoFindAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

165
            $query->/** @scrutinizer ignore-call */ 
166
                    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...
166
            )
167
        );
168
169
        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...
170
    }
171
172
173
    /**
174
     * @param string $objectIdentifier
175
     * @return array
176
     */
177
    public function findWorkingCopyByObjectIdentifier($objectIdentifier)
178
    {
179
        $query = $this->createQuery();
180
181
        $constraints = array(
182
            $query->equals('object_identifier', $objectIdentifier),
183
            $query->logicalNot($query->equals('temporary', TRUE)),
184
            $query->logicalNot($query->equals('suggestion', TRUE))
185
        );
186
187
        $query->matching($query->logicalAnd($constraints));
188
189
        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...
190
    }
191
192
    /**
193
     * @param string $identifier
194
     * @return Document
195
     */
196
    public function findByIdentifier($identifier)
197
    {
198
        $query = $this->createQuery();
199
200
        if (is_numeric($identifier)) {
201
            $constraints = [
202
                $query->equals('uid', $identifier)
203
            ];
204
        } else {
205
            $constraints = [
206
                $query->equals('object_identifier', $identifier)
207
            ];
208
        }
209
210
        $query->matching($query->logicalAnd($constraints));
211
212
        return $query->execute()->getFirst();
213
    }
214
215
    /**
216
     * @param string $identifier
217
     * @param bool $includeTemporary
218
     * @param bool $includeSuggestion
219
     * @return Document
220
     */
221
    public function findWorkingCopy($identifier, $includeTemporary = false, $includeSuggestion = false)
222
    {
223
        $query = $this->createQuery();
224
225
        if (is_numeric($identifier)) {
226
            $constraints = [
227
                $query->equals('uid', $identifier)
228
            ];
229
        } else {
230
            $constraints = [
231
                $query->equals('object_identifier', $identifier)
232
            ];
233
        }
234
235
        $constraints[] = $query->equals('temporary', $includeTemporary);
236
        $constraints[] = $query->equals('suggestion', $includeSuggestion);
237
238
        $query->matching($query->logicalAnd($constraints));
239
240
        return $query->execute()->getFirst();
241
    }
242
243
244
    /**
245
     * @param object $modifiedObject The modified object
246
     * @throws \Exception
247
     */
248
    public function update($modifiedObject)
249
    {
250
        /** @var Document $document */
251
        $document = $modifiedObject;
252
253
        if (trim($document->getObjectIdentifier()) && !$document->isTemporary() && !$document->isSuggestion()) {
254
            $query = $this->createQuery();
255
            $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...
256
            $constraints[] = $query->equals('temporary', false);
257
            $constraints[] = $query->equals('suggestion', false);
258
            $query->matching($query->logicalAnd($constraints));
259
260
            /** @var Document $workingCopy */
261
            foreach ($query->execute() as $workingCopy) {
262
                if ($workingCopy->getUid() !== $document->getUid()) {
263
                    throw new \Exception(
264
                        "Working copy for " . $document->getObjectIdentifier() . " already exists."
265
                    );
266
                }
267
            }
268
        }
269
270
        parent::update($document);
271
    }
272
273
274
275
    public function updateCreator()
276
    {
277
278
        $query = $this->createQuery();
279
        $query->statement( "update tx_dpf_domain_model_document set creator = owner");
280
        $query->execute();
281
282
    }
283
    
284
    /**
285
     * Finds all records with embargo date
286
     * @return mixed
287
     */
288
    public function crossClientEmbargoFindAll() {
289
        /** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
290
        $querySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
291
        $querySettings->setRespectStoragePage(false);
292
        $this->setDefaultQuerySettings($querySettings);
293
        $query = $this->createQuery();
294
        $query->matching(
295
            $query->logicalAnd(
296
                array(
297
                    $query->logicalNot(
298
                        $query->equals('embargo_date', 0)
299
                    )
300
                )
301
            )
302
        );
303
        return $query->execute();
304
    }
305
}
306