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, and synchronise |
||
45 | * membership to groups mapped to LDAP. |
||
46 | * |
||
47 | * Keep in mind this will currently NOT trigger if there are no |
||
48 | * field changes due to onAfterWrite in framework not being called |
||
49 | * when there are no changes. |
||
50 | * |
||
51 | * This requires setting write permissions on the user configured in the LDAP |
||
52 | * credentials, which is why this is disabled by default. |
||
53 | * |
||
54 | * @var bool |
||
55 | * @config |
||
56 | */ |
||
57 | private static $update_ldap_from_local = false; |
||
58 | |||
59 | /** |
||
60 | * If enabled, Member records with a Username field have the user created in LDAP |
||
61 | * on write. |
||
62 | * |
||
63 | * This requires setting write permissions on the user configured in the LDAP |
||
64 | * credentials, which is why this is disabled by default. |
||
65 | * |
||
66 | * @var bool |
||
67 | * @config |
||
68 | */ |
||
69 | private static $create_users_in_ldap = false; |
||
70 | |||
71 | /** |
||
72 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
73 | * |
||
74 | * This requires setting write permissions on the user configured in the LDAP |
||
75 | * credentials, which is why this is disabled by default. |
||
76 | * |
||
77 | * @var bool |
||
78 | * @config |
||
79 | */ |
||
80 | private static $delete_users_in_ldap = false; |
||
81 | |||
82 | /** |
||
83 | * @param FieldList $fields |
||
84 | */ |
||
85 | public function updateCMSFields(FieldList $fields) |
||
135 | |||
136 | public function validate(ValidationResult $validationResult) |
||
153 | |||
154 | /** |
||
155 | * Create the user in LDAP, provided this configuration is enabled |
||
156 | * and a username was passed to a new Member record. |
||
157 | */ |
||
158 | View Code Duplication | public function onBeforeWrite() |
|
176 | |||
177 | public function onAfterWrite() |
||
188 | |||
189 | View Code Duplication | public function onAfterDelete() { |
|
205 | |||
206 | /** |
||
207 | * Write DataObject without triggering this extension's hooks. |
||
208 | * |
||
209 | * @throws Exception |
||
210 | */ |
||
211 | public function writeWithoutSync() { |
||
221 | |||
222 | /** |
||
223 | * Update the local data with LDAP, and ensure local membership is also set in |
||
224 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
225 | */ |
||
226 | public function sync() { |
||
237 | |||
238 | /** |
||
239 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
240 | * this will update the Member record from AD data. |
||
241 | */ |
||
242 | public function memberLoggedIn() |
||
248 | } |
||
249 |
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.