Issues (44)

src/Trait/DatabaseConfig.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Ikechukwukalu\Dynamicdatabaseconfig\Trait;
4
5
use Config;
6
use Ikechukwukalu\Dynamicdatabaseconfig\Models\DatabaseConfiguration;
7
use Illuminate\Support\Facades\Artisan;
8
use Illuminate\Support\Facades\DB;
9
10
trait DatabaseConfig
11
{
12
    /**
13
     * Add new database connection
14
     *
15
     * @param string $database
16
     * @param string $name
17
     * @param array $newConfig
18
     * @return void
19
     */
20
    public function addNewConfig(string $database, string $name, array $newConfig): void
21
    {
22
        if (!in_array($database, $this->listOfDatabases())) {
23
            return;
24
        }
25
26
        $this->newConfig($database, $name, $newConfig);
27
    }
28
29
    /**
30
     * Create database
31
     *
32
     * @param string $database
33
     * @param string $schemaName
34
     * @return void
35
     */
36
    public function createDatabase(string $database, string $schemaName): void
37
    {
38
        if ($database === 'pgsql') {
39
            DB::connection($database)->statement("SELECT 'CREATE DATABASE {$schemaName}'
40
            WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '{$schemaName}')\gexec");
41
            return;
42
        }
43
44
        if ($database === 'sqlsrv') {
45
            DB::connection($database)->statement("IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{$schemaName}')
46
            BEGIN
47
              CREATE DATABASE {$schemaName};
48
            END;
49
            GO");
50
            return;
51
        }
52
53
        /**
54
         * $database === 'mysql'
55
         */
56
        DB::connection($database)->statement("CREATE DATABASE IF NOT EXISTS {$schemaName};");
57
        return;
58
    }
59
60
    /**
61
     * Migrate database
62
     *
63
     * @param string $name
64
     * @param null|string $path
65
     * @return void
66
     */
67
    public function migrateDatabase(string $name, null|string $path): void
68
    {
69
        if (!$path) {
70
            Artisan::call('migrate', ['--database' => $name]);
71
72
            return;
73
        }
74
75
        Artisan::call('migrate', ['--database' => $name, '--path' => $path]);
76
    }
77
78
    /**
79
     * Get database config template
80
     *
81
     * @param string $database
82
     * @return null|array
83
     */
84
    public function getDatabaseConfigTemplate(string $database): null|array
85
    {
86
        if (!in_array($database, $this->listOfDatabases())) {
87
            return null;
88
        }
89
90
        return Config::get('database.connections.' . $database);
91
    }
92
93
    /**
94
     * Get database config
95
     *
96
     * @return array
97
     */
98
    public function getDatabaseConfig(): array
99
    {
100
        return Config::get('database');
101
    }
102
103
    /**
104
     * Get dynamic database configuration
105
     *
106
     * @param null|string $ref
107
     * @return array
108
     */
109
    public function getDynamicDatabaseConfiguration(string $ref): array
110
    {
111
        $databaseConfig = DatabaseConfiguration::configuredDatabase($ref)
112
                            ->first();
113
114
        return [
115
            $databaseConfig->database ?? null,
116
            $databaseConfig->configuration ?? null,
117
            $databaseConfig->name ?? null
118
        ];
119
    }
120
121
    /**
122
     * Set and retrieve new dynamic database configuration
123
     *
124
     * @param string $database
125
     * @param array $configuration
126
     * @return array
127
     */
128
    public function setNewDynamicConfig(string $database, array $configuration): array
129
    {
130
        return array_merge($this->getDatabaseConfigTemplate($database),
0 ignored issues
show
It seems like $this->getDatabaseConfigTemplate($database) can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        return array_merge(/** @scrutinizer ignore-type */ $this->getDatabaseConfigTemplate($database),
Loading history...
131
                $configuration);
132
    }
133
134
    /**
135
     * Set and retrieve new env config
136
     *
137
     * @param string $database
138
     * @param string $postFix
139
     * @return array
140
     */
141
    public function setNewEnvConfig(string $database, string $postFix): array
142
    {
143
        return array_merge($this->getDatabaseConfigTemplate($database), [
0 ignored issues
show
It seems like $this->getDatabaseConfigTemplate($database) can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        return array_merge(/** @scrutinizer ignore-type */ $this->getDatabaseConfigTemplate($database), [
Loading history...
144
            'host' =>  env('DB_HOST_' . $postFix),
145
            'port' => env('DB_PORT_' . $postFix),
146
            'database' => env('DB_DATABASE_' . $postFix),
147
            'username' => env('DB_USERNAME_' . $postFix),
148
            'password' => env('DB_PASSWORD_' . $postFix)
149
        ]);
150
    }
151
152
    /**
153
     * Set database config
154
     *
155
     * @param array $config
156
     * @return void
157
     */
158
    private function setDatabaseConfig(array $config): void
159
    {
160
        Config::set('database', $config);
161
    }
162
163
    /**
164
     * Merge new database config to existing config
165
     *
166
     * @param string $database
167
     * @param array $newConfig
168
     * @return array
169
     */
170
    private function mergeToDatabaseConfig(string $database, array $newConfig): array
171
    {
172
        return array_merge(Config::get('database.connections.' . $database),
173
                        $newConfig);
174
    }
175
176
    /**
177
     * New config
178
     *
179
     * @param string $database
180
     * @param string $name
181
     * @param array $newConfig
182
     * @return void
183
     */
184
    private function newConfig(string $database, string $name, array $newConfig): void
185
    {
186
        $config = $this->getDatabaseConfig();
187
        $newDatabaseConfig = $this->mergeToDatabaseConfig($database,
188
                            $newConfig);
189
        $config['connections'][$name] = $newDatabaseConfig;
190
        $this->setDatabaseConfig($config);
191
    }
192
193
    /**
194
     * List of databases
195
     *
196
     * @return array
197
     */
198
    private function listOfDatabases(): array
199
    {
200
        return array_keys(Config::get('database.connections'));
201
    }
202
}
203