DbDumperFactory::callMethodOnDumper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 3
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|array|null $methodValue
98
     *
99
     * @return \Spatie\DbDumper\DbDumper
100
     */
101
    protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue = null): DbDumper
102
    {
103
        if (! $methodValue) {
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