Completed
Pull Request — master (#166)
by
unknown
112:06 queued 110:39
created

AbstractCaller::getOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command\Caller;
22
23
/**
24
 * AbstractCaller
25
 *
26
 * @author Tim Bernhard <[email protected]>
27
 */
28
abstract class AbstractCaller implements CallerInterface
29
{
30
    /**
31
     * Git binary path
32
     *
33
     * @var string
34
     */
35
    protected $binaryPath;
36
37
    /**
38
     * Git binary version
39
     *
40
     * @var string
41
     */
42
    protected $binaryVersion;
43
44
    /**
45
     * the output lines of the command
46
     *
47
     * @var array
48
     */
49
    protected $outputLines = [];
50
51
    /**
52
     * @inheritdoc
53
     */
54 103
    public function getBinaryPath(): string
55
    {
56 103
        return $this->binaryPath;
57
    }
58
59
    /**
60
     * path setter
61
     *
62
     * @param string $path the path to the system git binary
63
     */
64 106
    public function setBinaryPath(string $path): self
65
    {
66 106
        $this->binaryPath = $path;
67
68 106
        return $this;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 5
    public function getBinaryVersion(): string
75
    {
76 5
        if (is_null($this->binaryVersion)) {
77 5
            $this->execute('--version');
78 5
            $version = $this->getOutput();
79 5
            if (!preg_match('/^git version [0-9\.]+/', $version)) {
80
                throw new \RuntimeException('Could not parse git version. Unexpected format "' . $version . '".');
81
            }
82 5
            $this->binaryVersion = preg_replace('/^git version ([0-9\.]+)/', '$1', $version);
83
        }
84
85 5
        return $this->binaryVersion;
86
    }
87
88
    /**
89
     * returns the output of the last executed command
90
     *
91
     * @return string
92
     */
93
    public function getOutput(): string
94
    {
95
        return implode("\n", $this->outputLines);
96
    }
97
98
    /**
99
     * returns the output of the last executed command as an array of lines
100
     *
101
     * @param bool $stripBlankLines remove the blank lines
102
     *
103
     * @return array
104
     */
105
    public function getOutputLines(bool $stripBlankLines = false): array
106
    {
107
        if ($stripBlankLines) {
108
            $output = [];
109
            foreach ($this->outputLines as $line) {
110
                if ('' !== $line) {
111
                    $output[] = $line;
112
                }
113
            }
114
115
            return $output;
116
        }
117
118
        return $this->outputLines;
119
    }
120
}
121