Completed
Pull Request — master (#643)
by Costin
02:20
created

DbDumperFactory::createFromConnection()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 32
nop 1
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Spatie\DbDumper\DbDumper;
6
use Spatie\DbDumper\Databases\MySql;
7
use Spatie\DbDumper\Databases\Sqlite;
8
use Spatie\DbDumper\Databases\MongoDb;
9
use Spatie\DbDumper\Databases\PostgreSql;
10
use Spatie\Backup\Exceptions\CannotCreateDbDumper;
11
12
class DbDumperFactory
13
{
14
    /**
15
     * @param string $dbConnectionName
16
     *
17
     * @return \Spatie\DbDumper\DbDumper
18
     */
19
    public static function createFromConnection(string $dbConnectionName): DbDumper
20
    {
21
        $dbConfig = config("database.connections.{$dbConnectionName}");
22
23
        if (isset($dbConfig['read'])) {
24
            $dbConfig = array_except(
25
                array_merge($dbConfig, $dbConfig['read']),
26
                ['read', 'write']
27
            );
28
        }
29
30
        $dbDumper = static::forDriver($dbConfig['driver'])
31
            ->setHost(array_first(array_wrap($dbConfig['host'] ?? '')))
32
            ->setDbName($dbConfig['database'])
33
            ->setUserName($dbConfig['username'] ?? '')
34
            ->setPassword($dbConfig['password'] ?? '');
35
36
        if ($dbDumper instanceof MySql) {
37
            $dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? '');
38
        }
39
40
        if (isset($dbConfig['port'])) {
41
            $dbDumper = $dbDumper->setPort($dbConfig['port']);
42
        }
43
44
        if ($dbDumper instanceof MySql && config('backup.backup.dump_from_master') !== 'AUTO') {
45
            $dbDumper = $dbDumper->setGtidPurged(config('backup.backup.dump_from_master') ?? 'AUTO');
0 ignored issues
show
Bug introduced by
The method setGtidPurged() does not seem to exist on object<Spatie\DbDumper\Databases\MySql>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        }
47
48
        if (isset($dbConfig['dump'])) {
49
            $dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper);
50
        }
51
52
        return $dbDumper;
53
    }
54
55
    protected static function forDriver($dbDriver): DbDumper
56
    {
57
        $driver = strtolower($dbDriver);
58
59
        if ($driver === 'mysql') {
60
            return new MySql();
61
        }
62
63
        if ($driver === 'pgsql') {
64
            return new PostgreSql();
65
        }
66
67
        if ($driver === 'sqlite') {
68
            return new Sqlite();
69
        }
70
71
        if ($driver === 'mongodb') {
72
            return new MongoDb();
73
        }
74
75
        throw CannotCreateDbDumper::unsupportedDriver($driver);
76
    }
77
78
    /**
79
     * @param array $dumpConfiguration
80
     *
81
     * @param $dbDumper
82
     *
83
     * @return mixed
84
     */
85
    protected static function processExtraDumpParameters(array $dumpConfiguration, $dbDumper): DbDumper
86
    {
87
        collect($dumpConfiguration)->each(function ($configValue, $configName) use ($dbDumper) {
88
            $methodName = lcfirst(studly_case(is_numeric($configName) ? $configValue : $configName));
89
            $methodValue = is_numeric($configName) ? null : $configValue;
90
91
            $methodName = static::determineValidMethodName($dbDumper, $methodName);
92
93
            if (method_exists($dbDumper, $methodName)) {
94
                static::callMethodOnDumper($dbDumper, $methodName, $methodValue);
95
            }
96
        });
97
98
        return $dbDumper;
99
    }
100
101
    /**
102
     * @param \Spatie\DbDumper\DbDumper $dbDumper
103
     * @param string $methodName
104
     * @param string|null $methodValue
105
     *
106
     * @return \Spatie\DbDumper\DbDumper
107
     */
108
    protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue): DbDumper
109
    {
110
        if (! $methodValue) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methodValue of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
111
            $dbDumper->$methodName();
112
113
            return $dbDumper;
114
        }
115
116
        $dbDumper->$methodName($methodValue);
117
118
        return $dbDumper;
119
    }
120
121
    protected static function determineValidMethodName(DbDumper $dbDumper, string $methodName): string
122
    {
123
        return collect([$methodName, 'set'.ucfirst($methodName)])
124
            ->first(function (string $methodName) use ($dbDumper) {
125
                return method_exists($dbDumper, $methodName);
126
            }, '');
127
    }
128
}
129