Completed
Push — master ( 5c1b58...59d232 )
by Freek
14s queued 11s
created

DbDumperFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

5 Methods

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