Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.66% covered (warning)
53.66%
22 / 41
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
connection_parameter_factory
53.66% covered (warning)
53.66%
22 / 41
20.00% covered (danger)
20.00%
1 / 5
41.48
0.00% covered (danger)
0.00%
0 / 1
 get_configuration
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 build_connection_parameters
25.00% covered (danger)
25.00%
4 / 16
0.00% covered (danger)
0.00%
0 / 1
21.19
 build_sqlite_parameters
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 enrich_parameters
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
9.66
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\db\doctrine;
15
16/**
17 * Helper class to generate Doctrine DBAL configuration.
18 */
19class connection_parameter_factory
20{
21    /**
22     * Returns configuration options for Doctrine DBAL.
23     *
24     * @param string        $driver        Driver name.
25     * @param string|null    $host        Hostname.
26     * @param string|null    $user        Username.
27     * @param string|null    $password    Password.
28     * @param string|null    $name        Database name.
29     * @param string|null    $port        Database port.
30     *
31     * @return array Doctrine DBAL connection parameters.
32     *
33     * @throws \InvalidArgumentException If a required parameter is empty or null.
34     */
35    public static function get_configuration(
36        string $driver,
37        string|null $host = null,
38        string|null $user = null,
39        string|null $password = null,
40        string|null $name = null,
41        string|null $port = null) : array
42    {
43        $params = [
44            'driver' => $driver,
45        ];
46
47        return self::build_connection_parameters(
48            $params,
49            $host,
50            $user,
51            $password,
52            $name,
53            $port
54        );
55    }
56
57    /**
58     * Build Doctrine configuration array.
59     *
60     * @param array            $params        Parameter array.
61     * @param string|null    $host        Database hostname.
62     * @param string|null    $user        Username.
63     * @param string|null    $password    Password.
64     * @param string|null    $name        Database name.
65     * @param string|null    $port        Database port.
66     *
67     * @return array Doctrine's DBAL configuration for SQLite.
68     *
69     * @throws \InvalidArgumentException If a required parameter is empty or null.
70     */
71    private static function build_connection_parameters(
72        array $params,
73        string|null $host = null,
74        string|null $user = null,
75        string|null $password = null,
76        string|null $name = null,
77        string|null $port = null) : array
78    {
79        if (in_array($params['driver'], ['pdo_sqlite', 'sqlite3']))
80        {
81            return self::enrich_parameters(
82                self::build_sqlite_parameters($params, $host, $user, $password)
83            );
84        }
85
86        if (empty($user) || empty($name))
87        {
88            throw new \InvalidArgumentException('Required database parameter is not set.');
89        }
90
91        $params = array_merge($params, [
92            'host'        => $host,
93            'user'        => $user,
94            'dbname'    => $name,
95        ]);
96
97        if (!empty($password))
98        {
99            $params['password'] = $password;
100        }
101
102        if (!empty($port))
103        {
104            $params['port'] = (int) $port;
105        }
106
107        return self::enrich_parameters($params);
108    }
109
110    /**
111     * Build configuration array for SQLite.
112     *
113     * @param array            $params        Parameter array.
114     * @param string        $path        Path to the database.
115     * @param string|null    $user        Username.
116     * @param string|null    $password    Password.
117     *
118     * @return array Doctrine's DBAL configuration for SQLite.
119     */
120    private static function build_sqlite_parameters(array $params, string $path, string|null $user, string|null $password) : array
121    {
122        $params['path'] = $path;
123
124        if (!empty($user))
125        {
126            $params['user'] = $user;
127        }
128
129        if (!empty($password))
130        {
131            $params['password'] = $password;
132        }
133
134        return $params;
135    }
136
137    /**
138     * Add additional configuration options to the parameter list.
139     *
140     * @param array $params    The parameter list to enrich.
141     *
142     * @return array The enriched parameter list.
143     */
144    private static function enrich_parameters(array $params) : array
145    {
146        if (in_array($params['driver'], ['mysqli', 'pdo_mysql', 'pgsql', 'pdo_pgsql', 'oci8', 'pdo_oci']))
147        {
148            $params['charset'] = 'UTF8';
149        }
150
151        if ($params['driver'] === 'pdo_mysql' && extension_loaded('pdo_mysql'))
152        {
153            // Constant PDO::MYSQL_ATTR_FOUND_ROWS is deprecated since 8.5, use Pdo\Mysql::ATTR_FOUND_ROWS instead
154            if (class_exists('\Pdo\Mysql'))
155            {
156                /**
157                 * @psalm-suppress UndefinedClass
158                 */
159                $params[\Pdo\Mysql::ATTR_FOUND_ROWS] = true;
160            }
161            else
162            {
163                $params[\PDO::MYSQL_ATTR_FOUND_ROWS] = true;
164            }
165        }
166
167        return $params;
168    }
169
170    /*
171     * Disable constructing this class.
172     */
173    private function __construct()
174    {
175    }
176}