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. |
||
58 | * |
||
59 | * This requires setting write permissions on the user configured in the LDAP |
||
60 | * credentials, which is why this is disabled by default. |
||
61 | * |
||
62 | * @var bool |
||
63 | * @config |
||
64 | */ |
||
65 | private static $update_ldap_from_local = false; |
||
66 | |||
67 | /** |
||
68 | * If enabled, Member records with a Username field have the user created in LDAP |
||
69 | * on write. |
||
70 | * |
||
71 | * This requires setting write permissions on the user configured in the LDAP |
||
72 | * credentials, which is why this is disabled by default. |
||
73 | * |
||
74 | * @var bool |
||
75 | * @config |
||
76 | */ |
||
77 | private static $create_users_in_ldap = false; |
||
78 | |||
79 | /** |
||
80 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
81 | * |
||
82 | * This requires setting write permissions on the user configured in the LDAP |
||
83 | * credentials, which is why this is disabled by default. |
||
84 | * |
||
85 | * @var bool |
||
86 | * @config |
||
87 | */ |
||
88 | private static $delete_users_in_ldap = false; |
||
89 | |||
90 | /** |
||
91 | * @param FieldList $fields |
||
92 | */ |
||
93 | public function updateCMSFields(FieldList $fields) |
||
149 | |||
150 | /** |
||
151 | * @param ValidationResult |
||
152 | * @throws ValidationException |
||
153 | */ |
||
154 | public function validate(ValidationResult $validationResult) |
||
171 | |||
172 | /** |
||
173 | * Create the user in LDAP, provided this configuration is enabled |
||
174 | * and a username was passed to a new Member record. |
||
175 | */ |
||
176 | public function onBeforeWrite() |
||
189 | |||
190 | /** |
||
191 | * Update the local data with LDAP, and ensure local membership is also set in |
||
192 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
193 | */ |
||
194 | View Code Duplication | public function onAfterWrite() |
|
207 | |||
208 | View Code Duplication | public function onAfterDelete() |
|
220 | |||
221 | /** |
||
222 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
223 | * this will update the Member record from AD data. |
||
224 | */ |
||
225 | public function memberLoggedIn() |
||
233 | } |
||
234 |
This check marks private properties in classes that are never used. Those properties can be removed.