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 | '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 = array( |
||
| 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 | * Note that some constants must be configured in your environment file: |
||
| 62 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
| 63 | * |
||
| 64 | * @var bool |
||
| 65 | * @config |
||
| 66 | */ |
||
| 67 | private static $create_users_in_ldap = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
| 71 | * |
||
| 72 | * This requires setting write permissions on the user configured in the LDAP |
||
| 73 | * credentials, which is why this is disabled by default. |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | * @config |
||
| 77 | */ |
||
| 78 | private static $delete_users_in_ldap = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param FieldList $fields |
||
| 82 | */ |
||
| 83 | public function updateCMSFields(FieldList $fields) |
||
| 133 | |||
| 134 | public function validate(ValidationResult $validationResult) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Create the user in LDAP, provided this configuration is enabled |
||
| 154 | * and a username was passed to a new Member record. |
||
| 155 | */ |
||
| 156 | public function onBeforeWrite() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Update the local data with LDAP, and ensure local membership is also set in |
||
| 173 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function onAfterWrite() |
|
| 189 | |||
| 190 | View Code Duplication | public function onAfterDelete() { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
| 205 | * this will update the Member record from AD data. |
||
| 206 | */ |
||
| 207 | public function memberLoggedIn() |
||
| 213 | } |
||
| 214 |
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.