BackupFactory::buildBackupEngine()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 55

Duplication

Lines 20
Ratio 36.36 %

Importance

Changes 0
Metric Value
dl 20
loc 55
rs 8.0484
c 0
b 0
f 0
cc 7
nc 5
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cornford\Backup;
4
5
use Cornford\Backup\Contracts\BackupFactoryInterface;
6
use Cornford\Backup\Contracts\BackupEngineInterface;
7
use Cornford\Backup\Contracts\BackupFilesystemInterface;
8
use Cornford\Backup\Contracts\BackupInterface;
9
use Cornford\Backup\Engines\BackupEngineMysql;
10
use Cornford\Backup\Engines\BackupEnginePgsql;
11
use Cornford\Backup\Engines\BackupEngineSqlite;
12
use Cornford\Backup\Engines\BackupEngineSqlsrv;
13
use Cornford\Backup\Exceptions\BackupException;
14
use Cornford\Backup\Exceptions\BackupArgumentException;
15
use Symfony\Component\Process\Process;
16
17
class BackupFactory implements BackupFactoryInterface
18
{
19
    /**
20
     * Build a new Backup object and its dependencies.
21
     *
22
     * @param array  $options
23
     * @param string $database
24
     *
25
     * @throws BackupException
26
     *
27
     * @return BackupInterface
28
     */
29
    public function build(
30
        array $options = [],
31
        $database = null
32
    ): BackupInterface {
33
        $backupEngine = $this->buildBackupEngine($options, $database);
34
        $backupFilesystem = $this->buildBackupFilesystem();
35
36
        return $this->buildBackup($backupEngine, $backupFilesystem, $options);
37
    }
38
39
    /**
40
     * Build the Backup object.
41
     *
42
     * @param BackupEngineInterface     $backupEngine
43
     * @param BackupFilesystemInterface $backupFilesystem
44
     * @param array                     $options
45
     *
46
     * @throws BackupArgumentException
47
     *
48
     * @return BackupInterface
49
     */
50
    public function buildBackup(
51
        BackupEngineInterface $backupEngine,
52
        BackupFilesystemInterface $backupFilesystem,
53
        array $options = []
54
    ): BackupInterface {
55
        return new Backup($backupEngine, $backupFilesystem, $options);
56
    }
57
58
    /**
59
     * Build a new Backup Engine object.
60
     *
61
     * @param array  $options
62
     * @param string $database
63
     *
64
     * @throws BackupException
65
     *
66
     * @return BackupEngineInterface
67
     */
68
    public function buildBackupEngine(
69
        array $options = [],
70
        $database = null
71
    ): BackupEngineInterface {
72
        $backupProcessInstance = new BackupProcess(new Process([]));
73
74
        switch ($database ? $database : $options['default']) {
75
            case 'mysql':
76
                return new BackupEngineMysql(
77
                    $backupProcessInstance,
78
                    $options['connections'][$options['default']]['database'],
79
                    $options['connections'][$options['default']]['host'],
80
                    (
81
                        isset($options['connections'][$options['default']]['port']) ?
82
                            $options['connections'][$options['default']]['port'] :
83
                            3306
84
                    ),
85
                    $options['connections'][$options['default']]['username'],
86
                    $options['connections'][$options['default']]['password'],
87
                    $options
88
                );
89 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...
90
                return new BackupEnginePgsql(
91
                    $backupProcessInstance,
92
                    $options['connections'][$options['default']]['database'],
93
                    $options['connections'][$options['default']]['host'],
94
                    null,
95
                    $options['connections'][$options['default']]['username'],
96
                    $options['connections'][$options['default']]['password'],
97
                    $options
98
                );
99
            case 'sqlite':
100
                return new BackupEngineSqlite(
101
                    $backupProcessInstance,
102
                    $options['connections'][$options['default']]['database'],
103
                    null,
104
                    null,
105
                    null,
106
                    null,
107
                    $options
108
                );
109 View Code Duplication
            case 'sqlsrv':
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...
110
                return new BackupEngineSqlsrv(
111
                    $backupProcessInstance,
112
                    $options['connections'][$options['default']]['database'],
113
                    $options['connections'][$options['default']]['host'],
114
                    null,
115
                    $options['connections'][$options['default']]['username'],
116
                    $options['connections'][$options['default']]['password'],
117
                    $options
118
                );
119
            default:
120
                throw new BackupException('Database driver isn\'t supported.');
121
        }
122
    }
123
124
    /**
125
     * Build the Backup filesystem object.
126
     *
127
     * @return BackupFilesystemInterface
128
     */
129
    public function buildBackupFilesystem(): BackupFilesystemInterface
130
    {
131
        return new BackupFilesystem();
132
    }
133
}
134