Completed
Push — develop ( f63da3...8c2b94 )
by Tom
04:32
created

Execs   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 120
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCompression() 0 4 1
A getCompressor() 0 8 2
A setFileName() 0 4 1
A getFileName() 0 4 1
A addOptions() 0 4 1
A add() 0 4 1
A getBaseCommand() 0 12 2
A getCommands() 0 15 4
1
<?php
2
/*
3
 * @author Tom Klingenberg <https://github.com/ktomk>
4
 */
5
6
namespace N98\Magento\Command\Database;
7
8
use N98\Magento\Command\Database\Compressor\AbstractCompressor;
9
10
/**
11
 * One or multiple commands to execute, with support for Compressors
12
 *
13
 * @package N98\Magento\Command\Database
14
 */
15
class Execs
16
{
17
    /**
18
     * @var array
19
     */
20
    private $options = [];
21
22
    /**
23
     * @var array
24
     */
25
    private $execs = [];
26
27
    /**
28
     * @var AbstractCompressor
29
     */
30
    private $compressor;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $fileName;
36
37
    /**
38
     * Execs constructor.
39
     *
40
     * @param string $command [optional]
0 ignored issues
show
Documentation introduced by
Should the type for parameter $command not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     */
42
    public function __construct($command = null)
43
    {
44
        $this->options = (array) $command;
45
    }
46
47
    /**
48
     * @param string $type of compression: "gz" | "gzip" | "none" | null
49
     */
50
    public function setCompression($type)
51
    {
52
        $this->compressor = AbstractCompressor::create($type);
53
    }
54
55
    /**
56
     * @return AbstractCompressor
57
     */
58
    public function getCompressor()
59
    {
60
        if (!$this->compressor) {
61
            $this->setCompression(null);
62
        }
63
64
        return $this->compressor;
65
    }
66
67
    /**
68
     * @param string $fileName output filename, will redirect mysqldump data into
69
     */
70
    public function setFileName($fileName)
71
    {
72
        $this->fileName = $fileName;
73
    }
74
75
    /**
76
     * @return string|null
77
     */
78
    public function getFileName()
79
    {
80
        return $this->fileName;
81
    }
82
83
    /**
84
     * @param string $options
85
     */
86
    public function addOptions($options)
87
    {
88
        $this->options[] = trim($options, ' ');
89
    }
90
91
    /**
92
     * @param string $options
93
     */
94
    public function add($options)
95
    {
96
        $this->execs[] = $options;
97
    }
98
99
    /**
100
     * @param string $separator
101
     * @return string
102
     */
103
    public function getBaseCommand($separator = '>')
104
    {
105
        $command = $this->getCompressor()->getCompressingCommand(
106
            implode(' ', $this->options)
107
        );
108
109
        if (strlen($this->fileName)) {
110
            $command .= ' ' . $separator . ' ' . escapeshellarg($this->fileName);
111
        }
112
113
        return $command;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getCommands()
120
    {
121
        if (empty($this->execs)) {
122
            return [$this->getBaseCommand()];
123
        }
124
125
        $commands = [];
126
        foreach ($this->execs as $exec) {
127
            $next = clone $this;
128
            $next->options[] = trim($exec);
129
            $commands[] = $next->getBaseCommand($commands ? '>>' : '>');
130
        }
131
132
        return $commands;
133
    }
134
}
135