Total Complexity | 43 |
Total Lines | 257 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MongoDB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MongoDB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MongoDB |
||
12 | { |
||
13 | use MongoConnection; |
||
14 | |||
15 | protected $db; |
||
16 | protected $collection; |
||
17 | protected $databaseName; |
||
18 | protected $collectionName; |
||
19 | |||
20 | public function command(array $command) |
||
21 | { |
||
22 | return static::getMongoClient()->{$this->admin}->command($command); |
||
|
|||
23 | } |
||
24 | |||
25 | public function renameCollection($fromNs, $toNs) |
||
26 | { |
||
27 | return $this->command(array('renameCollection' => $fromNs, 'to' => $toNs)); |
||
28 | } |
||
29 | |||
30 | public function renameFields($collectionName, $fields) |
||
31 | { |
||
32 | $rename = []; |
||
33 | foreach ($fields as $oldName => $newName) { |
||
34 | $rename[$oldName] = $newName; |
||
35 | } |
||
36 | $update = array( |
||
37 | '$rename' => $rename, |
||
38 | ); |
||
39 | |||
40 | return $this->selectCollection($collectionName)->updateMany(array(), $update, array('upsert' => true)); |
||
41 | } |
||
42 | |||
43 | public function getDB() |
||
44 | { |
||
45 | return DB::connection()->getMongoDB(); |
||
46 | } |
||
47 | |||
48 | public function getNamespace($databaseName, $collectionName) |
||
49 | { |
||
50 | return $databaseName . '.' . $collectionName; |
||
51 | } |
||
52 | |||
53 | public function getCollections() |
||
54 | { |
||
55 | return $this->getDB()->listCollections(); |
||
56 | } |
||
57 | |||
58 | public function getCollectionNames() |
||
59 | { |
||
60 | $collections = $this->getCollections(); |
||
61 | $collectionNames = []; |
||
62 | foreach ($collections as $key => $collection) { |
||
63 | $collectionNames[] = $collection->getName(); |
||
64 | } |
||
65 | |||
66 | return $collectionNames; |
||
67 | } |
||
68 | |||
69 | public function hasCollection($collectionName) |
||
70 | { |
||
71 | return (in_array($collectionName, $this->getCollectionNames())) ? true : false; |
||
72 | } |
||
73 | |||
74 | public function createCollection($collectionName) |
||
75 | { |
||
76 | return $this->getDB()->createCollection($collectionName); |
||
77 | } |
||
78 | |||
79 | public function getCollection($collectionName) |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | public function updateCollection($collection) |
||
112 | |||
113 | } |
||
114 | |||
115 | public function setFields($collectionName, $fields) |
||
116 | { |
||
117 | $collection = $this->selectCollection($collectionName); |
||
118 | /* |
||
119 | * Rename Columns |
||
120 | */ |
||
121 | $renames = []; |
||
122 | foreach ($fields as $field) { |
||
123 | if ($field->oldName != "") { |
||
124 | if ($field->oldName != $field->name) { |
||
125 | $renames[$field->oldName] = $field->name; |
||
126 | } |
||
127 | |||
128 | } |
||
129 | } |
||
130 | $update = []; |
||
131 | if (count($renames) > 0) { |
||
132 | $update['$rename'] = $renames; |
||
133 | $collection->updateMany(array(), $update, array('upsert' => true)); |
||
134 | foreach ($renames as $oldName => $newName) { |
||
135 | $collection_field = CollectionField::where('old_name', $oldName)->first(); |
||
136 | $collection_field->old_name = $newName; |
||
137 | $collection_field->update(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | $newFields = $this->getColumnsName($collectionName); |
||
142 | $columns = $this->getCollectionColumns($collectionName); |
||
143 | |||
144 | /* |
||
145 | * Add Columns |
||
146 | */ |
||
147 | |||
148 | if ($collection->count() > 0) { |
||
149 | $sets = []; |
||
150 | foreach ($newFields as $newField) { |
||
151 | $cursor = $collection->find(); |
||
152 | $iterator = iterator_to_array($cursor); |
||
153 | |||
154 | foreach ($iterator as $document) { |
||
155 | $columnNames = []; |
||
156 | $id = ""; |
||
157 | foreach ($document as $columnName => $columnValue) { |
||
158 | if (is_object($columnValue)) { |
||
159 | foreach ($columnValue as $key => $value) { |
||
160 | if ($columnName == '_id') { |
||
161 | $id = $value; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | $columnNames[] = $columnName; |
||
166 | } |
||
167 | |||
168 | if ($id != "" && !in_array($newField, $columnNames)) { |
||
169 | $update['$set'] = array($newField => ""); |
||
170 | $collection->updateOne( |
||
171 | array("_id" => new \MongoDB\BSON\ObjectID($id)), |
||
172 | $update, |
||
173 | array('upsert' => true) |
||
174 | ); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | } |
||
180 | |||
181 | /* |
||
182 | * Remove Columns |
||
183 | */ |
||
184 | |||
185 | $update = []; |
||
186 | |||
187 | $unsets = []; |
||
188 | foreach ($columns as $column) { |
||
189 | if (!in_array($column, $newFields)) { |
||
190 | $unsets[$column] = ""; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if (count($unsets) > 0) { |
||
195 | $update['$unset'] = $unsets; |
||
196 | $collection->updateMany(array(), $update, array('upsert' => true)); |
||
197 | } |
||
198 | |||
199 | return true; |
||
200 | } |
||
201 | |||
202 | public function dropCollection($collectionName) |
||
203 | { |
||
204 | $this->getDB()->dropCollection($collectionName); |
||
205 | } |
||
206 | |||
207 | public function selectCollection($collectionName) |
||
210 | } |
||
211 | |||
212 | public function getCollectionColumns($collectionName) |
||
213 | { |
||
214 | $cursor = $this->selectCollection($collectionName)->find(); |
||
215 | $iterator = iterator_to_array($cursor); |
||
216 | $columnNames = []; |
||
217 | |||
218 | foreach ($iterator as $document) { |
||
219 | foreach ($document as $columnName => $columnValue) { |
||
220 | $columnNames[] = $columnName; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | return array_values(array_unique($columnNames)); |
||
225 | } |
||
226 | |||
227 | public function getColumns($collectionName) |
||
263 | } |
||
264 | |||
265 | public function getColumnsName($collectionName) |
||
268 | } |
||
269 | |||
270 | } |
||
271 |