| Conditions | 10 |
| Paths | 10 |
| Total Lines | 90 |
| Lines | 32 |
| Ratio | 35.56 % |
| 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 |
||
| 27 | public function run($request) |
||
| 28 | { |
||
| 29 | // get all groups from LDAP, but only get the attributes we need. |
||
| 30 | // this is useful to avoid holding onto too much data in memory |
||
| 31 | // especially in the case where getGroups() would return a lot of groups |
||
| 32 | $ldapGroups = $this->ldapService->getGroups( |
||
| 33 | false, |
||
| 34 | ['objectguid', 'samaccountname', 'dn', 'name', 'description'], |
||
| 35 | // Change the indexing attribute so we can look up by GUID during the deletion process below. |
||
| 36 | 'objectguid' |
||
| 37 | ); |
||
| 38 | |||
| 39 | $start = time(); |
||
| 40 | |||
| 41 | $created = 0; |
||
| 42 | $updated = 0; |
||
| 43 | $deleted = 0; |
||
| 44 | |||
| 45 | View Code Duplication | foreach ($ldapGroups as $data) { |
|
| 46 | $group = Group::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
| 47 | |||
| 48 | if (!($group && $group->exists())) { |
||
| 49 | // create the initial Group with some internal fields |
||
| 50 | $group = new Group(); |
||
| 51 | $group->GUID = $data['objectguid']; |
||
| 52 | |||
| 53 | $this->log(sprintf( |
||
| 54 | 'Creating new Group (GUID: %s, sAMAccountName: %s)', |
||
| 55 | $data['objectguid'], |
||
| 56 | $data['samaccountname'] |
||
| 57 | )); |
||
| 58 | $created++; |
||
| 59 | } else { |
||
| 60 | $this->log(sprintf( |
||
| 61 | 'Updating existing Group "%s" (ID: %s, GUID: %s, sAMAccountName: %s)', |
||
| 62 | $group->getTitle(), |
||
| 63 | $group->ID, |
||
| 64 | $data['objectguid'], |
||
| 65 | $data['samaccountname'] |
||
| 66 | )); |
||
| 67 | $updated++; |
||
| 68 | } |
||
| 69 | |||
| 70 | try { |
||
| 71 | $this->ldapService->updateGroupFromLDAP($group, $data); |
||
| 72 | } catch (Exception $e) { |
||
| 73 | $this->log($e->getMessage()); |
||
| 74 | continue; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // remove Group records that were previously imported, but no longer exist in the directory |
||
| 79 | // NOTE: DB::query() here is used for performance and so we don't run out of memory |
||
| 80 | if ($this->config()->destructive) { |
||
| 81 | foreach (DB::query('SELECT "ID", "GUID" FROM "Group" WHERE "GUID" IS NOT NULL') as $record) { |
||
| 82 | if (!isset($ldapGroups[$record['GUID']])) { |
||
| 83 | $group = Group::get()->byId($record['ID']); |
||
| 84 | |||
| 85 | $this->log(sprintf( |
||
| 86 | 'Removing Group "%s" (GUID: %s) that no longer exists in LDAP.', |
||
| 87 | $group->Title, |
||
| 88 | $group->GUID |
||
| 89 | )); |
||
| 90 | |||
| 91 | try { |
||
| 92 | // Cascade into mappings, just to clean up behind ourselves. |
||
| 93 | foreach ($group->LDAPGroupMappings() as $mapping) { |
||
| 94 | $mapping->delete(); |
||
| 95 | } |
||
| 96 | $group->delete(); |
||
| 97 | } catch (Exception $e) { |
||
| 98 | $this->log($e->getMessage()); |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $deleted++; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $end = time() - $start; |
||
| 108 | |||
| 109 | $this->log(sprintf( |
||
| 110 | 'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds', |
||
| 111 | $created, |
||
| 112 | $updated, |
||
| 113 | $deleted, |
||
| 114 | round($end, 0) |
||
| 115 | )); |
||
| 116 | } |
||
| 117 | |||
| 124 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.