Conditions | 9 |
Paths | 10 |
Total Lines | 86 |
Lines | 34 |
Ratio | 39.53 % |
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 users 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 getUser() would return a lot of users |
||
32 | $users = $this->ldapService->getUsers(array_merge( |
||
33 | ['objectguid', 'samaccountname', 'useraccountcontrol', 'memberof'], |
||
34 | array_keys(Config::inst()->get('Member', 'ldap_field_mappings')) |
||
35 | )); |
||
36 | |||
37 | $start = time(); |
||
38 | |||
39 | $created = 0; |
||
40 | $updated = 0; |
||
41 | $deleted = 0; |
||
42 | |||
43 | View Code Duplication | foreach ($users as $data) { |
|
44 | $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
45 | |||
46 | if (!($member && $member->exists())) { |
||
47 | // create the initial Member with some internal fields |
||
48 | $member = new Member(); |
||
49 | $member->GUID = $data['objectguid']; |
||
50 | |||
51 | $this->log(sprintf( |
||
52 | 'Creating new Member (GUID: %s, sAMAccountName: %s)', |
||
53 | $data['objectguid'], |
||
54 | $data['samaccountname'] |
||
55 | )); |
||
56 | $created++; |
||
57 | } else { |
||
58 | $this->log(sprintf( |
||
59 | 'Updating existing Member "%s" (ID: %s, GUID: %s, sAMAccountName: %s)', |
||
60 | $member->getName(), |
||
61 | $member->ID, |
||
62 | $data['objectguid'], |
||
63 | $data['samaccountname'] |
||
64 | )); |
||
65 | $updated++; |
||
66 | } |
||
67 | |||
68 | // Sync attributes from LDAP to the Member record. This will also write the Member record. |
||
69 | // this is also responsible for putting the user into mapped groups |
||
70 | try { |
||
71 | $this->ldapService->updateMemberFromLDAP($member, $data); |
||
72 | } catch (Exception $e) { |
||
73 | $this->log($e->getMessage()); |
||
74 | continue; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // remove Member 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 "Member" WHERE "GUID" IS NOT NULL') as $record) { |
||
82 | $member = Member::get()->byId($record['ID']); |
||
83 | |||
84 | if (!isset($users[$record['GUID']])) { |
||
85 | $this->log(sprintf( |
||
86 | 'Removing Member "%s" (GUID: %s) that no longer exists in LDAP.', |
||
87 | $member->getName(), |
||
88 | $member->GUID |
||
89 | )); |
||
90 | |||
91 | try { |
||
92 | $member->delete(); |
||
93 | } catch (Exception $e) { |
||
94 | $this->log($e->getMessage()); |
||
95 | continue; |
||
96 | } |
||
97 | |||
98 | $deleted++; |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $end = time() - $start; |
||
104 | |||
105 | $this->log(sprintf( |
||
106 | 'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds', |
||
107 | $created, |
||
108 | $updated, |
||
109 | $deleted, |
||
110 | round($end, 0) |
||
111 | )); |
||
112 | } |
||
113 | |||
120 |
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.