| Total Complexity | 58 |
| Total Lines | 307 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TUserManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TUserManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 73 | class TUserManager extends \Prado\TModule implements IUserManager |
||
| 74 | { |
||
| 75 | /** |
||
| 76 | * extension name to the user file |
||
| 77 | */ |
||
| 78 | const USER_FILE_EXT = '.xml'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array list of users managed by this module |
||
| 82 | */ |
||
| 83 | private $_users = []; |
||
| 84 | /** |
||
| 85 | * @var array list of roles managed by this module |
||
| 86 | */ |
||
| 87 | private $_roles = []; |
||
| 88 | /** |
||
| 89 | * @var string guest name |
||
| 90 | */ |
||
| 91 | private $_guestName = 'Guest'; |
||
| 92 | /** |
||
| 93 | * @var TUserManagerPasswordMode password mode |
||
| 94 | */ |
||
| 95 | private $_passwordMode = TUserManagerPasswordMode::MD5; |
||
| 96 | /** |
||
| 97 | * @var boolean whether the module has been initialized |
||
| 98 | */ |
||
| 99 | private $_initialized = false; |
||
| 100 | /** |
||
| 101 | * @var string user/role information file |
||
| 102 | */ |
||
| 103 | private $_userFile; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Initializes the module. |
||
|
|
|||
| 107 | * This method is required by IModule and is invoked by application. |
||
| 108 | * It loads user/role information from the module configuration. |
||
| 109 | * @param mixed module configuration |
||
| 110 | */ |
||
| 111 | public function init($config) |
||
| 112 | { |
||
| 113 | $this->loadUserData($config); |
||
| 114 | if($this->_userFile !== null) |
||
| 115 | { |
||
| 116 | if($this->getApplication()->getConfigurationType() == TApplication::CONFIG_TYPE_PHP) |
||
| 117 | { |
||
| 118 | $userFile = include $this->_userFile; |
||
| 119 | $this->loadUserDataFromPhp($userFile); |
||
| 120 | } |
||
| 121 | else |
||
| 122 | { |
||
| 123 | $dom = new TXmlDocument; |
||
| 124 | $dom->loadFromFile($this->_userFile); |
||
| 125 | $this->loadUserDataFromXml($dom); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | $this->_initialized = true; |
||
| 129 | } |
||
| 130 | |||
| 131 | /* |
||
| 132 | * Loads user/role information |
||
| 133 | * @param mixed the variable containing the user information |
||
| 134 | */ |
||
| 135 | private function loadUserData($config) |
||
| 136 | { |
||
| 137 | if($this->getApplication()->getConfigurationType() == TApplication::CONFIG_TYPE_PHP) |
||
| 138 | $this->loadUserDataFromPhp($config); |
||
| 139 | else |
||
| 140 | $this->loadUserDataFromXml($config); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Loads user/role information from an php array. |
||
| 145 | * @param array the array containing the user information |
||
| 146 | */ |
||
| 147 | private function loadUserDataFromPhp($config) |
||
| 148 | { |
||
| 149 | if(isset($config['users']) && is_array($config['users'])) |
||
| 150 | { |
||
| 151 | foreach($config['users'] as $user) |
||
| 152 | { |
||
| 153 | $name = trim(strtolower(isset($user['name'])?$user['name']:'')); |
||
| 154 | $password = isset($user['password'])?$user['password']:''; |
||
| 155 | $this->_users[$name] = $password; |
||
| 156 | $roles = isset($user['roles'])?$user['roles']:''; |
||
| 157 | if($roles !== '') |
||
| 158 | { |
||
| 159 | foreach(explode(',', $roles) as $role) |
||
| 160 | { |
||
| 161 | if(($role = trim($role)) !== '') |
||
| 162 | $this->_roles[$name][] = $role; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | if(isset($config['roles']) && is_array($config['roles'])) |
||
| 168 | { |
||
| 169 | foreach($config['roles'] as $role) |
||
| 170 | { |
||
| 171 | $name = isset($role['name'])?$role['name']:''; |
||
| 172 | $users = isset($role['users'])?$role['users']:''; |
||
| 173 | foreach(explode(',', $users) as $user) |
||
| 174 | { |
||
| 175 | if(($user = trim($user)) !== '') |
||
| 176 | $this->_roles[strtolower($user)][] = $name; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Loads user/role information from an XML node. |
||
| 184 | * @param TXmlElement the XML node containing the user information |
||
| 185 | */ |
||
| 186 | private function loadUserDataFromXml($xmlNode) |
||
| 187 | { |
||
| 188 | foreach($xmlNode->getElementsByTagName('user') as $node) |
||
| 189 | { |
||
| 190 | $name = trim(strtolower($node->getAttribute('name'))); |
||
| 191 | $this->_users[$name] = $node->getAttribute('password'); |
||
| 192 | if(($roles = trim($node->getAttribute('roles'))) !== '') |
||
| 193 | { |
||
| 194 | foreach(explode(',', $roles) as $role) |
||
| 195 | { |
||
| 196 | if(($role = trim($role)) !== '') |
||
| 197 | $this->_roles[$name][] = $role; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | foreach($xmlNode->getElementsByTagName('role') as $node) |
||
| 202 | { |
||
| 203 | foreach(explode(',', $node->getAttribute('users')) as $user) |
||
| 204 | { |
||
| 205 | if(($user = trim($user)) !== '') |
||
| 206 | $this->_roles[strtolower($user)][] = $node->getAttribute('name'); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns an array of all users. |
||
| 213 | * Each array element represents a single user. |
||
| 214 | * The array key is the username in lower case, and the array value is the |
||
| 215 | * corresponding user password. |
||
| 216 | * @return array list of users |
||
| 217 | */ |
||
| 218 | public function getUsers() |
||
| 219 | { |
||
| 220 | return $this->_users; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns an array of user role information. |
||
| 225 | * Each array element represents the roles for a single user. |
||
| 226 | * The array key is the username in lower case, and the array value is |
||
| 227 | * the roles (represented as an array) that the user is in. |
||
| 228 | * @return array list of user role information |
||
| 229 | */ |
||
| 230 | public function getRoles() |
||
| 231 | { |
||
| 232 | return $this->_roles; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string the full path to the file storing user/role information |
||
| 237 | */ |
||
| 238 | public function getUserFile() |
||
| 239 | { |
||
| 240 | return $this->_userFile; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string user/role data file path (in namespace form). The file format is XML |
||
| 245 | * whose content is similar to that user/role block in application configuration. |
||
| 246 | * @throws TInvalidOperationException if the module is already initialized |
||
| 247 | * @throws TConfigurationException if the file is not in proper namespace format |
||
| 248 | */ |
||
| 249 | public function setUserFile($value) |
||
| 250 | { |
||
| 251 | if($this->_initialized) |
||
| 252 | throw new TInvalidOperationException('usermanager_userfile_unchangeable'); |
||
| 253 | elseif(($this->_userFile = Prado::getPathOfNamespace($value, self::USER_FILE_EXT)) === null || !is_file($this->_userFile)) |
||
| 254 | throw new TConfigurationException('usermanager_userfile_invalid', $value); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string guest name, defaults to 'Guest' |
||
| 259 | */ |
||
| 260 | public function getGuestName() |
||
| 261 | { |
||
| 262 | return $this->_guestName; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string name to be used for guest users. |
||
| 267 | */ |
||
| 268 | public function setGuestName($value) |
||
| 269 | { |
||
| 270 | $this->_guestName = $value; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return TUserManagerPasswordMode how password is stored, clear text, or MD5 or SHA1 hashed. Default to TUserManagerPasswordMode::MD5. |
||
| 275 | */ |
||
| 276 | public function getPasswordMode() |
||
| 277 | { |
||
| 278 | return $this->_passwordMode; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param TUserManagerPasswordMode how password is stored, clear text, or MD5 or SHA1 hashed. |
||
| 283 | */ |
||
| 284 | public function setPasswordMode($value) |
||
| 285 | { |
||
| 286 | $this->_passwordMode = TPropertyValue::ensureEnum($value, 'Prado\\Security\\TUserManagerPasswordMode'); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Validates if the username and password are correct. |
||
| 291 | * @param string user name |
||
| 292 | * @param string password |
||
| 293 | * @return boolean true if validation is successful, false otherwise. |
||
| 294 | */ |
||
| 295 | public function validateUser($username, $password) |
||
| 296 | { |
||
| 297 | if($this->_passwordMode === TUserManagerPasswordMode::MD5) |
||
| 298 | $password = md5($password); |
||
| 299 | elseif($this->_passwordMode === TUserManagerPasswordMode::SHA1) |
||
| 300 | $password = sha1($password); |
||
| 301 | $username = strtolower($username); |
||
| 302 | return (isset($this->_users[$username]) && $this->_users[$username] === $password); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a user instance given the user name. |
||
| 307 | * @param string user name, null if it is a guest. |
||
| 308 | * @return TUser the user instance, null if the specified username is not in the user database. |
||
| 309 | */ |
||
| 310 | public function getUser($username = null) |
||
| 311 | { |
||
| 312 | if($username === null) |
||
| 313 | { |
||
| 314 | $user = new TUser($this); |
||
| 315 | $user->setIsGuest(true); |
||
| 316 | return $user; |
||
| 317 | } |
||
| 318 | else |
||
| 319 | { |
||
| 320 | $username = strtolower($username); |
||
| 321 | if(isset($this->_users[$username])) |
||
| 322 | { |
||
| 323 | $user = new TUser($this); |
||
| 324 | $user->setName($username); |
||
| 325 | $user->setIsGuest(false); |
||
| 326 | if(isset($this->_roles[$username])) |
||
| 327 | $user->setRoles($this->_roles[$username]); |
||
| 328 | return $user; |
||
| 329 | } |
||
| 330 | else |
||
| 331 | return null; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns a user instance according to auth data stored in a cookie. |
||
| 337 | * @param THttpCookie the cookie storing user authentication information |
||
| 338 | * @return TUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data. |
||
| 339 | * @since 3.1.1 |
||
| 340 | */ |
||
| 341 | public function getUserFromCookie($cookie) |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Saves user auth data into a cookie. |
||
| 358 | * @param THttpCookie the cookie to receive the user auth data. |
||
| 359 | * @since 3.1.1 |
||
| 360 | */ |
||
| 361 | public function saveUserToCookie($cookie) |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Sets a user as a guest. |
||
| 374 | * User name is changed as guest name, and roles are emptied. |
||
| 375 | * @param TUser the user to be changed to a guest. |
||
| 376 | */ |
||
| 377 | public function switchToGuest($user) |
||
| 380 | } |
||
| 381 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths