Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LdapConnector 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LdapConnector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class LdapConnector { |
||
26 | |||
27 | public function __construct($xml_config) { |
||
28 | try { |
||
29 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', setting xml config', \OCP\Util::DEBUG); |
||
30 | $this->config_content = new \SimpleXMLElement($xml_config); |
||
31 | } catch (Exception $e) { |
||
32 | \OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', error in setting xml config', \OCP\Util::DEBUG); |
||
33 | } |
||
34 | } |
||
35 | private function convertDate ($ldapDate) { |
||
36 | |||
37 | $tstamp = strtotime($ldapDate); |
||
38 | $theDate = new \DateTime; |
||
39 | $theDate->setTimestamp($tstamp); |
||
40 | |||
41 | return $theDate; |
||
42 | } |
||
43 | /** |
||
44 | * @brief transform a ldap entry into an VCard object |
||
45 | * for each ldap entry which is like "property: value" |
||
46 | * to a VCard entry which is like "PROPERTY[;PARAMETER=param]:value" |
||
47 | * @param array $ldapEntry |
||
48 | * @return OC_VCard |
||
49 | */ |
||
50 | public function ldapToVCard($ldapEntry) { |
||
51 | $vcard = new \OCA\Contacts\VObject\VCard(); |
||
52 | $vcard->REV = $this->convertDate($ldapEntry['modifytimestamp'][0])->format(\DateTime::W3C); |
||
53 | //error_log("modifytimestamp: ".$vcard->REV); |
||
54 | $vcard->{'X-LDAP-DN'} = base64_encode($ldapEntry['dn']); |
||
55 | // OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' vcard is '.$vcard->serialize(), \OCP\Util::DEBUG); |
||
56 | |||
57 | for ($i=0; $i<$ldapEntry["count"]; $i++) { |
||
58 | // ldap property name : $ldap_entry[$i] |
||
59 | $lProperty = $ldapEntry[$i]; |
||
60 | for ($j=0;$j<$ldapEntry[$lProperty]["count"];$j++){ |
||
61 | |||
62 | // What to do : |
||
63 | // convert the ldap property into vcard property, type and position (if needed) |
||
64 | // $v_params format: array('property' => property, 'type' => array(types), 'position' => position) |
||
65 | $v_params = $this->getVCardProperty($lProperty); |
||
66 | |||
67 | foreach ($v_params as $v_param) { |
||
68 | |||
69 | if (isset($v_param['unassigned'])) { |
||
70 | // if the value comes from the unassigned entry, it's a vcard property dumped |
||
71 | try { |
||
72 | $property = \Sabre\VObject\Reader::read($ldapEntry[$lProperty][$j]); |
||
73 | $vcard->add($property); |
||
74 | } catch (exception $e) { |
||
75 | } |
||
76 | } else { |
||
77 | // Checks if a same kind of property already exists in the VCard (property and parameters) |
||
78 | // if so, sets a property variable with the current data |
||
79 | // else, creates a property variable |
||
80 | $v_property = $this->getOrCreateVCardProperty($vcard, $v_param, $j); |
||
81 | |||
82 | // modify the property with the new data |
||
83 | if (strcasecmp($v_param['image'], 'true') == 0) { |
||
84 | $this->updateVCardImageProperty($v_property, $ldapEntry[$lProperty][$j], $vcard->VERSION); |
||
85 | } else { |
||
86 | $this->updateVCardProperty($v_property, $ldapEntry[$lProperty][$j], $v_param['position']); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | if (!isset($vcard->UID)) { |
||
94 | $vcard->UID = base64_encode($ldapEntry['dn']); |
||
95 | } |
||
96 | return $vcard; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @brief returns the vcard property corresponding to the ldif parameter |
||
101 | * creates the property if it doesn't exists yet |
||
102 | * @param $vcard the vcard to get or create the properties with |
||
103 | * @param $v_param the parameter the find |
||
104 | * @param integer $index the position of the property in the vcard to find |
||
105 | */ |
||
106 | public function getOrCreateVCardProperty(&$vcard, $v_param, $index) { |
||
107 | |||
108 | // looking for one |
||
109 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' entering '.$vcard->serialize(), \OCP\Util::DEBUG); |
||
110 | $properties = $vcard->select($v_param['property']); |
||
111 | $counter = 0; |
||
112 | foreach ($properties as $property) { |
||
113 | if ($v_param['type'] == null) { |
||
114 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' property '.$v_param['type'].' found', \OCP\Util::DEBUG); |
||
115 | return $property; |
||
116 | } |
||
117 | foreach ($property->parameters as $parameter) { |
||
118 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' <> '.$v_param['type'], \OCP\Util::DEBUG); |
||
119 | if (!strcmp($parameter->getValue(), $v_param['type'])) { |
||
120 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' found', \OCP\Util::DEBUG); |
||
121 | if ($counter==$index) { |
||
122 | return $property; |
||
123 | } |
||
124 | $counter++; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Property not found, creating one |
||
130 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', create one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG); |
||
131 | $line = count($vcard->children) - 1; |
||
132 | $property = $vcard->createProperty($v_param['property']); |
||
133 | $vcard->add($property); |
||
134 | if ($v_param['type']!=null) { |
||
135 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', creating one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG); |
||
136 | //\OC_Log::write('ldapconnector', __METHOD__.', creating one '.$v_param['property'].';TYPE='.$v_param['type'], \OC_Log::DEBUG); |
||
137 | $property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.$v_param['type']); |
||
138 | switch ($v_param['property']) { |
||
139 | case "ADR": |
||
140 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', we have an address '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG); |
||
141 | $property->setValue(";;;;;;"); |
||
142 | break; |
||
143 | case "FN": |
||
144 | $property->setValue(";;;;"); |
||
145 | break; |
||
146 | } |
||
147 | } |
||
148 | //OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' exiting '.$vcard->serialize(), \OCP\Util::DEBUG); |
||
149 | return $property; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @brief modifies a vcard property array with the ldap_entry given in parameter at the given position |
||
154 | */ |
||
155 | public function updateVCardProperty(&$v_property, $ldap_entry, $position=null) { |
||
156 | for ($i=0; $i<count($v_property); $i++) { |
||
157 | if ($position != null) { |
||
158 | $v_array = explode(";", $v_property[$i]); |
||
159 | $v_array[intval($position)] = $ldap_entry; |
||
160 | $v_property[$i]->setValue(implode(";", $v_array)); |
||
161 | } else { |
||
162 | $v_property[$i]->setValue($ldap_entry); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @brief modifies a vcard property array with the image |
||
169 | */ |
||
170 | public function updateVCardImageProperty(&$v_property, $ldap_entry, $version) { |
||
171 | for ($i=0; $i<count($v_property); $i++) { |
||
172 | $image = new \OC_Image(); |
||
173 | $image->loadFromData($ldap_entry); |
||
174 | View Code Duplication | if (strcmp($version, '4.0') == 0) { |
|
175 | $type = $image->mimeType(); |
||
176 | } else { |
||
177 | $arrayType = explode('/', $image->mimeType()); |
||
178 | $type = strtoupper(array_pop($arrayType)); |
||
179 | } |
||
180 | $v_property[$i]->add('ENCODING', 'b'); |
||
181 | $v_property[$i]->add('TYPE', $type); |
||
182 | $v_property[$i]->setValue($image->__toString()); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @brief gets the vcard property values from an ldif entry name |
||
188 | * @param $lProperty the ldif property name |
||
189 | * @return array('property' => property, 'type' => type, 'position' => position) |
||
190 | */ |
||
191 | public function getVCardProperty($lProperty) { |
||
192 | $properties = array(); |
||
193 | if (strcmp($lProperty, $this->getUnassignedVCardProperty()) == 0) { |
||
194 | $properties[] = array('unassigned' => true); |
||
195 | } else { |
||
196 | foreach ($this->config_content->ldap_entries->ldif_entry as $ldif_entry) { |
||
197 | if ($lProperty == $ldif_entry['name']) { |
||
198 | // $ldif_entry['name'] is the right config xml |
||
199 | foreach ($ldif_entry->vcard_entry as $vcard_entry) { |
||
200 | $type=isset($vcard_entry['type'])?$vcard_entry['type']:""; |
||
201 | $position=isset($vcard_entry['position'])?$vcard_entry['position']:""; |
||
202 | $image=isset($ldif_entry['image'])?$ldif_entry['image']:""; |
||
203 | $properties[] = array('property' => $vcard_entry['property'], 'type' => $type, 'position' => $position, 'image' => $image); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | return $properties; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @brief return the ldif entries corresponding to the name and type given in parameter |
||
213 | * @param $propertyName the name of the vcard parameter |
||
214 | * @param $propertyType the type of the parameter |
||
215 | */ |
||
216 | public function getLdifEntry($propertyName, $propertyType) { |
||
217 | //\OC_Log::write('ldapconnector', __METHOD__."looking for $propertyName, $propertyType", \OC_Log::DEBUG); |
||
218 | if ($this->config_content !=null) { |
||
219 | $ldifEntries = array(); |
||
220 | foreach ($this->config_content->vcard_entries->vcard_entry as $vcardEntry) { |
||
221 | if (strcasecmp($vcardEntry['property'], $propertyName) == 0 && (!isset($vcardEntry['type']) || strcasecmp($vcardEntry['type'], $propertyType) == 0) && strcasecmp($vcardEntry['enabled'], 'true') == 0) { |
||
222 | foreach($vcardEntry->ldif_entry as $ldifEntry) { |
||
223 | $params = array(); |
||
224 | $params['name'] = $ldifEntry['name']; |
||
225 | if (isset($ldifEntry['vcard_position'])) { |
||
226 | $params['vcard_position'] = intval($ldifEntry['vcard_position']); |
||
227 | } |
||
228 | if (isset($vcardEntry['image']) && strcasecmp($vcardEntry['image'], 'true') == 0) { |
||
229 | $params['image'] = true; |
||
230 | } else { |
||
231 | $params['image'] = false; |
||
232 | } |
||
233 | $ldifEntries[] = $params; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | if (count($ldifEntries) == 0) { |
||
238 | $ldifEntries[] = array('unassigned' => true); |
||
239 | } |
||
240 | return $ldifEntries; |
||
241 | } |
||
242 | return null; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @brief transform a vcard into a ldap entry |
||
247 | * @param VCard $vcard |
||
248 | * @return array|false |
||
249 | */ |
||
250 | public function VCardToLdap($vcard) { |
||
251 | |||
252 | $ldifReturn = array(); // Array to return |
||
253 | foreach ($vcard->children() as $property) { |
||
254 | // Basically, a $property can be converted into one or multiple ldif entries |
||
255 | // and also some vcard properties can have data that can be split to fill different ldif entries |
||
256 | $ldifArray = self::getLdifProperty($property); |
||
257 | |||
258 | if (count($ldifArray) > 0) { |
||
259 | self::updateLdifProperty($ldifReturn, $ldifArray); |
||
260 | } |
||
261 | } |
||
262 | self::validateLdapEntry($ldifReturn); |
||
263 | return $ldifReturn; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @brief transform finds the ldif entries associated with the property |
||
268 | * @param Property $property |
||
269 | * @return array|false |
||
270 | */ |
||
271 | public function getLdifProperty($property) { |
||
272 | $ldifReturn = array(); |
||
273 | // Only one value per property, so we loop into types |
||
274 | // then for each one, look into config xml if there are ldif entries matching |
||
275 | $ldifEntries = self::getLdifEntry($property->name, $property['TYPE']); |
||
276 | // If one is found, create a tab entry like tab['ldif_entry'] |
||
277 | if ($ldifEntries != null && count($ldifEntries)>0) { |
||
278 | foreach ($ldifEntries as $ldifEntry) { |
||
279 | if (isset($ldifEntry['unassigned'])) { |
||
280 | if ((strcasecmp($property->name, "REV") != 0) && (strcasecmp($property->name, "VERSION") != 0) && (strcasecmp($property->name, "X-LDAP-DN") != 0)) { |
||
281 | // The unassigned properties are set in the ldap unassignedVCardProperty |
||
282 | $ldifReturn[(string)$this->getUnassignedVCardProperty()] = array($property->serialize()); |
||
283 | } |
||
284 | } else { |
||
285 | // Last, if the ldif entry has a vcard_position set, take only the value in the position index |
||
286 | $value = $property->getValue(); |
||
287 | if (isset($ldifEntry['vcard_position'])) { |
||
288 | //\OC_Log::write('ldapconnector', __METHOD__." position set ".$ldifEntry['vcard_position'], \OC_Log::DEBUG); |
||
289 | $tmpValues = explode(";", $property->getValue()); |
||
290 | $value = $tmpValues[$ldifEntry['vcard_position']]; |
||
291 | } |
||
292 | //\OC_Log::write('ldapconnector', __METHOD__.__METHOD__." entry : ".$ldifEntry['name']." - value : $value", \OC_Log::DEBUG); |
||
293 | // finally, sets tab['ldif_entry'][] with the value |
||
294 | if (strcmp($value, "") != 0) { |
||
295 | if ($ldifEntry['image']) { |
||
296 | $ldifReturn[(string)$ldifEntry['name']] = array(base64_decode($value)); |
||
297 | } else { |
||
298 | $ldifReturn[(string)$ldifEntry['name']] = array($value); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | return $ldifReturn; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @brief updates the ldifEntry with $ldifNewValues |
||
309 | * @param $ldifEntry the array to modify |
||
310 | * @param $ldifNewValues the new values |
||
311 | * @return boolean|null |
||
312 | */ |
||
313 | public function updateLdifProperty(&$ldifEntries, $ldifNewValues) { |
||
314 | foreach ($ldifNewValues as $key => $value) { |
||
315 | if (isset($ldifEntries[$key])) { |
||
316 | $ldifEntries[$key] = array_merge($ldifEntries[$key], $value); |
||
317 | } else { |
||
318 | $ldifEntries[$key] = $value; |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @brief returns all the ldap entries managed |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getLdapEntries() { |
||
344 | |||
345 | /** |
||
346 | * @brief returns the ldif entry for the VCard properties that don't have a ldap correspondance |
||
347 | * @return string|false |
||
348 | */ |
||
349 | View Code Duplication | public function getUnassignedVCardProperty() { |
|
350 | if ($this->config_content != null && $this->config_content->ldap_entries[0]->ldap_core[0]->unassigned_vcard_property['ldap_name'] != null) { |
||
351 | return ($this->config_content->ldap_entries[0]->ldap_core[0]->unassigned_vcard_property['ldap_name']); |
||
352 | } else { |
||
353 | return false; |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @brief get the id attribute in the ldap |
||
359 | * @return string|false |
||
360 | */ |
||
361 | View Code Duplication | public function getLdapId() { |
|
362 | if ($this->config_content != null && $this->config_content->ldap_entries[0]->ldap_core[0]->ldap_id['name'] != null) { |
||
363 | return ($this->config_content->ldap_entries[0]->ldap_core[0]->ldap_id['name']); |
||
364 | } else { |
||
365 | return false; |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @brief get the xml config name |
||
371 | * @return string|false |
||
372 | */ |
||
373 | public function getXmlConfigName() { |
||
374 | if ($this->config_content != null && $this->config_content['name'] != null) { |
||
375 | return ($this->config_content['name']); |
||
376 | } else { |
||
377 | return false; |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @brief checks if the ldapEntry is valid and fixes if possible |
||
383 | * @param $ldapEntry array |
||
384 | * @return boolean |
||
385 | */ |
||
386 | public function validateLdapEntry(&$ldapEntry) { |
||
387 | if ($this->config_content != null) { |
||
388 | $ldapEntry['objectclass'] = array(); |
||
389 | |||
390 | // Fill $ldapEntry with the objectClass params |
||
391 | foreach ($this->config_content->ldap_entries[0]->ldap_core[0]->object_class as $object_class) { |
||
392 | $ldapEntry['objectclass'][] = (string)$object_class['name']; |
||
393 | } |
||
394 | |||
395 | foreach ($this->config_content->ldap_entries[0]->ldap_core[0]->not_null as $not_null) { |
||
396 | $key = (string)$not_null['name']; |
||
397 | |||
398 | // Repair $ldapEntry if a field is null or empty |
||
399 | if (!array_key_exists($key, $ldapEntry) || strcmp('', $ldapEntry[$key][0]) == 0) { |
||
400 | |||
401 | // Switch with another entry |
||
402 | if (isset($not_null->action_switch[0])) { |
||
403 | $ldapEntry[$key][0] = $ldapEntry[(string)$not_null->action_switch[0]['name']][0]; |
||
404 | unset($ldapEntry[(string)$not_null->action_switch[0]['name']][0]); |
||
405 | if (count($ldapEntry[(string)$not_null->action_switch[0]['name']]) == 0) { |
||
406 | unset($ldapEntry[(string)$not_null->action_switch[0]['name']]); |
||
407 | } |
||
408 | } else if (isset($not_null->action_default[0])) { |
||
409 | // Fill with a default value |
||
410 | $ldapEntry[$key][0] = (string)$not_null->action_default[0]['value']; |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | foreach ($this->config_content->vcard_entries[0]->vcard_entry as $vcardEntry) { |
||
416 | foreach ($vcardEntry->ldif_entry as $ldifEntry) { |
||
417 | // Remove duplicates if relevant |
||
418 | if (strcmp("true", $ldifEntry['unique'])==0 && array_key_exists((string)$ldifEntry['name'], $ldapEntry)) { // Y aller à coup de "bool array_key_exists ( mixed $key , array $search )" |
||
419 | // Holy hand-grenade, there are like 3 imbricated loops... |
||
420 | $ldapEntry[(string)$ldifEntry['name']] = array_unique($ldapEntry[(string)$ldifEntry['name']]); |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | |||
425 | return true; |
||
426 | } else { |
||
427 | return false; |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @brief adds empty entries in $dest if $dest doesn't have those entries and if $source has |
||
433 | * otherwise, I couldn't find how to remove attributes |
||
434 | * @param $source the source ldap entry as model |
||
435 | * @param $dest the destination entry to add empty params if we have to |
||
436 | */ |
||
437 | public function insertEmptyEntries($source, &$dest) { |
||
446 | } |
||
447 | |||
448 | ?> |
||
449 |