Completed
Push — master ( b84ab9...fc84ee )
by Freek
04:11
created

BackupJobFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 8
Bugs 0 Features 5
Metric Value
wmc 11
c 8
b 0
f 5
lcom 0
cbo 6
dl 0
loc 75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromArray() 0 9 1
A getFileSelection() 0 5 1
D getDbDumpers() 0 41 9
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
                case 'mysql':
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
                case 'pgsql':
61
                    $dbDumper =  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
69
                    if (isset($dbConfig['use_inserts']) && $dbConfig['use_inserts'] == true) {
70
                        $dbDumper->useInserts();
71
                    }
72
73
                    return $dbDumper;
74
                    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...
75
76
                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...
77
                    throw InvalidConfiguration::cannotUseUnsupportedDriver($dbConnectionName, $dbConfig['driver']);
78
                    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...
79
            }
80
        }, $dbConnectionNames);
81
82
        return $dbDumpers;
83
    }
84
}
85