Completed
Push — master ( d949a7...15ba37 )
by Andreas
15s queued 12s
created

CursorIterator::key()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace Alcaeus\MongoDbAdapter;
4
5
use IteratorIterator;
6
use MongoDB\BSON\ObjectID;
7
use Traversable;
8
9
/**
10
 * @internal
11
 */
12
final class CursorIterator extends IteratorIterator
13
{
14
    /** @var bool */
15
    private $useIdAsKey;
16
17 78
    public function __construct(Traversable $iterator, $useIdAsKey = false)
18
    {
19 78
        parent::__construct($iterator);
20
21 78
        $this->useIdAsKey = $useIdAsKey;
22 78
    }
23
24 78
    public function key()
25
    {
26 78
        if (!$this->useIdAsKey) {
27 37
            return parent::key();
28
        }
29
30 41
        $current = $this->current();
31
32 41
        if (!isset($current->_id) || (is_object($current->_id) && !$current->_id instanceof ObjectID)) {
33 40
            return parent::key();
34
        }
35
36 20
        return (string) $current->_id;
37
    }
38
}
39