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 = 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 | 'givenname' => 'FirstName', |
||
30 | 'samaccountname' => 'Username', |
||
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, LDAP managed Member records (IsImportedFromLDAP flag) |
||
45 | * have their data written back to LDAP on write. |
||
46 | * |
||
47 | * This requires setting write permissions on the user configured in the LDAP |
||
48 | * credentials, which is why this is disabled by default. |
||
49 | * |
||
50 | * Note that some constants must be configured in your environment file: |
||
51 | * LDAP_DOMAIN - the base DN of the directory. e.g. "DN=mydomain,DC=com" |
||
52 | * |
||
53 | * @var bool |
||
54 | * @config |
||
55 | */ |
||
56 | private static $update_ldap_from_local = false; |
||
57 | |||
58 | /** |
||
59 | * If enabled, Member records with a Username field have the user created in LDAP |
||
60 | * on write. |
||
61 | * |
||
62 | * This requires setting write permissions on the user configured in the LDAP |
||
63 | * credentials, which is why this is disabled by default. |
||
64 | * |
||
65 | * Note that some constants must be configured in your environment file: |
||
66 | * LDAP_DOMAIN - the base DN of the directory. e.g. "DN=mydomain,DC=com" |
||
67 | * LDAP_NEW_USERS_OBJECT_CATEGORY - the type of object. e.g. "CN=Person,CN=Schema,DC=mydomain,DC=com" |
||
68 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
69 | * |
||
70 | * @var bool |
||
71 | * @config |
||
72 | */ |
||
73 | private static $create_users_in_ldap = false; |
||
74 | |||
75 | /** |
||
76 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
77 | * |
||
78 | * This requires setting write permissions on the user configured in the LDAP |
||
79 | * credentials, which is why this is disabled by default. |
||
80 | * |
||
81 | * @var bool |
||
82 | * @config |
||
83 | */ |
||
84 | private static $delete_users_in_ldap = false; |
||
85 | |||
86 | /** |
||
87 | * @param FieldList $fields |
||
88 | */ |
||
89 | public function updateCMSFields(FieldList $fields) |
||
143 | |||
144 | public function validate(ValidationResult $validationResult) |
||
165 | |||
166 | /** |
||
167 | * Create the user in LDAP, provided this configuration is enabled |
||
168 | * and a username was passed to a new Member record. |
||
169 | */ |
||
170 | View Code Duplication | public function onBeforeWrite() |
|
183 | |||
184 | /** |
||
185 | * Update the local data with LDAP, and ensure local membership is also set in |
||
186 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
187 | */ |
||
188 | View Code Duplication | public function onAfterWrite() |
|
202 | |||
203 | View Code Duplication | public function onAfterDelete() { |
|
215 | |||
216 | /** |
||
217 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
218 | * this will update the Member record from AD data. |
||
219 | */ |
||
220 | public function memberLoggedIn() |
||
226 | } |
||
227 |
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.