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 domain of the directory. e.g. "mydomain.com" |
||
| 67 | * LDAP_NEW_USERS_DN - where to place users in the directory. e.g. "OU=Users,DC=mydomain,DC=com" |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | * @config |
||
| 71 | */ |
||
| 72 | private static $create_users_in_ldap = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * If enabled, deleting Member records mapped to LDAP deletes the LDAP user. |
||
| 76 | * |
||
| 77 | * This requires setting write permissions on the user configured in the LDAP |
||
| 78 | * credentials, which is why this is disabled by default. |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | * @config |
||
| 82 | */ |
||
| 83 | private static $delete_users_in_ldap = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param FieldList $fields |
||
| 87 | */ |
||
| 88 | public function updateCMSFields(FieldList $fields) |
||
| 142 | |||
| 143 | public function validate(ValidationResult $validationResult) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Create the user in LDAP, provided this configuration is enabled |
||
| 167 | * and a username was passed to a new Member record. |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function onBeforeWrite() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Update the local data with LDAP, and ensure local membership is also set in |
||
| 185 | * LDAP too. This writes into LDAP, provided that feature is enabled. |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function onAfterWrite() |
|
| 201 | |||
| 202 | View Code Duplication | public function onAfterDelete() { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Triggered by {@link Member::logIn()} when successfully logged in, |
||
| 217 | * this will update the Member record from AD data. |
||
| 218 | */ |
||
| 219 | public function memberLoggedIn() |
||
| 225 | } |
||
| 226 |
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.