Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.55% covered (success)
96.55%
28 / 29
88.89% covered (success)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseConfiguration
96.55% covered (success)
96.55%
28 / 29
88.89% covered (success)
88.89%
8 / 9
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
4
 getServer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPort
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPassword
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDatabase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSchema
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Database configuration class
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2023-04-30
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Configuration;
21
22use RuntimeException;
23
24readonly class DatabaseConfiguration
25{
26    private string $server;
27
28    private ?int $port;
29
30    private string $user;
31
32    private string $password;
33
34    private string $database;
35
36    private string $prefix;
37
38    private string $type;
39
40    private ?string $schema;
41
42    public function __construct(string $filename)
43    {
44        if (!is_readable($filename)) {
45            throw new RuntimeException(sprintf('Database configuration file "%s" is not readable.', $filename));
46        }
47
48        $DB = [
49            'server' => '',
50            'port' => '',
51            'user' => '',
52            'password' => '',
53            'db' => '',
54            'prefix' => '',
55            'type' => '',
56            'schema' => '',
57        ];
58
59        include $filename;
60
61        $this->server = $DB['server'];
62        $this->port = $DB['port'] === '' ? null : (int) $DB['port'];
63        $this->user = $DB['user'];
64        $this->password = $DB['password'];
65        $this->database = $DB['db'];
66        $this->prefix = $DB['prefix'];
67        $this->type = $DB['type'];
68        $this->schema = $DB['schema'] === '' ? null : $DB['schema'];
69    }
70
71    public function getServer(): string
72    {
73        return $this->server;
74    }
75
76    public function getPort(): ?int
77    {
78        return $this->port;
79    }
80
81    public function getUser(): string
82    {
83        return $this->user;
84    }
85
86    public function getPassword(): string
87    {
88        return $this->password;
89    }
90
91    public function getDatabase(): string
92    {
93        return $this->database;
94    }
95
96    public function getPrefix(): string
97    {
98        return $this->prefix;
99    }
100
101    public function getType(): string
102    {
103        return $this->type;
104    }
105
106    public function getSchema(): ?string
107    {
108        return $this->schema;
109    }
110}