MongoDBPaginatorAdapter::count()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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): array
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(): int
57
    {
58
        // Avoid using EagerCursor::count as this stores a collection without limits to memory
59
        if ($this->cursor->getBaseCursor() instanceof EagerCursor) {
0 ignored issues
show
Bug introduced by
The class Doctrine\MongoDB\EagerCursor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
            return $this->cursor->getBaseCursor()->getCursor()->count();
61
        }
62
63
        return $this->cursor->count();
64
    }
65
}
66