Completed
Pull Request — master (#5)
by Antonio Oertel
05:30
created

Driver::getDatabase()   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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Respect\Structural\Driver\Mongo;
4
5
use Respect\Data\CollectionIterator;
6
use Respect\Data\Collections\Collection;
7
use Respect\Structural\Driver as BaseDriver;
8
9
class Driver implements BaseDriver
10
{
11
    /**
12
     * @var \MongoClient
13
     */
14
    private $connection;
15
16
    /**
17
     * @var \MongoDB
18
     */
19
    private $database;
20
21
    /**
22
     * Driver constructor.
23
     *
24
     * @param \MongoClient $connection
25
     * @param string       $database
26
     */
27 4
    public function __construct(\MongoClient $connection, $database)
28
    {
29 4
        $this->connection = $connection;
30 4
        $this->database = $connection->{$database};
31 4
    }
32
33
    /**
34
     * @return \MongoDB
35
     */
36
    public function getDatabase()
37
    {
38
        return $this->database;
39
    }
40
41
    public function getConnection()
42
    {
43
        return $this->connection;
44
    }
45
46
    /**
47
     * @param \Iterator $cursor
48
     *
49
     * @return array
50
     */
51
    public function fetch(\Iterator $cursor)
52
    {
53
        $data = $cursor->current();
54
        $cursor->next();
55
56
        return $data;
57
    }
58
59
    /**
60
     * @param array $collection
61
     * @param array $query
62
     *
63
     * @return \Iterator
64
     */
65
    public function find($collection, array $query = [])
66
    {
67
        $cursor = $this->getDatabase()->{$collection}->find($query);
68
        $cursor->rewind();
69
70
        return $cursor;
71
    }
72
73
    /**
74
     * @param Collection $collection
75
     *
76
     * @return array
77
     */
78 3
    public function generateQuery(Collection $collection)
79
    {
80 3
        return $this->parseConditions($collection);
81
    }
82
83
    /**
84
     * @param Collection $collection
85
     *
86
     * @return array
87
     */
88 3
    protected function parseConditions(Collection $collection)
89
    {
90 3
        $allCollections = CollectionIterator::recursive($collection);
91 3
        $allCollections = iterator_to_array($allCollections);
92 3
        $allCollections = array_slice($allCollections, 1);
93
94 3
        $condition = $this->getConditionArray($collection);
95
96 3
        foreach ($allCollections as $coll) {
97 1
            $condition += $this->getConditionArray($coll, true);
98 3
        }
99
100 3
        return $condition;
101
    }
102
103
    /**
104
     * @param Collection $collection
105
     * @param bool|false $prefix
106
     *
107
     * @return array
108
     */
109 3
    protected function getConditionArray(Collection $collection, $prefix = false)
110
    {
111 3
        $condition = $collection->getCondition();
112
113 3
        if (!is_array($condition)) {
114 2
            $condition = ['_id' => $this->createMongoId($condition)];
115 2
        }
116
117 3
        if ($prefix) {
118 1
            $condition = static::prefixArrayKeys($condition, $collection->getName() . '.');
119 1
        }
120
121 3
        return $condition;
122
    }
123
124
    /**
125
     * @param array  $array
126
     * @param string $prefix
127
     *
128
     * @return array
129
     */
130 1
    protected static function prefixArrayKeys(array $array, $prefix)
131
    {
132 1
        $new = [];
133
134 1
        foreach ($array as $key => $value) {
135 1
            $new["{$prefix}{$key}"] = $value;
136 1
        }
137
138 1
        return $new;
139
    }
140
141
    /**
142
     * @param int|string $id
143
     *
144
     * @return \MongoId|\MongoInt32
145
     */
146 2
    protected function createMongoId($id)
147
    {
148 2
        if (is_int($id)) {
149 1
            return new \MongoInt32($id);
150
        }
151
152 1
        return new \MongoId($id);
153
    }
154
155
    /**
156
     * @param Collection $collection
157
     * @param $document
158
     *
159
     * @return void
160
     */
161
    public function insert($collection, $document)
162
    {
163
        $this->getDatabase()->{$collection}->insert($document);
164
    }
165
166
    public function update($collection, $criteria, $document)
167
    {
168
        $this->getDatabase()->{$collection}->update($criteria, $document);
169
    }
170
171
    public function remove($collection, $criteria)
172
    {
173
        $this->getDatabase()->{$collection}->remove($criteria);
174
    }
175
}
176