| Conditions | 39 |
| Paths | 992 |
| Total Lines | 277 |
| Code Lines | 181 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 74 | public function __construct(array $info, array $config) |
||
| 75 | { |
||
| 76 | // Call the parent constructor first, as required by the interface |
||
| 77 | parent::__construct($info, $config); |
||
| 78 | |||
| 79 | // Check databases configuration that all required parameters are present |
||
| 80 | if (!array_key_exists('databases', $config)) { |
||
| 81 | throw new Exception(sprintf( |
||
| 82 | 'Missing required attribute \'databases\' for authentication source \'%s\'', |
||
| 83 | $this->authId, |
||
| 84 | )); |
||
| 85 | } else { |
||
| 86 | if (!is_array($config['databases'])) { |
||
| 87 | throw new Exception(sprintf( |
||
| 88 | 'Required parameter \'databases\' for authentication source \'%s\'' |
||
| 89 | . ' was provided and is expected to be an array. Instead it was: %s', |
||
| 90 | $this->authId, |
||
| 91 | var_export($config['databases'], true), |
||
| 92 | )); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (empty($config['databases'])) { |
||
| 96 | throw new Exception(sprintf( |
||
| 97 | 'Required parameter \'databases\' for authentication source \'%s\'' |
||
| 98 | . ' was provided but is an empty array.', |
||
| 99 | $this->authId, |
||
| 100 | )); |
||
| 101 | } |
||
| 102 | |||
| 103 | foreach ($config['databases'] as $dbname => $dbConfig) { |
||
| 104 | if (!is_array($dbConfig)) { |
||
| 105 | throw new Exception(sprintf( |
||
| 106 | 'Each entry in the %s \'databases\' parameter for authentication source \'%s\'' |
||
| 107 | . ' is expected to be an array. Instead it was: %s', |
||
| 108 | $dbname, |
||
| 109 | $this->authId, |
||
| 110 | var_export($dbConfig, true), |
||
| 111 | )); |
||
| 112 | } |
||
| 113 | foreach (['dsn', 'username', 'password'] as $param) { |
||
| 114 | if (!array_key_exists($param, $dbConfig)) { |
||
| 115 | throw new Exception(sprintf( |
||
| 116 | 'Database %s is missing required attribute \'%s\' for authentication source \'%s\'', |
||
| 117 | $dbname, |
||
| 118 | $param, |
||
| 119 | $this->authId, |
||
| 120 | )); |
||
| 121 | } |
||
| 122 | if (!is_string($dbConfig[$param])) { |
||
| 123 | throw new Exception(sprintf( |
||
| 124 | 'Expected parameter \'%s\' for authentication source %s to be a string. ' |
||
| 125 | . 'Instead it was: %s', |
||
| 126 | $dbname, |
||
| 127 | $this->authId, |
||
| 128 | var_export($config[$param], true), |
||
| 129 | )); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | if (array_key_exists('options', $dbConfig) && !is_array($dbConfig['options'])) { |
||
| 134 | throw new Exception(sprintf( |
||
| 135 | 'Optional parameter \'options\' for authentication source \'%s\'' |
||
| 136 | . ' was provided and is expected to be an array. Instead it was: %s', |
||
| 137 | $this->authId, |
||
| 138 | var_export($dbConfig['options'], true), |
||
| 139 | )); |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->databases[$dbname] = [ |
||
| 143 | '_pdo' => null, // Will hold the PDO connection when connected |
||
| 144 | 'dsn' => $dbConfig['dsn'], |
||
| 145 | 'username' => $dbConfig['username'], |
||
| 146 | 'password' => $dbConfig['password'], |
||
| 147 | 'options' => $dbConfig['options'] ?? [], |
||
| 148 | ]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // Check auth_queries configuration that all required parameters are present |
||
| 153 | if (!array_key_exists('auth_queries', $config)) { |
||
| 154 | throw new Exception(sprintf( |
||
| 155 | 'Missing required attribute \'auth_queries\' for authentication source \'%s\'', |
||
| 156 | $this->authId, |
||
| 157 | )); |
||
| 158 | } else { |
||
| 159 | if (!is_array($config['auth_queries'])) { |
||
| 160 | throw new Exception(sprintf( |
||
| 161 | 'Required parameter \'auth_queries\' for authentication source \'%s\'' |
||
| 162 | . ' was provided and is expected to be an array. Instead it was: %s', |
||
| 163 | $this->authId, |
||
| 164 | var_export($config['auth_queries'], true), |
||
| 165 | )); |
||
| 166 | } |
||
| 167 | |||
| 168 | if (empty($config['auth_queries'])) { |
||
| 169 | throw new Exception(sprintf( |
||
| 170 | 'Required parameter \'auth_queries\' for authentication source \'%s\'' |
||
| 171 | . ' was provided but is an empty array.', |
||
| 172 | $this->authId, |
||
| 173 | )); |
||
| 174 | } |
||
| 175 | |||
| 176 | foreach ($config['auth_queries'] as $authQueryName => $authQueryConfig) { |
||
| 177 | if (!is_array($authQueryConfig)) { |
||
| 178 | throw new Exception(sprintf( |
||
| 179 | 'Each entry in the %s \'auth_queries\' parameter for authentication source ' |
||
| 180 | . '\'%s\' is expected to be an array. Instead it was: %s' . |
||
| 181 | $authQueryName, |
||
| 182 | $this->authId, |
||
| 183 | var_export($authQueryConfig, true), |
||
| 184 | )); |
||
| 185 | } |
||
| 186 | |||
| 187 | foreach (['database', 'query'] as $param) { |
||
| 188 | if (!array_key_exists($param, $authQueryConfig)) { |
||
| 189 | throw new Exception(sprintf( |
||
| 190 | 'Auth query %s is missing required attribute \'%s\' for authentication source \'%s\'', |
||
| 191 | $param, |
||
| 192 | $authQueryName, |
||
| 193 | $this->authId, |
||
| 194 | )); |
||
| 195 | } |
||
| 196 | if (!is_string($authQueryConfig[$param])) { |
||
| 197 | throw new Exception(sprintf( |
||
| 198 | 'Expected parameter \'%s\' for authentication source \'%s\' to be a string.' |
||
| 199 | . ' Instead it was: %s', |
||
| 200 | $param, |
||
| 201 | $this->authId, |
||
| 202 | var_export($authQueryConfig[$param], true), |
||
| 203 | )); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if (!array_key_exists($authQueryConfig['database'], $this->databases)) { |
||
| 208 | throw new Exception(sprintf( |
||
| 209 | 'Auth query %s references unknown database \'%s\' for authentication source \'%s\'', |
||
| 210 | $authQueryName, |
||
| 211 | $authQueryConfig['database'], |
||
| 212 | $this->authId, |
||
| 213 | )); |
||
| 214 | } |
||
| 215 | |||
| 216 | $this->authQueries[$authQueryName] = [ |
||
| 217 | // Will be set to true for the query that successfully authenticated the user |
||
| 218 | '_winning_auth_query' => false, |
||
| 219 | |||
| 220 | // Will hold the value of the attribute named by 'extract_userid_from' |
||
| 221 | // if specified and authentication succeeds |
||
| 222 | '_extracted_userid' => null, |
||
| 223 | |||
| 224 | 'database' => $authQueryConfig['database'], |
||
| 225 | 'query' => $authQueryConfig['query'], |
||
| 226 | ]; |
||
| 227 | |||
| 228 | if (array_key_exists('username_regex', $authQueryConfig)) { |
||
| 229 | if (!is_string($authQueryConfig['username_regex'])) { |
||
| 230 | throw new Exception(sprintf( |
||
| 231 | 'Optional parameter \'username_regex\' for authentication source \'%s\'' |
||
| 232 | . ' was provided and is expected to be a string. Instead it was: %s', |
||
| 233 | $this->authId, |
||
| 234 | var_export($authQueryConfig['username_regex'], true), |
||
| 235 | )); |
||
| 236 | } |
||
| 237 | $this->authQueries[$authQueryName]['username_regex'] = $authQueryConfig['username_regex']; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (array_key_exists('extract_userid_from', $authQueryConfig)) { |
||
| 241 | if (!is_string($authQueryConfig['extract_userid_from'])) { |
||
| 242 | throw new Exception(sprintf( |
||
| 243 | 'Optional parameter \'extract_userid_from\' for authentication source \'%s\'' |
||
| 244 | . ' was provided and is expected to be a string. Instead it was: %s', |
||
| 245 | $this->authId, |
||
| 246 | var_export($authQueryConfig['extract_userid_from'], true), |
||
| 247 | )); |
||
| 248 | } |
||
| 249 | $this->authQueries[$authQueryName]['extract_userid_from'] = $authQueryConfig['extract_userid_from']; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (array_key_exists('password_verify_hash_column', $authQueryConfig)) { |
||
| 253 | if (!is_string($authQueryConfig['password_verify_hash_column'])) { |
||
| 254 | throw new Exception(sprintf( |
||
| 255 | 'Optional parameter \'password_verify_hash_column\' for authentication source \'%s\'' |
||
| 256 | . ' was provided and is expected to be a string. Instead it was: %s', |
||
| 257 | $this->authId, |
||
| 258 | var_export($authQueryConfig['password_verify_hash_column'], true), |
||
| 259 | )); |
||
| 260 | } |
||
| 261 | $this->authQueries[$authQueryName]['password_verify_hash_column'] = |
||
| 262 | $authQueryConfig['password_verify_hash_column']; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | // attr_queries is optional, but if specified, we need to check the parameters |
||
| 268 | if (array_key_exists('attr_queries', $config)) { |
||
| 269 | if (!is_array($config['attr_queries'])) { |
||
| 270 | throw new Exception(sprintf( |
||
| 271 | 'Optional parameter \'attr_queries\' for authentication source \'%s\'' |
||
| 272 | . ' was provided and is expected to be an array. Instead it was: %s', |
||
| 273 | $this->authId, |
||
| 274 | var_export($config['attr_queries'], true), |
||
| 275 | )); |
||
| 276 | } |
||
| 277 | |||
| 278 | foreach ($config['attr_queries'] as $attrQueryConfig) { |
||
| 279 | if (!is_array($attrQueryConfig)) { |
||
| 280 | throw new Exception(sprintf( |
||
| 281 | '\'attr_queries\' parameter for authentication source \'%s\'' |
||
| 282 | . ' is expected to be an array. Instead it was: %s' . |
||
| 283 | $this->authId, |
||
| 284 | var_export($attrQueryConfig, true), |
||
| 285 | )); |
||
| 286 | } |
||
| 287 | |||
| 288 | foreach (['database', 'query'] as $param) { |
||
| 289 | if (!array_key_exists($param, $attrQueryConfig)) { |
||
| 290 | throw new Exception(sprintf( |
||
| 291 | 'Attribute query is missing required attribute \'%s\' for authentication source %s', |
||
| 292 | $param, |
||
| 293 | $this->authId, |
||
| 294 | )); |
||
| 295 | } |
||
| 296 | |||
| 297 | if (!is_string($attrQueryConfig[$param])) { |
||
| 298 | throw new Exception(sprintf( |
||
| 299 | 'Expected parameter \'%s\' for authentication source \'%s\'' |
||
| 300 | . ' to be a string. Instead it was: %s', |
||
| 301 | $param, |
||
| 302 | $this->authId, |
||
| 303 | var_export($attrQueryConfig[$param], true), |
||
| 304 | )); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $currentAttributeQuery = [ |
||
| 309 | 'database' => $attrQueryConfig['database'], |
||
| 310 | 'query' => $attrQueryConfig['query'], |
||
| 311 | ]; |
||
| 312 | |||
| 313 | if (!array_key_exists($attrQueryConfig['database'], $this->databases)) { |
||
| 314 | throw new Exception(sprintf( |
||
| 315 | 'Attribute query references unknown database \'%s\' for authentication source \'%s\'', |
||
| 316 | $attrQueryConfig['database'], |
||
| 317 | $this->authId, |
||
| 318 | )); |
||
| 319 | } |
||
| 320 | |||
| 321 | if (array_key_exists('only_for_auth', $attrQueryConfig)) { |
||
| 322 | if (!is_array($attrQueryConfig['only_for_auth'])) { |
||
| 323 | throw new Exception(sprintf( |
||
| 324 | 'Optional parameter \'only_for_auth\' for authentication source \'%s\'' |
||
| 325 | . ' was provided and is expected to be an array. Instead it was: %s', |
||
| 326 | $this->authId, |
||
| 327 | var_export($attrQueryConfig['only_for_auth'], true), |
||
| 328 | )); |
||
| 329 | } |
||
| 330 | foreach ($attrQueryConfig['only_for_auth'] as $authQueryName) { |
||
| 331 | if (!is_string($authQueryName)) { |
||
| 332 | throw new Exception(sprintf( |
||
| 333 | 'Each entry in the \'only_for_auth\' array for authentication source \'%s\'' |
||
| 334 | . ' is expected to be a string. Instead it was: %s', |
||
| 335 | $this->authId, |
||
| 336 | var_export($authQueryName, true), |
||
| 337 | )); |
||
| 338 | } |
||
| 339 | if (!array_key_exists($authQueryName, $this->authQueries)) { |
||
| 340 | throw new Exception(sprintf( |
||
| 341 | 'Attribute query references unknown auth query \'%s\' for authentication source %s', |
||
| 342 | $authQueryName, |
||
| 343 | $this->authId, |
||
| 344 | )); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | $currentAttributeQuery['only_for_auth'] = $attrQueryConfig['only_for_auth']; |
||
| 348 | } |
||
| 349 | |||
| 350 | $this->attributesQueries[] = $currentAttributeQuery; |
||
| 351 | } |
||
| 775 |