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

BookmarkRepository::findDocumentBookmarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 1
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\Bookmark;
18
use EWW\Dpf\Domain\Model\Document;
19
use \EWW\Dpf\Domain\Workflow\DocumentWorkflow;
20
use \EWW\Dpf\Security\Security;
21
22
/**
23
 * The repository for Bookmarks
24
 */
25
class BookmarkRepository extends \EWW\Dpf\Domain\Repository\AbstractRepository
26
{
27
    /**
28
     * @param int $feUserUid
29
     * @param string $identifier
30
     * @return object
31
     */
32
    public function findBookmark($feUserUid, $identifier)
33
    {
34
        $query = $this->createQuery();
35
36
        $query->matching(
37
            $query->logicalAnd(
38
                $query->equals('document_identifier', $identifier),
39
                $query->equals('fe_user_uid', $feUserUid)
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('fe_user_uid', $feUserUid). ( Ignorable by Annotation )

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

39
            $query->/** @scrutinizer ignore-call */ 
40
                    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...
40
            )
41
        );
42
43
        return $query->execute()->getFirst();
44
    }
45
46
    /**
47
     * @param Document|string $document
48
     * @param int|null $feUserUid
49
     * @return bool
50
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
51
     */
52
    public function removeBookmark($document, $feUserUid)
53
    {
54
        $query = $this->createQuery();
55
56
        if ($document instanceof Document) {
57
            // A document can be identified (documentIdentifier) by its Fedora PID or the document UID in case it hasn't been
58
            // published (that means it exits only locally in the TYPO3  db).
59
            // In order to find a bookmark that belongs to a document, it is essential to search for both identifiers.
60
            $constraintsAnd[] = $query->logicalOr(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$constraintsAnd was never initialized. Although not strictly required by PHP, it is generally a good practice to add $constraintsAnd = array(); before regardless.
Loading history...
61
                $query->equals('document_identifier', $document->getObjectIdentifier()),
62
                $query->equals('document_identifier', $document->getUid())
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('document...', $document->getUid()). ( Ignorable by Annotation )

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

62
            /** @scrutinizer ignore-call */ 
63
            $constraintsAnd[] = $query->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...
63
            );
64
        } else {
65
            // In case $document already contains a plain identifier the above distinction is not necessary.
66
            $constraintsAnd[] = $query->equals('document_identifier', $document);
67
        }
68
69
        if ($feUserUid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $feUserUid of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70
            $constraintsAnd[] = $query->equals('fe_user_uid', $feUserUid);
71
        }
72
73
        $query->matching($query->logicalAnd($constraintsAnd));
74
75
        $queryResult = $query->execute();
76
77
        /** @var Bookmark @$bookmark */
78
        foreach ($queryResult as $bookmark) {
79
            $this->remove($bookmark);
80
        }
81
82
        return $queryResult->count() > 0;
83
    }
84
85
    /**
86
     * @param Document|string $document
87
     * @param int $feUserUid
88
     * @return bool
89
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
90
     */
91
    public function addBookmark($document, $feUserUid)
92
    {
93
        if ($document instanceof Document) {
94
            // The returned documentIdentifier is either a PID or the document UID (TYPO3 db), see also
95
            // the above method removeBookmark().
96
            $identifier = $document->getDocumentIdentifier();
97
        } else {
98
            // In case $document already contains a plain identifier we can use it directly.
99
            $identifier = $document;
100
        }
101
102
        $bookmark = $this->findBookmark($feUserUid, $identifier);
103
        if (!$bookmark) {
0 ignored issues
show
introduced by
$bookmark is of type object, thus it always evaluated to true.
Loading history...
104
            $bookmark = new Bookmark();
105
            $bookmark->setDocumentIdentifier($identifier);
106
            $bookmark->setFeUserUid($feUserUid);
107
            $this->add($bookmark);
108
            return true;
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * @param Document $document
116
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
117
     */
118
    public function findDocumentBookmarks(Document $document)
119
    {
120
        $query = $this->createQuery();
121
122
        $constraintsAnd[] = $query->logicalOr(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$constraintsAnd was never initialized. Although not strictly required by PHP, it is generally a good practice to add $constraintsAnd = array(); before regardless.
Loading history...
123
            $query->equals('document_identifier', $document->getObjectIdentifier()),
124
            $query->equals('document_identifier', $document->getUid())
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('document...', $document->getUid()). ( Ignorable by Annotation )

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

124
        /** @scrutinizer ignore-call */ 
125
        $constraintsAnd[] = $query->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...
125
        );
126
127
        $query->matching($query->logicalAnd($constraintsAnd));
128
        return $query->execute();
129
    }
130
131
}
132