Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
41 / 41 |
MongoConnector | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
16 | |
100.00% |
41 / 41 |
connection | |
100.00% |
1 / 1 |
2 | |
100.00% |
11 / 11 |
|||
collection | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
all | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
findOne | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
dropDatabase | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
__call | |
100.00% |
1 / 1 |
6 | |
100.00% |
14 / 14 |
|||
getDatabase | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
onDatabase | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setDatabase | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\DBConnector\Connectors; |
4 | |
5 | use Exception; |
6 | use Illuminate\Support\Collection; |
7 | use Illuminate\Support\Facades\Log; |
8 | use Qmp\Laravel\DBConnector\Config; |
9 | use MongoDB\Client; |
10 | |
11 | |
12 | class MongoConnector implements InterfaceConnector |
13 | { |
14 | /** |
15 | * The mongo client |
16 | * |
17 | * @var MongoDB\Client |
18 | */ |
19 | protected $mongo; |
20 | |
21 | /** |
22 | * The databse name |
23 | * |
24 | * @var string |
25 | */ |
26 | protected $database; |
27 | |
28 | /** |
29 | * The collection name |
30 | * |
31 | * @var string |
32 | */ |
33 | protected $collection; |
34 | |
35 | /** |
36 | * Undocumented variable |
37 | * |
38 | * @var boolean |
39 | */ |
40 | protected $onDatabase = false; |
41 | |
42 | /** |
43 | * Initiate the mongo connection |
44 | * |
45 | * @param Config $config |
46 | * @param array $options |
47 | * @return void |
48 | * @throws Exception |
49 | */ |
50 | public function connection(Config $config, array $options = []): void |
51 | { |
52 | $mongoConfig = $config->toArray(['user', 'password', 'host', 'port', 'database']); |
53 | $this->database = $mongoConfig['database'] ?? 'test'; |
54 | |
55 | try { |
56 | $format = 'mongodb://%s:%s@%s:%s'; |
57 | $uri = sprintf($format, $mongoConfig['user'], $mongoConfig['password'], $mongoConfig['host'], $mongoConfig['port']); |
58 | |
59 | $uriOptions = $options['uriOptions'] ?? []; |
60 | $driverOptions = $options['driverOptions'] ?? []; |
61 | |
62 | $this->mongo = new Client($uri, $uriOptions, $driverOptions); |
63 | } catch (Exception $e) { |
64 | Log::debug('Unable to connect mongo: ' . $e->getMessage()); |
65 | throw new Exception($e->getMessage()); |
66 | } |
67 | } |
68 | |
69 | /** |
70 | * Set the current collection |
71 | * |
72 | * @param string $collection |
73 | * @return self |
74 | */ |
75 | public function collection(string $collection): self |
76 | { |
77 | $this->collection = $collection; |
78 | return $this; |
79 | } |
80 | |
81 | /** |
82 | * Return all the documents |
83 | * |
84 | * @return Collection |
85 | */ |
86 | public function all($query = [], $options = []): Collection |
87 | { |
88 | $collection = collect(); |
89 | |
90 | foreach ($this->find($query, $options) as $entry) { |
91 | $collection->push(collect(iterator_to_array($entry))); |
92 | } |
93 | |
94 | return $collection; |
95 | } |
96 | |
97 | |
98 | /** |
99 | * Find one document |
100 | * |
101 | * @param array $query |
102 | * @return Collection |
103 | */ |
104 | public function findOne(array $query): Collection |
105 | { |
106 | $database = $this->database; |
107 | $collection = $this->collection; |
108 | $collection = collect($this->mongo->$database->$collection->findOne($query)); |
109 | |
110 | return $collection->fromMongo(); |
111 | } |
112 | |
113 | /** |
114 | * Drop a database on the server |
115 | * |
116 | * @param string $database |
117 | * @param array $options |
118 | */ |
119 | public function dropDatabase(string $database, array $options = []) |
120 | { |
121 | return $this->mongo->dropDatabase($database, $options); |
122 | } |
123 | |
124 | /** |
125 | * Call a method on the mongo client |
126 | * |
127 | * @param string $method |
128 | * @param iterable $arguments |
129 | * @return mixed |
130 | * @throws Exception |
131 | */ |
132 | public function __call(string $method, iterable $arguments) |
133 | { |
134 | try { |
135 | $database = $this->database; |
136 | $collection = $this->collection; |
137 | |
138 | |
139 | if ($this->onDatabase) { |
140 | if (!method_exists($this->mongo->$database, $method)) { |
141 | throw new Exception("method '$method' does not exist"); |
142 | } |
143 | |
144 | $this->onDatabase = false; |
145 | |
146 | return $this->mongo->$database->$method(...$arguments); |
147 | } |
148 | |
149 | if (!$this->collection) { |
150 | throw new Exception('No collection specified!'); |
151 | } |
152 | |
153 | if (!method_exists($this->mongo->$database->$collection, $method)) { |
154 | throw new Exception("method '$method' does not exist"); |
155 | } |
156 | |
157 | return $this->mongo->$database->$collection->$method(...$arguments); |
158 | } catch (Exception $e) { |
159 | throw new Exception($e->getMessage()); |
160 | } |
161 | } |
162 | |
163 | /** |
164 | * @return string |
165 | */ |
166 | public function getDatabase(): string |
167 | { |
168 | return $this->database; |
169 | } |
170 | |
171 | /** |
172 | * Undocumented function |
173 | * |
174 | * @return self |
175 | */ |
176 | public function onDatabase(): self |
177 | { |
178 | $this->onDatabase = true; |
179 | return $this; |
180 | } |
181 | |
182 | /** |
183 | * @param string $database |
184 | * @return self |
185 | */ |
186 | public function setDatabase(string $database): self |
187 | { |
188 | $this->database = $database; |
189 | return $this; |
190 | } |
191 | } |