Completed
Push — master ( 6eb684...31c9fa )
by Julián
01:56
created

MongoDBPaginatorAdapter::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
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\MongoDB\EagerCursor;
17
use Doctrine\ODM\MongoDB\Cursor;
18
use Zend\Paginator\Adapter\AdapterInterface;
19
20
/**
21
 * MongoDB paginator adapter.
22
 */
23
class MongoDBPaginatorAdapter implements AdapterInterface
24
{
25
    /**
26
     * @var Cursor
27
     */
28
    protected $cursor;
29
30
    /**
31
     * Adapter constructor.
32
     *
33
     * @param Cursor $cursor
34
     */
35
    public function __construct(Cursor $cursor)
36
    {
37
        $this->cursor = $cursor;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getItems($offset, $itemCountPerPage)
44
    {
45
        $cursor = clone $this->cursor;
46
        $cursor->recreate();
47
        $cursor->skip($offset);
48
        $cursor->limit($itemCountPerPage);
49
50
        return $cursor->toArray(false);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function count()
57
    {
58
        // Avoid using EagerCursor::count as this stores a collection without limits to memory
59
        if ($this->cursor->getBaseCursor() instanceof EagerCursor) {
60
            return $this->cursor->getBaseCursor()->getCursor()->count();
61
        }
62
63
        return $this->cursor->count();
64
    }
65
}
66