Conditions | 8 |
Paths | 10 |
Total Lines | 87 |
Code Lines | 53 |
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 |
||
60 | public function run($request) |
||
61 | { |
||
62 | ini_set('max_execution_time', 3600); // 3600s = 1hr |
||
63 | ini_set('memory_limit', '1024M'); // 1GB memory limit |
||
64 | |||
65 | // get all users from LDAP, but only get the attributes we need. |
||
66 | // this is useful to avoid holding onto too much data in memory |
||
67 | // especially in the case where getUser() would return a lot of users |
||
68 | $users = $this->ldapService->getUsers(array_merge( |
||
69 | ['objectguid', 'cn', 'samaccountname', 'useraccountcontrol', 'memberof'], |
||
70 | array_keys(Config::inst()->get(Member::class, 'ldap_field_mappings')) |
||
71 | )); |
||
72 | |||
73 | $start = time(); |
||
74 | |||
75 | $created = 0; |
||
76 | $updated = 0; |
||
77 | $deleted = 0; |
||
78 | |||
79 | foreach ($users as $data) { |
||
80 | $member = $this->findOrCreateMember($data); |
||
81 | |||
82 | // If member exists already, we're updating - otherwise we're creating |
||
83 | if ($member->exists()) { |
||
84 | $updated++; |
||
85 | $this->log(sprintf( |
||
86 | 'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)', |
||
87 | $data['objectguid'], |
||
88 | $member->getName(), |
||
89 | $member->ID, |
||
90 | $data['samaccountname'] |
||
91 | )); |
||
92 | } else { |
||
93 | $created++; |
||
94 | $this->log(sprintf( |
||
95 | 'Creating new Member %s: "%s" (SAM Account Name: %s)', |
||
96 | $data['objectguid'], |
||
97 | $data['cn'], |
||
98 | $data['samaccountname'] |
||
99 | )); |
||
100 | } |
||
101 | |||
102 | // Sync attributes from LDAP to the Member record. This will also write the Member record. |
||
103 | // this is also responsible for putting the user into mapped groups |
||
104 | try { |
||
105 | $this->ldapService->updateMemberFromLDAP($member, $data); |
||
106 | } catch (Exception $e) { |
||
107 | $this->log($e->getMessage()); |
||
108 | continue; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | // remove Member 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 "Member" WHERE "GUID" IS NOT NULL') as $record) { |
||
116 | $member = Member::get()->byId($record['ID']); |
||
117 | |||
118 | if (!isset($users[$record['GUID']])) { |
||
119 | $this->log(sprintf( |
||
120 | 'Removing Member "%s" (GUID: %s) that no longer exists in LDAP.', |
||
121 | $member->getName(), |
||
122 | $member->GUID |
||
123 | )); |
||
124 | |||
125 | try { |
||
126 | $member->delete(); |
||
127 | } catch (Exception $e) { |
||
128 | $this->log($e->getMessage()); |
||
129 | continue; |
||
130 | } |
||
131 | |||
132 | $deleted++; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $this->invokeWithExtensions('onAfterLDAPMemberSyncTask'); |
||
138 | |||
139 | $end = time() - $start; |
||
140 | |||
141 | $this->log(sprintf( |
||
142 | 'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds', |
||
143 | $created, |
||
144 | $updated, |
||
145 | $deleted, |
||
146 | round($end, 0) |
||
147 | )); |
||
190 |