Completed
Push — master ( 29461d...1ce752 )
by Julián
06:28
created

CouchDBRepository::flushObjects()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 2
1
<?php
2
3
/*
4
 * doctrine-couchdb-odm-repositories (https://github.com/juliangut/doctrine-couchdb-odm-repositories).
5
 * Doctrine2 CouchDB ODM utility entity repositories.
6
 *
7
 * @license MIT
8
 * @link https://github.com/juliangut/doctrine-couchdb-odm-repositories
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Jgut\Doctrine\Repository\CouchDB\ODM;
15
16
use Doctrine\Common\Util\ClassUtils;
17
use Doctrine\ODM\CouchDB\DocumentManager;
18
use Doctrine\ODM\CouchDB\DocumentRepository;
19
use Jgut\Doctrine\Repository\EventsTrait;
20
use Jgut\Doctrine\Repository\FiltersTrait;
21
use Jgut\Doctrine\Repository\PaginatorTrait;
22
use Jgut\Doctrine\Repository\Repository;
23
use Jgut\Doctrine\Repository\RepositoryTrait;
24
use Zend\Paginator\Paginator;
25
26
/**
27
 * CouchDB document repository.
28
 */
29
class CouchDBRepository extends DocumentRepository implements Repository
30
{
31
    use RepositoryTrait {
32
        refresh as baseRefresh;
33
    }
34
    use EventsTrait;
35
    use FiltersTrait;
36
    use PaginatorTrait;
37
38
    /**
39
     * Class name.
40
     *
41
     * @var string
42
     */
43
    protected $className;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getClassName(): string
49
    {
50
        if ($this->className === null) {
51
            $this->className = ClassUtils::getRealClass($this->getDocumentName());
52
        }
53
54
        return $this->className;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @throws \LogicException
61
     */
62
    protected function getFilterCollection()
63
    {
64
        throw new \LogicException('Doctrine\'s CouchDB manager does not implement filters');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function getManager(): DocumentManager
71
    {
72
        return $this->getDocumentManager();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @param array $criteria
79
     * @param array $orderBy
80
     * @param int   $itemsPerPage
81
     *
82
     * @return \Zend\Paginator\Paginator
83
     */
84
    public function findPaginatedBy($criteria, array $orderBy = [], int $itemsPerPage = 10): Paginator
85
    {
86
        if (!is_array($criteria)) {
87
            $criteria = [$criteria];
88
        }
89
90
        return $this->paginate($this->findBy($criteria, $orderBy));
0 ignored issues
show
Unused Code introduced by
The call to CouchDBRepository::findBy() has too many arguments starting with $criteria.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
    }
92
93
    /**
94
     * Paginate CouchDB results.
95
     *
96
     * @param array $results
97
     * @param int   $itemsPerPage
98
     *
99
     * @return Paginator
100
     */
101
    protected function paginate(array $results, int $itemsPerPage = 10): Paginator
102
    {
103
        return $this->getPaginator(new CouchDBPaginatorAdapter($results), $itemsPerPage);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @param array $criteria
110
     *
111
     * @return int
112
     */
113
    public function countBy($criteria): int
114
    {
115
        return count($this->findBy($criteria));
0 ignored issues
show
Unused Code introduced by
The call to CouchDBRepository::findBy() has too many arguments starting with $criteria.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
    }
117
118
    /**
119
     * Flush managed object.
120
     *
121
     * @param object|object[]|\Traversable $objects
122
     * @param bool                         $flush
123
     *
124
     * @SuppressWarnings(PMD.UnusedFormalParameter)
125
     */
126
    protected function flushObjects($objects, bool $flush)
0 ignored issues
show
Unused Code introduced by
The parameter $objects is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        if ($flush || $this->autoFlush) {
129
            $this->getManager()->flush();
130
        }
131
    }
132
}
133