Dumper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 67
ccs 11
cts 25
cp 0.44
rs 10
c 2
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A create() 0 3 1
A run() 0 15 3
A prepareProcessCommand() 0 5 1
A isWindows() 0 3 2
A quoteCommand() 0 3 2
A removeExtraSpaces() 0 3 1
1
<?php
2
3
namespace CodexShaper\Dumper;
4
5
use CodexShaper\Dumper\Contracts\Dumper as DumperContract;
6
use CodexShaper\Dumper\Traits\DumperTrait;
7
use CodexShaper\Dumper\Traits\PrepareOptionsTrait;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
abstract class Dumper implements DumperContract
12
{
13
    use DumperTrait, PrepareOptionsTrait;
14
15 118
    public function __construct(array $options = [])
16
    {
17 118
        foreach ($options as $option => $value) {
18
            if (property_exists($this, $option)) {
19
                $this->{$option} = $value;
20
            }
21
        }
22 118
    }
23
    /**
24
     * @return $this
25
     */
26 118
    public static function create(array $options = [])
27
    {
28 118
        return new static($options);
29
    }
30
31
    abstract public function dump();
32
    abstract public function restore();
33
34
    /**
35
     * @return \Symfony\Component\Process\Process
36
     */
37
    protected function prepareProcessCommand()
38
    {
39
        $process = Process::fromShellCommandline($this->command);
40
        $process->setTimeout($this->timeout);
41
        return $process;
42
    }
43
    /**
44
     * @return \Symfony\Component\Process\Process
45
     */
46
    protected function run()
47
    {
48
        try {
49
50
            $process = Process::fromShellCommandline($this->command);
51
            $process->setTimeout($this->timeout);
52
53
            if ($this->debug) {
54
                return $process->mustRun();
55
            }
56
57
            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...
58
59
        } catch (ProcessFailedException $e) {
60
            throw new \Exception($e->getMessage());
61
62
        }
63
    }
64
65 108
    public function removeExtraSpaces(string $str)
66
    {
67 108
        return preg_replace('/\s+/', ' ', $str);
68
    }
69
70 108
    public static function isWindows()
71
    {
72 108
        return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
73
    }
74
75 108
    public function quoteCommand(string $command)
76
    {
77 108
        return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
78
    }
79
}
80