Conditions | 9 |
Paths | 10 |
Total Lines | 84 |
Code Lines | 51 |
Lines | 34 |
Ratio | 40.48 % |
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 |
||
55 | public function run($request) |
||
56 | { |
||
57 | // get all users from LDAP, but only get the attributes we need. |
||
58 | // this is useful to avoid holding onto too much data in memory |
||
59 | // especially in the case where getUser() would return a lot of users |
||
60 | $users = $this->ldapService->getUsers(array_merge( |
||
61 | ['objectguid', 'samaccountname', 'useraccountcontrol', 'memberof'], |
||
62 | array_keys(Config::inst()->get('SilverStripe\\Security\\Member', 'ldap_field_mappings')) |
||
63 | )); |
||
64 | |||
65 | $start = time(); |
||
66 | |||
67 | $created = 0; |
||
68 | $updated = 0; |
||
69 | $deleted = 0; |
||
70 | |||
71 | View Code Duplication | foreach ($users as $data) { |
|
72 | $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
73 | |||
74 | if (!($member && $member->exists())) { |
||
75 | // create the initial Member with some internal fields |
||
76 | $member = new Member(); |
||
77 | $member->GUID = $data['objectguid']; |
||
78 | |||
79 | $this->log(sprintf( |
||
80 | 'Creating new Member (GUID: %s, sAMAccountName: %s)', |
||
81 | $data['objectguid'], |
||
82 | $data['samaccountname'] |
||
83 | )); |
||
84 | $created++; |
||
85 | } else { |
||
86 | $this->log(sprintf( |
||
87 | 'Updating existing Member "%s" (ID: %s, GUID: %s, sAMAccountName: %s)', |
||
88 | $member->getName(), |
||
89 | $member->ID, |
||
90 | $data['objectguid'], |
||
91 | $data['samaccountname'] |
||
92 | )); |
||
93 | $updated++; |
||
94 | } |
||
95 | |||
96 | // Sync attributes from LDAP to the Member record. This will also write the Member record. |
||
97 | // this is also responsible for putting the user into mapped groups |
||
98 | try { |
||
99 | $this->ldapService->updateMemberFromLDAP($member, $data); |
||
100 | } catch (Exception $e) { |
||
101 | $this->log($e->getMessage()); |
||
102 | continue; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // remove Member records that were previously imported, but no longer exist in the directory |
||
107 | // NOTE: DB::query() here is used for performance and so we don't run out of memory |
||
108 | if ($this->config()->destructive) { |
||
109 | foreach (DB::query('SELECT "ID", "GUID" FROM "Member" WHERE "GUID" IS NOT NULL') as $record) { |
||
110 | $member = Member::get()->byId($record['ID']); |
||
111 | |||
112 | if (!isset($users[$record['GUID']])) { |
||
113 | $this->log(sprintf( |
||
114 | 'Removing Member "%s" (GUID: %s) that no longer exists in LDAP.', |
||
115 | $member->getName(), |
||
116 | $member->GUID |
||
117 | )); |
||
118 | |||
119 | try { |
||
120 | $member->delete(); |
||
121 | } catch (Exception $e) { |
||
122 | $this->log($e->getMessage()); |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | $deleted++; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $end = time() - $start; |
||
132 | |||
133 | $this->log(sprintf( |
||
134 | 'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds', |
||
135 | $created, |
||
136 | $updated, |
||
137 | $deleted, |
||
138 | round($end, 0) |
||
139 | )); |
||
153 |
This check marks private properties in classes that are never used. Those properties can be removed.