Passed
Pull Request — master (#166)
by
unknown
21:05
created

BookmarkRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 74
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A removeBookmark() 0 25 3
A findBookmark() 0 12 1
A addBookmark() 0 14 2
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 mixed $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
            $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...
58
                $query->equals('document_identifier', $document->getObjectIdentifier()),
59
                $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

59
            /** @scrutinizer ignore-call */ 
60
            $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...
60
            );
61
        } else {
62
            $constraintsAnd[] = $query->equals('document_identifier', $document);
63
        }
64
65
        $constraintsAnd[] = $query->equals('fe_user_uid', $feUserUid);
66
67
        $query->matching($query->logicalAnd($constraintsAnd));
68
69
        $queryResult = $query->execute();
70
71
        /** @var Bookmark @$bookmark */
72
        foreach ($queryResult as $bookmark) {
73
            $this->remove($bookmark);
74
        }
75
76
        return $queryResult->count() > 0;
77
    }
78
79
    /**
80
     * @param int $feUserUid
81
     * @param Document $document
82
     * @return bool
83
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
84
     */
85
    public function addBookmark($feUserUid, Document $document)
86
    {
87
        $identifier = $document->getDocumentIdentifier();
88
89
        $bookmark = $this->findBookmark($feUserUid, $identifier);
90
        if (!$bookmark) {
0 ignored issues
show
introduced by
$bookmark is of type object, thus it always evaluated to true.
Loading history...
91
            $bookmark = new Bookmark();
92
            $bookmark->setDocumentIdentifier($identifier);
93
            $bookmark->setFeUserUid($feUserUid);
94
            $this->add($bookmark);
95
            return true;
96
        }
97
98
        return false;
99
    }
100
101
}
102