Completed
Push — master ( a28d80...2201d9 )
by Freek
02:58
created

DbDumperFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
B createFromConnection() 0 26 4
A forDriver() 0 18 4
A processExtraDumpParameters() 0 15 4
A callMethodOnDumper() 0 12 2
A determineValidMethodName() 0 7 1
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\PostgreSql;
9
use Spatie\Backup\Exceptions\CannotCreateDbDumper;
10
11
class DbDumperFactory
12
{
13
    /**
14
     * @param string $dbConnectionName
15
     *
16
     * @return \Spatie\DbDumper\DbDumper
17
     */
18
    public static function createFromConnection(string $dbConnectionName): DbDumper
19
    {
20
        $dbConfig = config("database.connections.{$dbConnectionName}");
21
22
        $dbHost = array_get($dbConfig, 'read.host', array_get($dbConfig, 'host'));
23
24
        $dbDumper = static::forDriver($dbConfig['driver'])
25
            ->setHost($dbHost ?? '')
26
            ->setDbName($dbConfig['database'])
27
            ->setUserName($dbConfig['username'] ?? '')
28
            ->setPassword($dbConfig['password'] ?? '');
29
30
        if ($dbDumper instanceof MySql) {
31
            $dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? '');
32
        }
33
34
        if (isset($dbConfig['port'])) {
35
            $dbDumper = $dbDumper->setPort($dbConfig['port']);
36
        }
37
38
        if (isset($dbConfig['dump'])) {
39
            $dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper);
40
        }
41
42
        return $dbDumper;
43
    }
44
45
    protected static function forDriver($dbDriver): DbDumper
46
    {
47
        $driver = strtolower($dbDriver);
48
49
        if ($driver === 'mysql') {
50
            return new MySql();
51
        }
52
53
        if ($driver === 'pgsql') {
54
            return new PostgreSql();
55
        }
56
57
        if ($driver === 'sqlite') {
58
            return new Sqlite();
59
        }
60
61
        throw CannotCreateDbDumper::unsupportedDriver($driver);
62
    }
63
64
    /**
65
     * @param array $dumpConfiguration
66
     *
67
     * @param $dbDumper
68
     *
69
     * @return mixed
70
     */
71
    protected static function processExtraDumpParameters(array $dumpConfiguration, $dbDumper): DbDumper
72
    {
73
        collect($dumpConfiguration)->each(function ($configValue, $configName) use ($dbDumper) {
74
            $methodName = lcfirst(studly_case(is_numeric($configName) ? $configValue : $configName));
75
            $methodValue = is_numeric($configName) ? null : $configValue;
76
77
            $methodName = static::determineValidMethodName($dbDumper, $methodName);
78
79
            if (method_exists($dbDumper, $methodName)) {
80
                static::callMethodOnDumper($dbDumper, $methodName, $methodValue);
81
            }
82
        });
83
84
        return $dbDumper;
85
    }
86
87
    /**
88
     * @param \Spatie\DbDumper\DbDumper $dbDumper
89
     * @param string $methodName
90
     * @param string|null $methodValue
91
     *
92
     * @return \Spatie\DbDumper\DbDumper
93
     */
94
    protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue): DbDumper
95
    {
96
        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...
97
            $dbDumper->$methodName();
98
99
            return $dbDumper;
100
        }
101
102
        $dbDumper->$methodName($methodValue);
103
104
        return $dbDumper;
105
    }
106
107
    protected static function determineValidMethodName(DbDumper $dbDumper, string $methodName): string
108
    {
109
        return collect([$methodName, 'set'.ucfirst($methodName)])
110
            ->first(function (string $methodName) use ($dbDumper) {
111
                return method_exists($dbDumper, $methodName);
112
            }, '');
113
    }
114
}
115