Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.18% covered (warning)
70.18%
40 / 57
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
connection_parameter_factory
70.18% covered (warning)
70.18%
40 / 57
20.00% covered (danger)
20.00%
1 / 5
19.20
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
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
3.01
 __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
16use InvalidArgumentException;
17use phpbb\db\doctrine\oci8\driver as oci8_driver;
18
19/**
20 * Helper class to generate Doctrine DBAL configuration.
21 */
22class connection_parameter_factory
23{
24    /**
25     * Returns configuration options for Doctrine DBAL.
26     *
27     * @param string        $driver        Driver name.
28     * @param string|null    $host        Hostname.
29     * @param string|null    $user        Username.
30     * @param string|null    $password    Password.
31     * @param string|null    $name        Database name.
32     * @param string|null    $port        Database port.
33     *
34     * @return array Doctrine DBAL connection parameters.
35     *
36     * @throws InvalidArgumentException If a required parameter is empty or null.
37     */
38    public static function get_configuration(
39        string $driver,
40        ?string $host = null,
41        ?string $user = null,
42        ?string $password = null,
43        ?string $name = null,
44        ?string $port = null) : array
45    {
46        $params = [
47            'driver' => $driver,
48        ];
49
50        return self::build_connection_parameters(
51            $params,
52            $host,
53            $user,
54            $password,
55            $name,
56            $port
57        );
58    }
59
60    /**
61     * Build Doctrine configuration array.
62     *
63     * @param array            $params        Parameter array.
64     * @param string|null    $host        Database hostname.
65     * @param string|null    $user        Username.
66     * @param string|null    $password    Password.
67     * @param string|null    $name        Database name.
68     * @param string|null    $port        Database port.
69     *
70     * @return array Doctrine's DBAL configuration for SQLite.
71     *
72     * @throws InvalidArgumentException If a required parameter is empty or null.
73     */
74    private static function build_connection_parameters(
75        array $params,
76        ?string $host = null,
77        ?string $user = null,
78        ?string $password = null,
79        ?string $name = null,
80        ?string $port = null) : array
81    {
82        if ($params['driver'] === 'pdo_sqlite')
83        {
84            return self::enrich_parameters(
85                self::build_sqlite_parameters($params, $host, $user, $password)
86            );
87        }
88
89        if (empty($user) || empty($name))
90        {
91            throw new InvalidArgumentException('Required database parameter is not set.');
92        }
93
94        $params = array_merge($params, [
95            'host'        => $host,
96            'user'        => $user,
97            'dbname'    => $name,
98        ]);
99
100        if (!empty($password))
101        {
102            $params['password'] = $password;
103        }
104
105        if (!empty($port))
106        {
107            $params['port'] = (int) $port;
108        }
109
110        return self::enrich_parameters($params);
111    }
112
113    /**
114     * Build configuration array for SQLite.
115     *
116     * @param array            $params        Parameter array.
117     * @param string        $path        Path to the database.
118     * @param string|null    $user        Username.
119     * @param string|null    $password    Password.
120     *
121     * @return array Doctrine's DBAL configuration for SQLite.
122     */
123    private static function build_sqlite_parameters(array $params, string $path, ?string $user, ?string $password) : array
124    {
125        $params['path'] = $path;
126
127        if (!empty($user))
128        {
129            $params['user'] = $user;
130        }
131
132        if (!empty($password))
133        {
134            $params['password'] = $password;
135        }
136
137        return $params;
138    }
139
140    /**
141     * Add additional configuration options to the parameter list.
142     *
143     * @param array $params    The parameter list to enrich.
144     *
145     * @return array The enriched parameter list.
146     */
147    private static function enrich_parameters(array $params) : array
148    {
149        $enrichment_tags = [
150            'pdo_mysql' => [
151                'charset' => 'UTF8',
152            ],
153            'oci8' => [
154                'charset' => 'UTF8',
155                'platform' => new oracle_platform(),
156                'driverClass' => oci8_driver::class,
157            ],
158            'pdo_pgsql' => [
159                'charset' => 'UTF8',
160                'platform' => new postgresql_platform(),
161            ],
162            'pdo_sqlsrv' => [
163                'platform' => new sqlsrv_platform(),
164            ],
165        ];
166
167        if ($params['driver'] === 'pdo_mysql')
168        {
169            $enrichment_tags['pdo_mysql'][\PDO::MYSQL_ATTR_FOUND_ROWS] = true;
170        }
171
172        $driver = $params['driver'];
173        if (!array_key_exists($driver, $enrichment_tags))
174        {
175            return $params;
176        }
177
178        return array_merge($params, $enrichment_tags[$driver]);
179    }
180
181    /*
182     * Disable constructing this class.
183     */
184    private function __construct()
185    {
186    }
187}