Completed
Pull Request — master (#63)
by Andreas
21:41
created

MongoCommandCursor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 93
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromDocument() 0 4 1
A __construct() 0 6 1
B ensureCursor() 0 15 5
A getCursorInfo() 0 12 1
A getIterationInfo() 0 15 2
A __sleep() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
use Alcaeus\MongoDbAdapter\AbstractCursor;
17
use Alcaeus\MongoDbAdapter\TypeConverter;
18
19
class MongoCommandCursor extends AbstractCursor implements MongoCursorInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    private $command;
25
26
    /**
27
     * MongoCommandCursor constructor.
28
     * @param MongoClient $connection
29
     * @param string $ns
30
     * @param array $command
31
     */
32
    public function __construct(MongoClient $connection, $ns, array $command = [])
33
    {
34
        parent::__construct($connection, $ns);
35
36
        $this->command = $command;
37
    }
38
39
    /**
40
     * @param MongoClient $connection
41
     * @param string $hash
42
     * @param array $document
43
     * @return MongoCommandCursor
44
     */
45
    public static function createFromDocument(MongoClient $connection, $hash, array $document)
0 ignored issues
show
Unused Code introduced by
The parameter $connection 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...
Unused Code introduced by
The parameter $hash 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...
Unused Code introduced by
The parameter $document 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...
46
    {
47
        throw new \Exception('Not implemented');
48
    }
49
50
    /**
51
     * @return \MongoDB\Driver\Cursor
52
     */
53
    protected function ensureCursor()
54
    {
55
        if ($this->cursor === null) {
56
            $convertedCommand = TypeConverter::fromLegacy($this->command);
57
            if (isset($convertedCommand->cursor)) {
58
                if ($convertedCommand->cursor === true || $convertedCommand->cursor === []) {
59
                    $convertedCommand->cursor = new \stdClass();
60
                }
61
            }
62
63
            $this->cursor = $this->db->command($convertedCommand, $this->getOptions());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->command($conv...d, $this->getOptions()) of type integer is incompatible with the declared type object<MongoDB\Driver\Cursor> of property $cursor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        }
65
66
        return $this->cursor;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->cursor; of type integer|MongoDB\Driver\Cursor adds the type integer to the return on line 66 which is incompatible with the return type declared by the abstract method Alcaeus\MongoDbAdapter\A...actCursor::ensureCursor of type MongoDB\Driver\Cursor.
Loading history...
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    protected function getCursorInfo()
73
    {
74
        return [
75
            'ns' => $this->ns,
76
            'limit' => 0,
77
            'batchSize' => $this->batchSize,
78
            'skip' => 0,
79
            'flags' => 0,
80
            'query' => $this->command,
81
            'fields' => null,
82
        ];
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function getIterationInfo()
89
    {
90
        $iterationInfo = parent::getIterationInfo();
91
92
        if ($iterationInfo['started_iterating']) {
93
            $iterationInfo += [
94
                'firstBatchAt' => $iterationInfo['at'],
95
                'firstBatchNumReturned' => $iterationInfo['numReturned'],
96
            ];
97
            $iterationInfo['at'] = 0;
98
            $iterationInfo['numReturned'] = 0;
99
        }
100
101
        return $iterationInfo;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function __sleep()
108
    {
109
        return ['command'] + parent::__sleep();
110
    }
111
}
112