Passed
Pull Request — master (#20)
by
unknown
04:14
created

SQL1Compat::__construct()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 40
rs 8.9617
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 SQL1Compat 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 SQL (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
                ],
40
            ],
41
        ];
42
43
        if (array_key_exists('username_regex', $config)) {
44
            $v2config['auth_queries']['default']['username_regex'] = $config['username_regex'];
45
        }
46
47
        $numQueries = is_array($config['query']) ? count($config['query']) : 0;
48
        if ($numQueries > 1) {
49
            $v2config['attr_queries'] = [];
50
            for ($i = 1; $i < $numQueries; $i++) {
51
                $v2config['attr_queries']['query' . $i] = [
52
                    'database' => 'default',
53
                    'query' => $config['query'][$i],
54
                ];
55
            }
56
        }
57
58
        parent::__construct($info, $v2config);
59
    }
60
}
61