| Conditions | 8 | 
| Paths | 6 | 
| Total Lines | 76 | 
| Code Lines | 41 | 
| Lines | 31 | 
| Ratio | 40.79 % | 
| 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 | ||
| 53 | public function run($request) | ||
| 54 |     { | ||
| 55 | // get all groups from LDAP, but only get the attributes we need. | ||
| 56 | // this is useful to avoid holding onto too much data in memory | ||
| 57 | // especially in the case where getGroups() would return a lot of groups | ||
| 58 | $ldapGroups = $this->ldapService->getGroups( | ||
| 59 | false, | ||
| 60 | ['objectguid', 'samaccountname', 'dn', 'name', 'description'], | ||
| 61 | // Change the indexing attribute so we can look up by GUID during the deletion process below. | ||
| 62 | 'objectguid' | ||
| 63 | ); | ||
| 64 | |||
| 65 | $start = time(); | ||
| 66 | |||
| 67 | $count = 0; | ||
| 68 | |||
| 69 | View Code Duplication |         foreach ($ldapGroups as $data) { | |
| 70 |             $group = Group::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); | ||
| 71 | |||
| 72 |             if (!($group && $group->exists())) { | ||
| 73 | // create the initial Group with some internal fields | ||
| 74 | $group = new Group(); | ||
| 75 | $group->GUID = $data['objectguid']; | ||
| 76 | |||
| 77 | $this->log(sprintf( | ||
| 78 | 'Creating new Group (ID: %s, GUID: %s, sAMAccountName: %s)', | ||
| 79 | $group->ID, | ||
| 80 | $data['objectguid'], | ||
| 81 | $data['samaccountname'] | ||
| 82 | )); | ||
| 83 |             } else { | ||
| 84 | $this->log(sprintf( | ||
| 85 | 'Updating existing Group "%s" (ID: %s, GUID: %s, sAMAccountName: %s)', | ||
| 86 | $group->getTitle(), | ||
| 87 | $group->ID, | ||
| 88 | $data['objectguid'], | ||
| 89 | $data['samaccountname'] | ||
| 90 | )); | ||
| 91 | } | ||
| 92 | |||
| 93 | $this->ldapService->updateGroupFromLDAP($group, $data); | ||
| 94 | |||
| 95 | // cleanup object from memory | ||
| 96 | $group->destroy(); | ||
| 97 | |||
| 98 | $count++; | ||
| 99 | } | ||
| 100 | |||
| 101 | // remove Group records that were previously imported, but no longer exist in the directory | ||
| 102 | // NOTE: DB::query() here is used for performance and so we don't run out of memory | ||
| 103 |         if ($this->config()->destructive) { | ||
| 104 |             foreach (DB::query('SELECT "ID", "GUID" FROM "Group" WHERE "GUID" IS NOT NULL') as $record) { | ||
| 105 |                 if (!isset($ldapGroups[$record['GUID']])) { | ||
| 106 | $group = Group::get()->byId($record['ID']); | ||
| 107 | // Cascade into mappings, just to clean up behind ourselves. | ||
| 108 |                     foreach ($group->LDAPGroupMappings() as $mapping) { | ||
| 109 | $mapping->delete(); | ||
| 110 | } | ||
| 111 | $group->delete(); | ||
| 112 | |||
| 113 | $this->log(sprintf( | ||
| 114 | 'Removing Group "%s" (GUID: %s) that no longer exists in LDAP.', | ||
| 115 | $group->Title, | ||
| 116 | $group->GUID | ||
| 117 | )); | ||
| 118 | |||
| 119 | // cleanup object from memory | ||
| 120 | $group->destroy(); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | $end = time() - $start; | ||
| 126 | |||
| 127 |         $this->log(sprintf('Done. Processed %s records. Duration: %s seconds', $count, round($end, 0))); | ||
| 128 | } | ||
| 129 | |||
| 141 |