Completed
Push — master ( cfb253...483bcf )
by CodexShaper
07:39
created

Dumper   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 60
c 2
b 0
f 0
dl 0
loc 134
ccs 56
cts 77
cp 0.7272
rs 8.48
wmc 49

12 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 15 3
A __construct() 0 5 3
A create() 0 3 1
A prepareProcessCommand() 0 5 1
A prepareDatabase() 0 8 6
A prepareCreateTables() 0 7 5
A prepareSocket() 0 5 3
A prepareUserName() 0 7 5
A preparePort() 0 7 5
A prepareIgnoreTables() 0 11 6
A prepareHost() 0 7 5
A prepareIncludeTables() 0 8 6

How to fix   Complexity   

Complex Class

Complex classes like Dumper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Dumper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace CodexShaper\Dumper;
4
5
use CodexShaper\Dumper\Contracts\Dumper as DumperContract;
6
use CodexShaper\Dumper\Traits\DumperTrait;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
use Symfony\Component\Process\Process;
9
10
abstract class Dumper implements DumperContract
11
{
12
    use DumperTrait;
13
14 118
    public function __construct(array $options = [])
15
    {
16 118
        foreach ($options as $option => $value) {
17
            if (property_exists($this, $option)) {
18
                $this->{$option} = $value;
19
            }
20
        }
21 118
    }
22
    /**
23
     * @return $this
24
     */
25 118
    public static function create(array $options = [])
26
    {
27 118
        return new static($options);
28
    }
29
    /**
30
     * @return \Symfony\Component\Process\Process
31
     */
32
    protected function prepareProcessCommand()
33
    {
34
        $process = Process::fromShellCommandline($this->command);
35
        $process->setTimeout($this->timeout);
36
        return $process;
37
    }
38
    /**
39
     * @return \Symfony\Component\Process\Process
40
     */
41
    protected function run()
42
    {
43
        try {
44
45
            $process = Process::fromShellCommandline($this->command);
46
            $process->setTimeout($this->timeout);
47
48
            if ($this->debug) {
49
                return $process->mustRun();
50
            }
51
52
            return $process->run();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $process->run() returns the type integer which is incompatible with the documented return type Symfony\Component\Process\Process.
Loading history...
53
54
        } catch (ProcessFailedException $e) {
55
            throw new \Exception($e->getMessage());
56
57
        }
58
    }
59
60
    abstract public function dump();
61
    abstract public function restore();
62
63 58
    public function prepareHost()
64
    {
65 58
        switch (strtolower($this->getDumperClassName())) {
66 58
            case 'pgsqldumper':
67 30
                return ($this->socket !== '') ? $this->socket : $this->host;
68 28
            case 'mongodumper';
69 28
                return !empty($this->host) ? "--host {$this->host}" : "";
70
        }
71
    }
72
73 58
    public function preparePort()
74
    {
75 58
        switch (strtolower($this->getDumperClassName())) {
76 58
            case 'pgsqldumper':
77 30
                return !empty($this->port) ? '-p ' . $this->port : '';
78 28
            case 'mongodumper':
79 28
                return !empty($this->port) ? "--port {$this->port}" : "";
80
        }
81
    }
82
83 28
    public function prepareSocket()
84
    {
85 28
        switch (strtolower($this->getDumperClassName())) {
86 28
            case 'mysqldumper':
87 28
                return ($this->socket !== '') ? "--socket={$this->socket}" : '';
88
        }
89
    }
90
91 80
    public function prepareDatabase()
92
    {
93 80
        switch (strtolower($this->getDumperClassName())) {
94 80
            case 'mysqldumper':
95 46
            case 'pgsqldumper':
96 64
                return !empty($this->dbName) ? $this->dbName : "";
97 16
            case 'mongodumper';
98 16
                return !empty($this->dbName) ? "--db {$this->dbName}" : "";
99
        }
100
    }
101
102 58
    public function prepareUserName()
103
    {
104 58
        switch (strtolower($this->getDumperClassName())) {
105 58
            case 'pgsqldumper':
106 30
                return !empty($this->username) ? $this->username : "";
107 28
            case 'mongodumper';
108 28
                return !empty($this->username) ? "--username {$this->username}" : "";
109
        }
110
    }
111
112 52
    public function prepareIncludeTables()
113
    {
114 52
        switch (strtolower($this->getDumperClassName())) {
115 52
            case 'mysqldumper':
116 28
                $includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
117 28
                return !empty($includeTables) ? "--tables {$includeTables}" : '';
118 24
            case 'pgsqldumper':
119 24
                return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
120
        }
121
    }
122
123 52
    public function prepareIgnoreTables()
124
    {
125 52
        switch (strtolower($this->getDumperClassName())) {
126 52
            case 'mysqldumper':
127 28
                $ignoreTablesArgs = [];
128 28
                foreach ($this->ignoreTables as $tableName) {
129 4
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
130
                }
131 28
                return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
132 24
            case 'pgsqldumper';
133 24
                return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
134
        }
135
    }
136
137 52
    public function prepareCreateTables()
138
    {
139 52
        switch (strtolower($this->getDumperClassName())) {
140 52
            case 'mysqldumper':
141 28
                return !$this->createTables ? '--no-create-info' : '';
142 24
            case 'pgsqldumper':
143 24
                return (!$this->createTables) ? '--data-only' : '';
144
        }
145
    }
146
}
147