Completed
Push — master ( c9e6e4...86d198 )
by Julián
06:43
created

MongoDBRepository::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * doctrine-mongodb-odm-repositories (https://github.com/juliangut/doctrine-mongodb-odm-repositories).
5
 * Doctrine2 MongoDB ODM utility entity repositories.
6
 *
7
 * @license MIT
8
 * @link https://github.com/juliangut/doctrine-mongodb-odm-repositories
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Jgut\Doctrine\Repository\MongoDB\ODM;
15
16
use Doctrine\Common\Util\ClassUtils;
17
use Doctrine\ODM\MongoDB\Cursor;
18
use Doctrine\ODM\MongoDB\DocumentManager;
19
use Doctrine\ODM\MongoDB\DocumentRepository;
20
use Jgut\Doctrine\Repository\EventsTrait;
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
 * MongoDB document repository.
28
 */
29
class MongoDBRepository extends DocumentRepository implements Repository
30
{
31
    use RepositoryTrait;
32
    use EventsTrait;
33
    use PaginatorTrait;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getClassName(): string
39
    {
40
        return ClassUtils::getRealClass(parent::getClassName());
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function getManager(): DocumentManager
47
    {
48
        return $this->getDocumentManager();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @param array $criteria
55
     * @param array $orderBy
56
     * @param int   $itemsPerPage
57
     *
58
     * @return \Zend\Paginator\Paginator
59
     */
60
    public function findPaginatedBy($criteria, array $orderBy = [], int $itemsPerPage = 10): Paginator
61
    {
62
        return $this->paginate($this->getDocumentPersister()->loadAll($criteria, $orderBy), $itemsPerPage);
63
    }
64
65
    /**
66
     * Paginate MongoDB cursor.
67
     *
68
     * @param Cursor $cursor
69
     * @param int    $itemsPerPage
70
     *
71
     * @return Paginator
72
     */
73
    protected function paginate(Cursor $cursor, int $itemsPerPage = 10): Paginator
74
    {
75
        return $this->getPaginator(new MongoDBPaginatorAdapter($cursor), $itemsPerPage);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @param array $criteria
82
     *
83
     * @return int
84
     */
85
    public function countBy($criteria): int
86
    {
87
        return $this->getDocumentPersister()->loadAll($criteria)->count();
88
    }
89
}
90