Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Cli::unlinkErrorFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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