Completed
Pull Request — master (#6)
by Antonio Oertel
03:40
created

MongoDbDriver::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Respect\Structural\Driver\MongoDb;
4
5
use MongoDB\BSON\ObjectID;
6
use MongoDB\Client as MongoDBClient;
7
use Respect\Data\Collections\Collection;
8
9
class MongoDbDriver extends AbstractDriver
10
{
11
    /**
12
     * @var MongoDBClient
13
     */
14
    private $connection;
15
16
    /**
17
     * Driver constructor.
18
     *
19
     * @param MongoDBClient $connection
20
     * @param string        $database
21
     */
22 5
    public function __construct(MongoDBClient $connection, $database)
23
    {
24 5
        $this->connection = $connection;
25 5
        $this->database = $connection->selectDatabase($database);
0 ignored issues
show
Bug introduced by
The property database does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26 5
    }
27
28
    /**
29
     * @return \MongoDB\Database
30
     */
31
    public function getDatabase()
32
    {
33
        return $this->database;
34
    }
35
36
    /**
37
     * @param int|string $id
38
     *
39
     * @return ObjectID
40
     */
41 2
    public function createObjectId($id)
42
    {
43 2
        return new ObjectID($id);
44
    }
45
46
    /**
47
     * @return MongoDBClient
48
     */
49
    public function getConnection()
50
    {
51
        return $this->connection;
52
    }
53
54
    /**
55
     * @param \Iterator $cursor
56
     *
57
     * @return array
58
     */
59
    public function fetch(\Iterator $cursor)
60
    {
61
        $data = null;
62
        if ($cursor->valid()) {
63
            $data = $cursor->current();
64
            $cursor->next();
65
        }
66
67
        return $data;
68
    }
69
70
    /**
71
     * @param array $collection
72
     * @param array $query
73
     *
74
     * @return \Iterator
75
     */
76
    public function find($collection, array $query = [])
77
    {
78
        $cursor = $this->getDatabase()->selectCollection($collection)->find($query);
0 ignored issues
show
Documentation introduced by
$collection is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
        $iterator = new \IteratorIterator($cursor);
80
        $iterator->rewind();
81
82
        return $iterator;
83
    }
84
85
    /**
86
     * @param Collection $collection
87
     * @param $document
88
     *
89
     * @return void
90
     */
91
    public function insert($collection, $document)
92
    {
93
        $result = $this->getDatabase()->selectCollection($collection)->insertOne($document);
0 ignored issues
show
Documentation introduced by
$collection is of type object<Respect\Data\Collections\Collection>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
        $document->_id = $result->getInsertedId();
95
    }
96
97
    public function update($collection, $criteria, $document)
98
    {
99
        $this->getDatabase()->selectCollection($collection)->updateOne($criteria, ['$set' => $document]);
100
    }
101
102
    /**
103
     * @param string $collection
104
     * @param array  $criteria
105
     */
106
    public function remove($collection, $criteria)
107
    {
108
        $this->getDatabase()->selectCollection($collection)->deleteOne($criteria);
109
    }
110
}
111