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 with a Username field have the user created in LDAP |
||
| 56 | * on write. |
||
| 57 | * |
||
| 58 | * This requires setting write permissions on the user configured in the LDAP |
||
| 59 | * credentials, which is why this is disabled by default. |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | * @config |
||
| 63 | */ |
||
| 64 | private static $create_users_in_ldap = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
| 68 | * |
||
| 69 | * This requires setting write permissions on the user configured in the LDAP |
||
| 70 | * credentials, which is why this is disabled by default. |
||
| 71 | * |
||
| 72 | * @var bool |
||
| 73 | * @config |
||
| 74 | */ |
||
| 75 | private static $delete_users_in_ldap = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param FieldList $fields |
||
| 79 | */ |
||
| 80 | public function updateCMSFields(FieldList $fields) |
||
| 130 | |||
| 131 | public function validate(ValidationResult $validationResult) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Generate a username for the user based on their name and/or email |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function generateLDAPUsername() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Create the user in LDAP, provided this configuration is enabled |
||
| 189 | * and a username was passed to a new Member record. |
||
| 190 | */ |
||
| 191 | public function onBeforeWrite() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Update the local data with LDAP, and ensure local membership is also set in |
||
| 212 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function onAfterWrite() |
|
| 228 | |||
| 229 | View Code Duplication | public function onAfterDelete() { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
| 244 | * this will update the Member record from AD data. |
||
| 245 | */ |
||
| 246 | public function memberLoggedIn() |
||
| 252 | } |
||
| 253 |
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.