Passed
Pull Request — master (#20)
by Tim
02:46
created

PasswordVerify1Compat   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 45 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\sqlauth\Auth\Source;
6
7
/**
8
 * @package SimpleSAMLphp
9
 */
10
11
class PasswordVerify1Compat extends SQL2
12
{
13
    /**
14
     * Constructor for this authentication source.
15
     *
16
     * @param array $info  Information about this authentication source.
17
     * @param array $config  Configuration.
18
     */
19
    public function __construct(array $info, array $config)
20
    {
21
        /* Transform PasswordVerify (version 1) config to SQL2 config
22
         * Version 1 supported only one database, but multiple queries. The first query was defined
23
         * to be the "authentication query", all subsequent queries were "attribute queries".
24
         */
25
        $v2config = [
26
            'sqlauth:SQL2',
27
            'databases' => [
28
                'default' => [
29
                    'dsn' => $config['dsn'],
30
                    'username' => $config['username'],
31
                    'password' => $config['password'],
32
                ],
33
            ],
34
35
            'auth_queries' => [
36
                'default' => [
37
                    'database' => 'default',
38
                    'query' => is_array($config['query']) ? $config['query'][0] : $config['query'],
39
                    'password_verify_hash_column' => 'passwordhash',
40
                ],
41
            ],
42
        ];
43
44
        if (array_key_exists('username_regex', $config)) {
45
            $v2config['auth_queries']['default']['username_regex'] = $config['username_regex'];
46
        }
47
48
        // Override the default passwordhash column if configured
49
        if (array_key_exists('passwordhash_column', $config)) {
50
            $v2config['auth_queries']['default']['password_verify_hash_column'] = $config['passwordhash_column'];
51
        }
52
53
        if (is_array($config['query']) && count($config['query']) > 1) {
54
            $v2config['attr_queries'] = [];
55
            for ($i = 1; $i < count($config['query']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
56
                $v2config['attr_queries']['query' . $i] = [
57
                    'database' => 'default',
58
                    'query' => $config['query'][$i],
59
                ];
60
            }
61
        }
62
63
        parent::__construct($info, $v2config);
64
    }
65
}
66