Completed
Push — master ( 198887...1344a8 )
by Freek
04:45
created

BackupJobFactory::getDbDumpers()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 35
Code Lines 27

Duplication

Lines 18
Ratio 51.43 %

Importance

Changes 6
Bugs 0 Features 4
Metric Value
c 6
b 0
f 4
dl 18
loc 35
rs 6.7272
cc 7
eloc 27
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Spatie\Backup\BackupDestination\BackupDestinationFactory;
6
use Spatie\Backup\Exceptions\InvalidConfiguration;
7
use Spatie\DbDumper\Databases\MySql;
8
use Spatie\DbDumper\Databases\PostgreSql;
9
10
class BackupJobFactory
11
{
12
    /**
13
     * @param array $config
14
     *
15
     * @return \Spatie\Backup\Tasks\Backup\BackupJob
16
     */
17
    public static function createFromArray(array $config)
18
    {
19
        $backupJob = (new BackupJob())
20
            ->setFileSelection(static::getFileSelection($config['backup']['source']['files']))
21
            ->setDbDumpers(static::getDbDumpers($config['backup']['source']['databases']))
22
            ->setBackupDestinations(BackupDestinationFactory::createFromArray($config['backup']));
23
24
        return $backupJob;
25
    }
26
27
    /**
28
     * @param array $sourceFiles
29
     *
30
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
31
     */
32
    protected static function getFileSelection(array $sourceFiles)
33
    {
34
        return (new FileSelection($sourceFiles['include']))
35
            ->excludeFilesFrom($sourceFiles['exclude']);
36
    }
37
38
    /**
39
     * @param array $dbConnectionNames
40
     *
41
     * @return array
42
     */
43
    protected static function getDbDumpers(array $dbConnectionNames)
44
    {
45
        $dbDumpers = array_map(function ($dbConnectionName) {
46
47
            $dbConfig = config("database.connections.{$dbConnectionName}");
48
49
            switch ($dbConfig['driver']) {
50 View Code Duplication
                case 'mysql':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                    return MySql::create()
52
                        ->setHost($dbConfig['host'])
53
                        ->setDbName($dbConfig['database'])
54
                        ->setUserName($dbConfig['username'])
55
                        ->setPassword($dbConfig['password'])
56
                        ->setDumpBinaryPath(isset($dbConfig['dump_command_path']) ? $dbConfig['dump_command_path'] : '')
57
                        ->setTimeout(isset($dbConfig['dump_command_timeout']) ? $dbConfig['dump_command_timeout'] : null);
58
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
59
60 View Code Duplication
                case 'pgsql':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
                    return PostgreSql::create()
62
                        ->setHost($dbConfig['host'])
63
                        ->setDbName($dbConfig['database'])
64
                        ->setUserName($dbConfig['username'])
65
                        ->setPassword($dbConfig['password'])
66
                        ->setDumpBinaryPath(isset($dbConfig['dump_command_path']) ? $dbConfig['dump_command_path'] : '')
67
                        ->setTimeout(isset($dbConfig['dump_command_timeout']) ? $dbConfig['dump_command_timeout'] : null);
68
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
70
                default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
                    throw InvalidConfiguration::cannotUseUnsupportedDriver($dbConnectionName, $dbConfig['driver']);
72
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
73
            }
74
        }, $dbConnectionNames);
75
76
        return $dbDumpers;
77
    }
78
}
79