| Conditions | 39 |
| Paths | 992 |
| Total Lines | 212 |
| Code Lines | 140 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | throw new Exception('Missing required attribute \'databases\' for authentication source ' . $this->authId); |
||
| 65 | } else { |
||
| 66 | if (!is_array($config['databases'])) { |
||
| 67 | throw new Exception('Required parameter \'databases\' for authentication source ' . |
||
| 68 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 69 | var_export($config['databases'], true)); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (empty($config['databases'])) { |
||
| 73 | throw new Exception('Required parameter \'databases\' for authentication source ' . |
||
| 74 | $this->authId . ' was provided but is an empty array.'); |
||
| 75 | } |
||
| 76 | |||
| 77 | foreach ($config['databases'] as $dbname => $dbConfig) { |
||
| 78 | if (!is_array($dbConfig)) { |
||
| 79 | throw new Exception('Each entry in the ' . |
||
| 80 | $dbname . ' \'databases\' parameter for authentication source ' . |
||
| 81 | $this->authId . ' is expected to be an array. Instead it was: ' . |
||
| 82 | var_export($dbConfig, true)); |
||
| 83 | } |
||
| 84 | foreach (['dsn', 'username', 'password'] as $param) { |
||
| 85 | if (!array_key_exists($param, $dbConfig)) { |
||
| 86 | throw new Exception('Database ' . |
||
| 87 | $dbname . ' is missing required attribute \'' . |
||
| 88 | $param . '\' for authentication source ' . |
||
| 89 | $this->authId); |
||
| 90 | } |
||
| 91 | if (!is_string($dbConfig[$param])) { |
||
| 92 | throw new Exception('Expected parameter \'' . $param . |
||
| 93 | '\' for authentication source ' . $this->authId . |
||
| 94 | ' to be a string. Instead it was: ' . |
||
| 95 | var_export($config[$param], true)); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (array_key_exists('options', $dbConfig) && !is_array($dbConfig['options'])) { |
||
| 100 | throw new Exception('Optional parameter \'options\' for authentication source ' . |
||
| 101 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 102 | var_export($dbConfig['options'], true)); |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->databases[$dbname] = [ |
||
| 106 | '_pdo' => null, // Will hold the PDO connection when connected |
||
| 107 | 'dsn' => $dbConfig['dsn'], |
||
| 108 | 'username' => $dbConfig['username'], |
||
| 109 | 'password' => $dbConfig['password'], |
||
| 110 | 'options' => $dbConfig['options'] ?? [], |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // Check auth_queries configuration that all required parameters are present |
||
| 116 | if (!array_key_exists('auth_queries', $config)) { |
||
| 117 | throw new Exception( |
||
| 118 | 'Missing required attribute \'auth_queries\' for authentication source ' . |
||
| 119 | $this->authId, |
||
| 120 | ); |
||
| 121 | } else { |
||
| 122 | if (!is_array($config['auth_queries'])) { |
||
| 123 | throw new Exception('Required parameter \'auth_queries\' for authentication source ' . |
||
| 124 | $this->authId . ' was provided and is expected to be an array. Instead it was: ' . |
||
| 125 | var_export($config['auth_queries'], true)); |
||
| 126 | } |
||
| 127 | |||
| 128 | if (empty($config['auth_queries'])) { |
||
| 129 | throw new Exception('Required parameter \'auth_queries\' for authentication source ' . |
||
| 130 | $this->authId . ' was provided but is an empty array.'); |
||
| 131 | } |
||
| 132 | |||
| 133 | foreach ($config['auth_queries'] as $authQueryName => $authQueryConfig) { |
||
| 134 | if (!is_array($authQueryConfig)) { |
||
| 135 | throw new Exception('Each entry in the ' . |
||
| 136 | $authQueryName . ' \'auth_queries\' parameter for authentication source ' . |
||
| 137 | $this->authId . ' is expected to be an array. Instead it was: ' . |
||
| 138 | var_export($authQueryConfig, true)); |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach (['database', 'query'] as $param) { |
||
| 142 | if (!array_key_exists($param, $authQueryConfig)) { |
||
| 143 | throw new Exception('Auth query ' . |
||
| 144 | $authQueryName . ' is missing required attribute \'' . |
||
| 145 | $param . '\' for authentication source ' . |
||
| 146 | $this->authId); |
||
| 147 | } |
||
| 148 | if (!is_string($authQueryConfig[$param])) { |
||
| 149 | throw new Exception('Expected parameter \'' . $param . |
||
| 150 | '\' for authentication source \'' . $this->authId . '\'' . |
||
| 151 | ' to be a string. Instead it was: ' . |
||
| 152 | var_export($authQueryConfig[$param], true)); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | if (!array_key_exists($authQueryConfig['database'], $this->databases)) { |
||
| 157 | throw new Exception('Auth query ' . |
||
| 158 | $authQueryName . ' references unknown database \'' . |
||
| 159 | $authQueryConfig['database'] . '\' for authentication source ' . |
||
| 160 | $this->authId); |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->authQueries[$authQueryName] = [ |
||
| 164 | // Will be set to true for the query that successfully authenticated the user |
||
| 165 | '_winning_auth_query' => false, |
||
| 166 | |||
| 167 | // Will hold the value of the attribute named by 'extract_userid_from' |
||
| 168 | // if specified and authentication succeeds |
||
| 169 | '_extracted_userid' => null, |
||
| 170 | |||
| 171 | 'database' => $authQueryConfig['database'], |
||
| 172 | 'query' => $authQueryConfig['query'], |
||
| 173 | ]; |
||
| 174 | |||
| 175 | if (array_key_exists('username_regex', $authQueryConfig)) { |
||
| 176 | if (!is_string($authQueryConfig['username_regex'])) { |
||
| 177 | throw new Exception('Optional parameter \'username_regex\' for authentication source ' . |
||
| 178 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 179 | var_export($authQueryConfig['username_regex'], true)); |
||
| 180 | } |
||
| 181 | $this->authQueries[$authQueryName]['username_regex'] = $authQueryConfig['username_regex']; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (array_key_exists('extract_userid_from', $authQueryConfig)) { |
||
| 185 | if (!is_string($authQueryConfig['extract_userid_from'])) { |
||
| 186 | throw new Exception('Optional parameter \'extract_userid_from\' for authentication source ' . |
||
| 187 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 188 | var_export($authQueryConfig['extract_userid_from'], true)); |
||
| 189 | } |
||
| 190 | $this->authQueries[$authQueryName]['extract_userid_from'] = $authQueryConfig['extract_userid_from']; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (array_key_exists('password_verify_hash_column', $authQueryConfig)) { |
||
| 194 | if (!is_string($authQueryConfig['password_verify_hash_column'])) { |
||
| 195 | throw new Exception( |
||
| 196 | 'Optional parameter \'password_verify_hash_column\' for authentication source ' . |
||
| 197 | $this->authId . ' was provided and is expected to be a string. Instead it was: ' . |
||
| 198 | var_export($authQueryConfig['password_verify_hash_column'], true), |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | $this->authQueries[$authQueryName]['password_verify_hash_column'] = |
||
| 202 | $authQueryConfig['password_verify_hash_column']; |
||
| 203 | } |
||
| 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 |