Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class LDAPMemberExtension extends DataExtension |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private static $db = [ |
||
13 | // Unique user identifier, same field is used by SAMLMemberExtension |
||
14 | 'GUID' => 'Varchar(50)', |
||
15 | 'Username' => 'Varchar(64)', |
||
16 | 'IsExpired' => 'Boolean', |
||
17 | 'LastSynced' => 'SS_Datetime', |
||
18 | ]; |
||
19 | |||
20 | /** |
||
21 | * These fields are used by {@link LDAPMemberSync} to map specific AD attributes |
||
22 | * to {@link Member} fields. |
||
23 | * |
||
24 | * @var array |
||
25 | * @config |
||
26 | */ |
||
27 | private static $ldap_field_mappings = [ |
||
28 | 'givenname' => 'FirstName', |
||
29 | 'samaccountname' => 'Username', |
||
30 | 'sn' => 'Surname', |
||
31 | 'mail' => 'Email', |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * The location (relative to /assets) where to save thumbnailphoto data. |
||
36 | * |
||
37 | * @var string |
||
38 | * @config |
||
39 | */ |
||
40 | private static $ldap_thumbnail_path = 'Uploads'; |
||
41 | |||
42 | /** |
||
43 | * When enabled, LDAP managed Member records (GUID flag) |
||
44 | * have their data written back to LDAP on write. |
||
45 | * |
||
46 | * This requires setting write permissions on the user configured in the LDAP |
||
47 | * credentials, which is why this is disabled by default. |
||
48 | * |
||
49 | * @var bool |
||
50 | * @config |
||
51 | */ |
||
52 | private static $update_ldap_from_local = false; |
||
53 | |||
54 | /** |
||
55 | * If enabled, Member records are created in LDAP on write. |
||
56 | * |
||
57 | * This requires setting write permissions on the user configured in the LDAP |
||
58 | * credentials, which is why this is disabled by default. |
||
59 | * |
||
60 | * @var bool |
||
61 | * @config |
||
62 | */ |
||
63 | private static $create_users_in_ldap = false; |
||
64 | |||
65 | /** |
||
66 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
67 | * |
||
68 | * This requires setting write permissions on the user configured in the LDAP |
||
69 | * credentials, which is why this is disabled by default. |
||
70 | * |
||
71 | * @var bool |
||
72 | * @config |
||
73 | */ |
||
74 | private static $delete_users_in_ldap = false; |
||
75 | |||
76 | /** |
||
77 | * @param FieldList $fields |
||
78 | */ |
||
79 | public function updateCMSFields(FieldList $fields) |
||
129 | |||
130 | public function validate(ValidationResult $validationResult) |
||
147 | |||
148 | /** |
||
149 | * Generate a username for the user based on their name and/or email |
||
150 | * @return string |
||
151 | */ |
||
152 | public function generateLDAPUsername() |
||
185 | |||
186 | /** |
||
187 | * Create the user in LDAP, provided this configuration is enabled |
||
188 | * and a username was passed to a new Member record. |
||
189 | */ |
||
190 | public function onBeforeWrite() |
||
208 | |||
209 | /** |
||
210 | * Update the local data with LDAP, and ensure local membership is also set in |
||
211 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
212 | */ |
||
213 | View Code Duplication | public function onAfterWrite() |
|
227 | |||
228 | View Code Duplication | public function onAfterDelete() { |
|
240 | |||
241 | /** |
||
242 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
243 | * this will update the Member record from AD data. |
||
244 | */ |
||
245 | public function memberLoggedIn() |
||
251 | } |
||
252 |
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.