| Conditions | 34 |
| Paths | 132 |
| Total Lines | 106 |
| Code Lines | 75 |
| Lines | 55 |
| Ratio | 51.89 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 11 | function update_active_ad_users() { |
||
|
|
|||
| 12 | |||
| 13 | $ldap_password = $this->config->item('ldapbindpass'); |
||
| 14 | $ldap_username = $this->config->item('ldapshortdomain').$this->config->item('ldapbindun'); |
||
| 15 | $ldap_connection = ldap_connect($this->config->item('ldapserver')); |
||
| 16 | |||
| 17 | ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version'); |
||
| 18 | ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); |
||
| 19 | |||
| 20 | if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)) { |
||
| 21 | $search_filter = '(&(objectCategory=person)(samaccountname=*))'; |
||
| 22 | $attributes = array(); |
||
| 23 | $attributes[] = 'givenname'; |
||
| 24 | $attributes[] = 'samaccountname'; |
||
| 25 | $attributes[] = 'sn'; |
||
| 26 | $attributes[] = 'mail'; |
||
| 27 | $ad_users = array(); |
||
| 28 | |||
| 29 | //empty database |
||
| 30 | $this->db->truncate('users_ad'); |
||
| 31 | |||
| 32 | $result_admin = ldap_search($ldap_connection, $this->config->item('ldapuserjobouone'), $search_filter, $attributes); |
||
| 33 | View Code Duplication | if (FALSE !== $result_admin) { |
|
| 34 | $entries = ldap_get_entries($ldap_connection, $result_admin); |
||
| 35 | for ($x = 0; $x < $entries['count']; $x++) { |
||
| 36 | if (!empty($entries[$x]['givenname'][0]) && |
||
| 37 | !empty($entries[$x]['samaccountname'][0]) && |
||
| 38 | !empty($entries[$x]['sn'][0]) && |
||
| 39 | !empty($entries[$x]['mail'][0])) { |
||
| 40 | $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0])); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | $result_canterbury = ldap_search($ldap_connection, $this->config->item('ldapuserjoboutwo'), $search_filter, $attributes); |
||
| 45 | View Code Duplication | if (FALSE !== $result_canterbury) { |
|
| 46 | $entries = ldap_get_entries($ldap_connection, $result_canterbury); |
||
| 47 | for ($x = 0; $x < $entries['count']; $x++) { |
||
| 48 | if (!empty($entries[$x]['givenname'][0]) && |
||
| 49 | !empty($entries[$x]['samaccountname'][0]) && |
||
| 50 | !empty($entries[$x]['sn'][0]) && |
||
| 51 | !empty($entries[$x]['mail'][0])) { |
||
| 52 | $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0])); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | $result_sheppy = ldap_search($ldap_connection, $this->config->item('ldapuserjobouthree'), $search_filter, $attributes); |
||
| 57 | View Code Duplication | if (FALSE !== $result_sheppy) { |
|
| 58 | $entries = ldap_get_entries($ldap_connection, $result_sheppy); |
||
| 59 | for ($x = 0; $x < $entries['count']; $x++) { |
||
| 60 | if (!empty($entries[$x]['givenname'][0]) && |
||
| 61 | !empty($entries[$x]['samaccountname'][0]) && |
||
| 62 | !empty($entries[$x]['sn'][0]) && |
||
| 63 | !empty($entries[$x]['mail'][0])) { |
||
| 64 | $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0])); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | $result_nicks = ldap_search($ldap_connection, $this->config->item('ldapuserjoboufour'), $search_filter, $attributes); |
||
| 69 | View Code Duplication | if (FALSE !== $result_nicks) { |
|
| 70 | $entries = ldap_get_entries($ldap_connection, $result_nicks); |
||
| 71 | for ($x = 0; $x < $entries['count']; $x++) { |
||
| 72 | if (!empty($entries[$x]['givenname'][0]) && |
||
| 73 | !empty($entries[$x]['samaccountname'][0]) && |
||
| 74 | !empty($entries[$x]['sn'][0]) && |
||
| 75 | !empty($entries[$x]['mail'][0])) { |
||
| 76 | $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0])); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | $result_nicks = ldap_search($ldap_connection, $this->config->item('ldapuserjoboufive'), $search_filter, $attributes); |
||
| 81 | View Code Duplication | if (FALSE !== $result_nicks) { |
|
| 82 | $entries = ldap_get_entries($ldap_connection, $result_nicks); |
||
| 83 | for ($x = 0; $x < $entries['count']; $x++) { |
||
| 84 | if (!empty($entries[$x]['givenname'][0]) && |
||
| 85 | !empty($entries[$x]['samaccountname'][0]) && |
||
| 86 | !empty($entries[$x]['sn'][0]) && |
||
| 87 | !empty($entries[$x]['mail'][0])) { |
||
| 88 | $ad_users[] = array('first_name' => trim($entries[$x]['givenname'][0]), 'last_name' => trim($entries[$x]['sn'][0]), 'full_name' => trim($entries[$x]['givenname'][0]).' '. trim($entries[$x]['sn'][0]), 'username' => trim($entries[$x]['samaccountname'][0]), 'email' => trim($entries[$x]['mail'][0])); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | ldap_unbind($ldap_connection); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($this->db->insert_batch('users_ad', $ad_users)) { |
||
| 97 | |||
| 98 | $ip = $_SERVER['REMOTE_ADDR']; |
||
| 99 | $data = array( |
||
| 100 | 'timestamp' => date("Y-m-d H:i:s", time()), |
||
| 101 | 'ip' => $ip, |
||
| 102 | 'job' => 'Users updated successfully.', |
||
| 103 | ); |
||
| 104 | |||
| 105 | return $this->db->insert('job_log', $data); |
||
| 106 | } else { |
||
| 107 | |||
| 108 | $ip = $_SERVER['REMOTE_ADDR']; |
||
| 109 | $data = array( |
||
| 110 | 'timestamp' => date("Y-m-d H:i:s", time()), |
||
| 111 | 'ip' => $ip, |
||
| 112 | 'job' => 'Users updated failed.', |
||
| 113 | ); |
||
| 114 | return $this->db->insert('job_log', $data); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.