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

MongoDriver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 27.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 99
ccs 8
cts 29
cp 0.2759
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDatabase() 0 4 1
A getConnection() 0 4 1
A fetch() 0 7 1
A find() 0 7 1
A createObjectId() 0 8 2
A insert() 0 4 1
A update() 0 4 1
A remove() 0 4 1
1
<?php
2
3
namespace Respect\Structural\Driver\MongoDb;
4
5
use Respect\Data\Collections\Collection;
6
7
class MongoDriver extends AbstractDriver
8
{
9
    /**
10
     * @var \MongoClient
11
     */
12
    private $connection;
13
14
    /**
15
     * @var \MongoDB
16
     */
17
    private $database;
18
19
    /**
20
     * Driver constructor.
21
     *
22
     * @param \MongoClient $connection
23
     * @param string       $database
24
     */
25 5
    public function __construct(\MongoClient $connection, $database)
26
    {
27 5
        $this->connection = $connection;
28 5
        $this->database = $connection->{$database};
29 5
    }
30
31
    /**
32
     * @return \MongoDB
33
     */
34
    public function getDatabase()
35
    {
36
        return $this->database;
37
    }
38
39
    public function getConnection()
40
    {
41
        return $this->connection;
42
    }
43
44
    /**
45
     * @param \Iterator $cursor
46
     *
47
     * @return array
48
     */
49
    public function fetch(\Iterator $cursor)
50
    {
51
        $data = $cursor->current();
52
        $cursor->next();
53
54
        return $data;
55
    }
56
57
    /**
58
     * @param array $collection
59
     * @param array $query
60
     *
61
     * @return \Iterator
62
     */
63
    public function find($collection, array $query = [])
64
    {
65
        $cursor = $this->getDatabase()->{$collection}->find($query);
66
        $cursor->rewind();
67
68
        return $cursor;
69
    }
70
71
    /**
72
     * @param int|string $id
73
     *
74
     * @return \MongoId|\MongoInt32
75
     */
76 2
    public function createObjectId($id)
77
    {
78 2
        if (is_int($id)) {
79 1
            return new \MongoInt32($id);
80
        }
81
82 1
        return new \MongoId($id);
83
    }
84
85
    /**
86
     * @param Collection $collection
87
     * @param $document
88
     *
89
     * @return void
90
     */
91
    public function insert($collection, $document)
92
    {
93
        $this->getDatabase()->{$collection}->insert($document);
94
    }
95
96
    public function update($collection, $criteria, $document)
97
    {
98
        $this->getDatabase()->{$collection}->update($criteria, $document);
99
    }
100
101
    public function remove($collection, $criteria)
102
    {
103
        $this->getDatabase()->{$collection}->remove($criteria);
104
    }
105
}
106