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 |
||
20 | class LDAPMemberExtension extends DataExtension |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private static $db = [ |
||
|
|||
26 | // Unique user identifier, same field is used by SAMLMemberExtension |
||
27 | 'GUID' => 'Varchar(50)', |
||
28 | 'Username' => 'Varchar(64)', |
||
29 | 'IsExpired' => 'Boolean', |
||
30 | 'LastSynced' => 'DBDatetime', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * These fields are used by {@link LDAPMemberSync} to map specific AD attributes |
||
35 | * to {@link Member} fields. |
||
36 | * |
||
37 | * @var array |
||
38 | * @config |
||
39 | */ |
||
40 | private static $ldap_field_mappings = [ |
||
41 | 'givenname' => 'FirstName', |
||
42 | 'samaccountname' => 'Username', |
||
43 | 'sn' => 'Surname', |
||
44 | 'mail' => 'Email', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * The location (relative to /assets) where to save thumbnailphoto data. |
||
49 | * |
||
50 | * @var string |
||
51 | * @config |
||
52 | */ |
||
53 | private static $ldap_thumbnail_path = 'Uploads'; |
||
54 | |||
55 | /** |
||
56 | * When enabled, LDAP managed Member records (GUID flag) |
||
57 | * have their data written back to LDAP on write, and synchronise |
||
58 | * membership to groups mapped to LDAP. |
||
59 | * |
||
60 | * Keep in mind this will currently NOT trigger if there are no |
||
61 | * field changes due to onAfterWrite in framework not being called |
||
62 | * when there are no changes. |
||
63 | * |
||
64 | * This requires setting write permissions on the user configured in the LDAP |
||
65 | * credentials, which is why this is disabled by default. |
||
66 | * |
||
67 | * @var bool |
||
68 | * @config |
||
69 | */ |
||
70 | private static $update_ldap_from_local = false; |
||
71 | |||
72 | /** |
||
73 | * If enabled, Member records with a Username field have the user created in LDAP |
||
74 | * on write. |
||
75 | * |
||
76 | * This requires setting write permissions on the user configured in the LDAP |
||
77 | * credentials, which is why this is disabled by default. |
||
78 | * |
||
79 | * @var bool |
||
80 | * @config |
||
81 | */ |
||
82 | private static $create_users_in_ldap = false; |
||
83 | |||
84 | /** |
||
85 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
86 | * |
||
87 | * This requires setting write permissions on the user configured in the LDAP |
||
88 | * credentials, which is why this is disabled by default. |
||
89 | * |
||
90 | * @var bool |
||
91 | * @config |
||
92 | */ |
||
93 | private static $delete_users_in_ldap = false; |
||
94 | |||
95 | /** |
||
96 | * @param FieldList $fields |
||
97 | */ |
||
98 | public function updateCMSFields(FieldList $fields) |
||
154 | |||
155 | /** |
||
156 | * @param ValidationResult |
||
157 | * @throws ValidationException |
||
158 | */ |
||
159 | public function validate(ValidationResult $validationResult) |
||
176 | |||
177 | /** |
||
178 | * Create the user in LDAP, provided this configuration is enabled |
||
179 | * and a username was passed to a new Member record. |
||
180 | */ |
||
181 | View Code Duplication | public function onBeforeWrite() |
|
198 | |||
199 | public function onAfterWrite() |
||
215 | |||
216 | View Code Duplication | public function onAfterDelete() |
|
233 | |||
234 | /** |
||
235 | * Write DataObject without triggering this extension's hooks. |
||
236 | * |
||
237 | * @throws Exception |
||
238 | */ |
||
239 | public function writeWithoutSync() |
||
248 | |||
249 | /** |
||
250 | * Update the local data with LDAP, and ensure local membership is also set in |
||
251 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
252 | */ |
||
253 | public function sync() |
||
265 | |||
266 | /** |
||
267 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
268 | * this will update the Member record from AD data. |
||
269 | */ |
||
270 | public function memberLoggedIn() |
||
278 | } |
||
279 |
This check marks private properties in classes that are never used. Those properties can be removed.