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