| Conditions | 39 |
| Paths | 992 |
| Total Lines | 212 |
| Code Lines | 140 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 57 | public function __construct(array $info, array $config) |
||
| 58 | { |
||
| 59 | // Call the parent constructor first, as required by the interface |
||
| 60 | parent::__construct($info, $config); |
||
| 61 | |||
| 62 | // Check databases configuration that all required parameters are present |
||
| 63 | if (array_key_exists('databases', $config)) { |
||
| 64 | if (!is_array($config['databases'])) { |
||
| 65 | throw new Exception('Required parameter \'databases\' for authentication source ' . |
||
| 66 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 67 | var_export($config['databases'], true)); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (empty($config['databases'])) { |
||
| 71 | throw new Exception('Required parameter \'databases\' for authentication source ' . |
||
| 72 | $this->authId . ' was provided but is an empty array.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | foreach ($config['databases'] as $dbname => $dbConfig) { |
||
| 76 | if (!is_array($dbConfig)) { |
||
| 77 | throw new Exception('Each entry in the ' . |
||
| 78 | $dbname . ' \'databases\' parameter for authentication source ' . |
||
| 79 | $this->authId . ' is expected to be an array. Instead it was: ' . |
||
| 80 | var_export($dbConfig, true)); |
||
| 81 | } |
||
| 82 | foreach (['dsn', 'username', 'password'] as $param) { |
||
| 83 | if (!array_key_exists($param, $dbConfig)) { |
||
| 84 | throw new Exception('Database ' . |
||
| 85 | $dbname . ' is missing required attribute \'' . |
||
| 86 | $param . '\' for authentication source ' . |
||
| 87 | $this->authId); |
||
| 88 | } |
||
| 89 | if (!is_string($dbConfig[$param])) { |
||
| 90 | throw new Exception('Expected parameter \'' . $param . |
||
| 91 | '\' for authentication source ' . $this->authId . |
||
| 92 | ' to be a string. Instead it was: ' . |
||
| 93 | var_export($config[$param], true)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if (array_key_exists('options', $dbConfig) && !is_array($dbConfig['options'])) { |
||
| 98 | throw new Exception('Optional parameter \'options\' for authentication source ' . |
||
| 99 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 100 | var_export($dbConfig['options'], true)); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->databases[$dbname] = [ |
||
| 104 | '_pdo' => null, // Will hold the PDO connection when connected |
||
| 105 | 'dsn' => $dbConfig['dsn'], |
||
| 106 | 'username' => $dbConfig['username'], |
||
| 107 | 'password' => $dbConfig['password'], |
||
| 108 | 'options' => $dbConfig['options'] ?? [], |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | } else { |
||
| 112 | throw new Exception('Missing required attribute \'databases\' for authentication source ' . $this->authId); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Check auth_queries configuration that all required parameters are present |
||
| 116 | if (array_key_exists('auth_queries', $config)) { |
||
| 117 | if (!is_array($config['auth_queries'])) { |
||
| 118 | throw new Exception('Required parameter \'auth_queries\' for authentication source ' . |
||
| 119 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 120 | var_export($config['auth_queries'], true)); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (empty($config['auth_queries'])) { |
||
| 124 | throw new Exception('Required parameter \'auth_queries\' for authentication source ' . |
||
| 125 | $this->authId . ' was provided but is an empty array.'); |
||
| 126 | } |
||
| 127 | |||
| 128 | foreach ($config['auth_queries'] as $authQueryName => $authQueryConfig) { |
||
| 129 | if (!is_array($authQueryConfig)) { |
||
| 130 | throw new Exception('Each entry in the ' . |
||
| 131 | $authQueryName . ' \'auth_queries\' parameter for authentication source ' . |
||
| 132 | $this->authId . ' is expected to be an array. Instead it was: ' . |
||
| 133 | var_export($authQueryConfig, true)); |
||
| 134 | } |
||
| 135 | |||
| 136 | foreach (['database', 'query'] as $param) { |
||
| 137 | if (!array_key_exists($param, $authQueryConfig)) { |
||
| 138 | throw new Exception('Auth query ' . |
||
| 139 | $authQueryName . ' is missing required attribute \'' . |
||
| 140 | $param . '\' for authentication source ' . |
||
| 141 | $this->authId); |
||
| 142 | } |
||
| 143 | if (!is_string($authQueryConfig[$param])) { |
||
| 144 | throw new Exception('Expected parameter \'' . $param . |
||
| 145 | '\' for authentication source \'' . $this->authId . '\'' . |
||
| 146 | ' to be a string. Instead it was: ' . |
||
| 147 | var_export($authQueryConfig[$param], true)); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if (!array_key_exists($authQueryConfig['database'], $this->databases)) { |
||
| 152 | throw new Exception('Auth query ' . |
||
| 153 | $authQueryName . ' references unknown database \'' . |
||
| 154 | $authQueryConfig['database'] . '\' for authentication source ' . |
||
| 155 | $this->authId); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->authQueries[$authQueryName] = [ |
||
| 159 | // Will be set to true for the query that successfully authenticated the user |
||
| 160 | '_winning_auth_query' => false, |
||
| 161 | |||
| 162 | // Will hold the value of the attribute named by 'extract_userid_from' |
||
| 163 | // if specified and authentication succeeds |
||
| 164 | '_extracted_userid' => null, |
||
| 165 | |||
| 166 | 'database' => $authQueryConfig['database'], |
||
| 167 | 'query' => $authQueryConfig['query'], |
||
| 168 | ]; |
||
| 169 | |||
| 170 | if (array_key_exists('username_regex', $authQueryConfig)) { |
||
| 171 | if (!is_string($authQueryConfig['username_regex'])) { |
||
| 172 | throw new Exception('Optional parameter \'username_regex\' for authentication source ' . |
||
| 173 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 174 | var_export($authQueryConfig['username_regex'], true)); |
||
| 175 | } |
||
| 176 | $this->authQueries[$authQueryName]['username_regex'] = $authQueryConfig['username_regex']; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (array_key_exists('extract_userid_from', $authQueryConfig)) { |
||
| 180 | if (!is_string($authQueryConfig['extract_userid_from'])) { |
||
| 181 | throw new Exception('Optional parameter \'extract_userid_from\' for authentication source ' . |
||
| 182 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 183 | var_export($authQueryConfig['extract_userid_from'], true)); |
||
| 184 | } |
||
| 185 | $this->authQueries[$authQueryName]['extract_userid_from'] = $authQueryConfig['extract_userid_from']; |
||
| 186 | } |
||
| 187 | |||
| 188 | if (array_key_exists('password_verify_hash_column', $authQueryConfig)) { |
||
| 189 | if (!is_string($authQueryConfig['password_verify_hash_column'])) { |
||
| 190 | throw new Exception( |
||
| 191 | 'Optional parameter \'password_verify_hash_column\' for authentication source ' . |
||
| 192 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 193 | var_export($authQueryConfig['password_verify_hash_column'], true), |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | $this->authQueries[$authQueryName]['password_verify_hash_column'] = |
||
| 197 | $authQueryConfig['password_verify_hash_column']; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | throw new Exception( |
||
| 202 | 'Missing required attribute \'auth_queries\' for authentication source ' . |
||
| 203 | $this->authId, |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | // attr_queries is optional, but if specified, we need to check the parameters |
||
| 208 | if (array_key_exists('attr_queries', $config)) { |
||
| 209 | if (!is_array($config['attr_queries'])) { |
||
| 210 | throw new Exception('Optional parameter \'attr_queries\' for authentication source ' . |
||
| 211 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 212 | var_export($config['attr_queries'], true)); |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach ($config['attr_queries'] as $attrQueryConfig) { |
||
| 216 | if (!is_array($attrQueryConfig)) { |
||
| 217 | throw new Exception('\'attr_queries\' parameter for authentication source ' . |
||
| 218 | $this->authId . ' is expected to be an array. Instead it was: ' . |
||
| 219 | var_export($attrQueryConfig, true)); |
||
| 220 | } |
||
| 221 | |||
| 222 | foreach (['database', 'query'] as $param) { |
||
| 223 | if (!array_key_exists($param, $attrQueryConfig)) { |
||
| 224 | throw new Exception('Attribute query is missing required attribute \'' . |
||
| 225 | $param . '\' for authentication source ' . |
||
| 226 | $this->authId); |
||
| 227 | } |
||
| 228 | if (!is_string($attrQueryConfig[$param])) { |
||
| 229 | throw new Exception('Expected parameter \'' . $param . |
||
| 230 | '\' for authentication source \'' . $this->authId . '\'' . |
||
| 231 | ' to be a string. Instead it was: ' . |
||
| 232 | var_export($attrQueryConfig[$param], true)); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | $currentAttributeQuery = [ |
||
| 237 | 'database' => $attrQueryConfig['database'], |
||
| 238 | 'query' => $attrQueryConfig['query'], |
||
| 239 | ]; |
||
| 240 | |||
| 241 | if (!array_key_exists($attrQueryConfig['database'], $this->databases)) { |
||
| 242 | throw new Exception('Attribute query references unknown database \'' . |
||
| 243 | $attrQueryConfig['database'] . '\' for authentication source ' . |
||
| 244 | $this->authId); |
||
| 245 | } |
||
| 246 | |||
| 247 | if (array_key_exists('only_for_auth', $attrQueryConfig)) { |
||
| 248 | if (!is_array($attrQueryConfig['only_for_auth'])) { |
||
| 249 | throw new Exception('Optional parameter \'only_for_auth\' for authentication source ' . |
||
| 250 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 251 | var_export($attrQueryConfig['only_for_auth'], true)); |
||
| 252 | } |
||
| 253 | foreach ($attrQueryConfig['only_for_auth'] as $authQueryName) { |
||
| 254 | if (!is_string($authQueryName)) { |
||
| 255 | throw new Exception('Each entry in the \'only_for_auth\' array for authentication source ' . |
||
| 256 | $this->authId . ' is expected to be a string. Instead it was: ' . |
||
| 257 | var_export($authQueryName, true)); |
||
| 258 | } |
||
| 259 | if (!array_key_exists($authQueryName, $this->authQueries)) { |
||
| 260 | throw new Exception('Attribute query references unknown auth query \'' . |
||
| 261 | $authQueryName . '\' for authentication source ' . |
||
| 262 | $this->authId); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | $currentAttributeQuery['only_for_auth'] = $attrQueryConfig['only_for_auth']; |
||
| 266 | } |
||
| 267 | |||
| 268 | $this->attributesQueries[] = $currentAttributeQuery; |
||
| 269 | } |
||
| 623 |