Complex classes like LDAPMemberExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAPMemberExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class LDAPMemberExtension extends DataExtension |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private static $db = array( |
||
13 | // Unique user identifier, same field is used by SAMLMemberExtension |
||
14 | 'GUID' => 'Varchar(50)', |
||
15 | 'Username' => 'Varchar(64)', |
||
16 | 'IsImportedFromLDAP' => 'Boolean', |
||
17 | 'IsExpired' => 'Boolean', |
||
18 | 'LastSynced' => 'SS_Datetime', |
||
19 | ); |
||
20 | |||
21 | /** |
||
22 | * These fields are used by {@link LDAPMemberSync} to map specific AD attributes |
||
23 | * to {@link Member} fields. |
||
24 | * |
||
25 | * @var array |
||
26 | * @config |
||
27 | */ |
||
28 | private static $ldap_field_mappings = array( |
||
29 | 'samaccountname' => 'Username', |
||
30 | 'givenname' => 'FirstName', |
||
31 | 'sn' => 'Surname', |
||
32 | 'mail' => 'Email', |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * The location (relative to /assets) where to save thumbnailphoto data. |
||
37 | * |
||
38 | * @var string |
||
39 | * @config |
||
40 | */ |
||
41 | private static $ldap_thumbnail_path = 'Uploads'; |
||
42 | |||
43 | /** |
||
44 | * When enabled, any user that is in LDAP has their data written back |
||
45 | * to LDAP. This is a push to LDAP, rather than {@link LDAPMemberSyncTask} |
||
46 | * which pulls from it. This also requires setting write permissions on the |
||
47 | * user who talks to LDAP, which is why it is disabled by default. |
||
48 | * |
||
49 | * Note that some constants must be configured in your environment file |
||
50 | * for this to work: |
||
51 | * |
||
52 | * LDAP_DOMAIN - the base DN of the directory. e.g. "DN=mydomain,DC=com" |
||
53 | * LDAP_NEW_USERS_OBJECT_CATEGORY - the type of object. e.g. "CN=Person,CN=Schema,DC=mydomain,DC=com" |
||
54 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
55 | * |
||
56 | * @var bool |
||
57 | * @config |
||
58 | */ |
||
59 | private static $reverse_sync_ldap = false; |
||
60 | |||
61 | /** |
||
62 | * If enabled, new users written are also created in LDAP. |
||
63 | * Please see reverse_sync_ldap for constants that must be configured in |
||
64 | * your environment file for this to work. |
||
65 | * |
||
66 | * @var bool |
||
67 | * @config |
||
68 | */ |
||
69 | private static $create_new_users_in_ldap = false; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $dependencies = array( |
||
75 | 'ldapService' => '%$LDAPService', |
||
76 | ); |
||
77 | |||
78 | public function updateSummaryFields(&$fields) |
||
82 | |||
83 | /** |
||
84 | * @param FieldList $fields |
||
85 | */ |
||
86 | public function updateCMSFields(FieldList $fields) |
||
133 | |||
134 | /** |
||
135 | * Creates a new LDAP user given the current Member details. Assumption is |
||
136 | * the record has been validated for the presence of FirstName, Surname, Email, |
||
137 | * and Username prior to the request being sent to LDAP. |
||
138 | */ |
||
139 | public function createUser() |
||
181 | |||
182 | /** |
||
183 | * Sync the Member data back to the corresponding LDAP user object. |
||
184 | * |
||
185 | * This is effectively a reverse sync, so we don't want to be doing |
||
186 | * this onBeforeWrite as LDAPMemberSyncTask could get it into a loop. |
||
187 | * This method should be called explicitly when a sync of the |
||
188 | * Platform Dashboard user back to LDAP is required. |
||
189 | * |
||
190 | * @throws ValidationException |
||
191 | */ |
||
192 | public function sync() |
||
231 | |||
232 | public function validate(ValidationResult $validationResult) |
||
253 | |||
254 | /** |
||
255 | * Ensure the user belongs to the correct groups in LDAP, making the |
||
256 | * assumption that the assigned groups are correct. |
||
257 | * This is considered a reverse sync back to LDAP. |
||
258 | * |
||
259 | * This also removes them from LDAP groups if they've been taken out of one. |
||
260 | * It will not affect group membership of non-mapped groups, so it will |
||
261 | * not touch such internal AD groups like "Domain Users". |
||
262 | */ |
||
263 | public function syncGroups() |
||
351 | |||
352 | /** |
||
353 | * Given a group DN, look up the group membership data in LDAP. |
||
354 | * |
||
355 | * @param string $groupDn |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | protected function getLDAPGroupMembers($groupDn) |
||
377 | |||
378 | /** |
||
379 | * Create the user in LDAP and mark as synced, provided that |
||
380 | * reverse sync is enabled. |
||
381 | * |
||
382 | * Set a flag "Creating" so other extensions using on*() events can |
||
383 | * detect whether it's in a state of being created, such as for |
||
384 | * synchronising with other services when a user is being created |
||
385 | * in LDAP for the first time. |
||
386 | */ |
||
387 | public function onBeforeWrite() |
||
407 | |||
408 | /** |
||
409 | * Sync the local data with LDAP, and ensure local membership is also set in |
||
410 | * LDAP too. This writes into LDAP, provided reverse sync is enabled. |
||
411 | */ |
||
412 | public function onAfterWrite() |
||
434 | |||
435 | /** |
||
436 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
437 | * this will update the Member record from AD data. |
||
438 | */ |
||
439 | public function memberLoggedIn() |
||
445 | } |
||
446 |
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.