Completed
Pull Request — master (#3)
by Antonio Oertel
03:05
created

Driver::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
     * @param \MongoClient $connection
24
     * @param string $database
25
     */
26 4
    public function __construct(\MongoClient $connection, $database)
27
    {
28 4
        $this->connection = $connection;
29 4
        $this->database = $connection->{$database};
30 4
    }
31
32
    /**
33
     * @return \MongoDB
34
     */
35
    public function getDatabase()
36
    {
37
        return $this->database;
38
    }
39
40
    public function getConnection()
41
    {
42
        return $this->connection;
43
    }
44
45
    /**
46
     * @param \Iterator $cursor
47
     * @return array
48
     */
49
    public function fetch(\Iterator $cursor)
50
    {
51
        $cursor->next();
52
        return $cursor->current();
53
    }
54
55
    /**
56
     * @param array $collection
57
     * @param array $query
58
     * @return \Iterator
59
     */
60
    public function find($collection, array $query = array())
61
    {
62
        return $this->getDatabase()->{$collection}->find($query);
63
    }
64
65
    /**
66
     * @param Collection $collection
67
     * @return array
68
     */
69 3
    public function generateQuery(Collection $collection)
70
    {
71 3
        return $this->parseConditions($collection);
72
    }
73
74
    /**
75
     * @param Collection $collection
76
     * @return array
77
     */
78 3
    protected function parseConditions(Collection $collection)
79
    {
80 3
        $allCollections = CollectionIterator::recursive($collection);
81 3
        $allCollections = iterator_to_array($allCollections);
82 3
        $allCollections = array_slice($allCollections, 1);
83
84 3
        $condition = $this->getConditionArray($collection);
85
86 3
        foreach ($allCollections as $name => $coll)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
87 3
            $condition += $this->getConditionArray($coll, true);
88
89 3
        return $condition;
90
    }
91
92
    /**
93
     * @param Collection $collection
94
     * @param bool|false $prefix
95
     * @return array
96
     */
97 3
    protected function getConditionArray(Collection $collection, $prefix = false)
98
    {
99 3
        $condition = $collection->getCondition();
100
101 3
        if (!is_array($condition)) {
102 2
            $condition = array('_id' => $this->createMongoId($condition));
103 2
        }
104
105
        if ($prefix)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
106 3
            $condition = static::prefixArrayKeys($condition, $collection->getName() . ".");
107
108 3
        return $condition;
109
    }
110
111
    /**
112
     * @param array $array
113
     * @param string $prefix
114
     * @return array
115
     */
116 1
    protected static function prefixArrayKeys(array $array, $prefix)
117
    {
118 1
        $new = array();
119
120 1
        foreach ($array as $key => $value)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
121 1
            $new["{$prefix}{$key}"] = $value;
122
123 1
        return $new;
124
    }
125
126
    /**
127
     * @param int|string $id
128
     * @return \MongoId|\MongoInt32
129
     */
130 2
    protected function createMongoId($id)
131
    {
132 2
        if (is_int($id)) {
133 1
            return new \MongoInt32($id);
134
        }
135 1
        return new \MongoId($id);
136
    }
137
138
    /**
139
     * @param Collection $collection
140
     * @param $document
141
     * @return void
142
     */
143
    public function insert($collection, $document)
144
    {
145
        $this->getDatabase()->{$collection}->insert($document);
146
    }
147
148
    public function update($collection, $criteria, $document)
149
    {
150
        $this->getDatabase()->{$collection}->update($criteria, $document);
151
    }
152
153
    public function remove($collection, $criteria)
154
    {
155
        $this->getDatabase()->{$collection}->remove($criteria);
156
    }
157
}
158