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 | * @var bool |
||
| 51 | * @config |
||
| 52 | */ |
||
| 53 | private static $update_ldap_from_local = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If enabled, Member records with a Username field have the user created in LDAP |
||
| 57 | * 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 | * Note that some constants must be configured in your environment file: |
||
| 63 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | * @config |
||
| 67 | */ |
||
| 68 | private static $create_users_in_ldap = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
| 72 | * |
||
| 73 | * This requires setting write permissions on the user configured in the LDAP |
||
| 74 | * credentials, which is why this is disabled by default. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | * @config |
||
| 78 | */ |
||
| 79 | private static $delete_users_in_ldap = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param FieldList $fields |
||
| 83 | */ |
||
| 84 | public function updateCMSFields(FieldList $fields) |
||
| 138 | |||
| 139 | public function validate(ValidationResult $validationResult) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Create the user in LDAP, provided this configuration is enabled |
||
| 163 | * and a username was passed to a new Member record. |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function onBeforeWrite() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Update the local data with LDAP, and ensure local membership is also set in |
||
| 181 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function onAfterWrite() |
|
| 197 | |||
| 198 | View Code Duplication | public function onAfterDelete() { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
| 213 | * this will update the Member record from AD data. |
||
| 214 | */ |
||
| 215 | public function memberLoggedIn() |
||
| 221 | } |
||
| 222 |
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.