Completed
Push — master ( 273890...647b73 )
by Sebastian
03:36
created

Cli::toAbsolutePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace phpbu\App\Backup;
3
4
use phpbu\App\Cli\Executable;
5
use phpbu\App\Cli\Result;
6
use phpbu\App\Configuration;
7
use phpbu\App\Util;
8
use SebastianFeldmann\Cli\Command\Runner;
9
10
/**
11
 * Base class for Actions using cli tools.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 2.1.0
20
 */
21
abstract class Cli
22
{
23
    /**
24
     * Command runner to execute the executable.
25
     *
26
     * @var \SebastianFeldmann\Cli\Command\Runner
27
     */
28
    protected $runner;
29
30
    /**
31
     * Current timestamp
32
     *
33
     * @var int
34
     */
35
    protected $time;
36
37
    /**
38
     * Executable command.
39
     *
40 129
     * @var \phpbu\App\Cli\Executable
41
     */
42 129
    protected $executable;
43 129
44
    /**
45
     * Cli constructor.
46
     *
47
     * @param \SebastianFeldmann\Cli\Command\Runner $runner
48
     * @param int                                   $time
49
     */
50
    public function __construct(Runner $runner = null, $time = null)
51
    {
52 37
        $this->runner = $runner ? : new Runner\Simple();
53
        $this->time   = $time   ? : time();
54 37
    }
55
56
    /**
57
     * Executes the cli commands and handles compression
58
     *
59
     * @param  \phpbu\App\Backup\Target $target
60
     * @return \phpbu\App\Cli\Result
61
     * @throws \RuntimeException
62
     */
63 41
    protected function execute(Target $target) : Result
64
    {
65 41
        return $this->runCommand($this->getExecutable($target));
66
    }
67 40
68
    /**
69 2
     * Execute a cli command.
70
     *
71
     * @param  \phpbu\App\Cli\Executable $command
72 40
     * @return \phpbu\App\Cli\Result
73
     */
74
    protected function runCommand(Executable $command) : Result
75
    {
76
        $res = $this->runner->run($command);
77
78
        if (!$res->isSuccessful() && $res->isOutputRedirected()) {
79
            // remove file with errors
80 2
            $this->unlinkErrorFile($res->getRedirectPath());
81
        }
82 2
83 2
        return new Result($res->getCommandResult(), $command->getCommandPrintable());
84
    }
85 2
86
    /**
87
     * Remove file if it exists.
88
     *
89
     * @param string $file
90
     */
91
    public function unlinkErrorFile(string $file)
92
    {
93 99
        if (file_exists($file) && !is_dir($file)) {
94
            unlink($file);
95 99
        }
96 99
    }
97
98 98
    /**
99
     * Returns the executable for this action.
100
     *
101
     * @param  \phpbu\App\Backup\Target $target
102
     * @return \phpbu\App\Cli\Executable
103
     */
104
    public function getExecutable(Target $target) : Executable
105
    {
106
        if (null === $this->executable) {
107
            $this->executable = $this->createExecutable($target);
108
        }
109
        return $this->executable;
110
    }
111
112
    /**
113
     * Return an absolute path relative to the used file.
114
     *
115
     * @param  string $path
116
     * @param  string $default
117
     * @return string
118
     */
119
    protected function toAbsolutePath(string $path, string $default = '')
120
    {
121
        return !empty($path) ? Util\Path::toAbsolutePath($path, Configuration::getWorkingDirectory()) : $default;
122
    }
123
124
    /**
125
     * Creates the executable for this action.
126
     *
127
     * @param  \phpbu\App\Backup\Target $target
128
     * @return \phpbu\App\Cli\Executable
129
     */
130
    abstract protected function createExecutable(Target $target) : Executable;
131
}
132