Completed
Push — master ( c1a738...2c88a4 )
by
unknown
02:35 queued 01:13
created

AbstractCaller::getRawOutput()   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|null
34
     */
35
    protected $binaryPath;
36
37
    /**
38
     * Git binary version
39
     *
40
     * @var string|null
41
     */
42
    protected $binaryVersion;
43
44
    /**
45
     * the output lines of the command
46
     *
47
     * @var array
48
     */
49
    protected $outputLines = [];
50
51
    /**
52
     * raw output of the command
53
     *
54
     * @var string
55
     */
56
    protected $rawOutput;
57
58
    /**
59
     * @inheritdoc
60
     */
61 104
    public function getBinaryPath(): string
62
    {
63 104
        return $this->binaryPath;
64
    }
65
66
    /**
67
     * path setter
68
     *
69
     * @param string $path the path to the system git binary
70
     */
71 107
    public function setBinaryPath(string $path): self
72
    {
73 107
        $this->binaryPath = $path;
74
75 107
        return $this;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 5
    public function getBinaryVersion(): string
82
    {
83 5
        if (is_null($this->binaryVersion)) {
84 5
            $this->execute('--version');
85 5
            $version = $this->getOutput();
86 5
            if (!preg_match('/^git version [0-9\.]+/', $version)) {
87
                throw new \RuntimeException('Could not parse git version. Unexpected format "' . $version . '".');
88
            }
89 5
            $this->binaryVersion = preg_replace('/^git version ([0-9\.]+)/', '$1', $version);
90
        }
91
92 5
        return $this->binaryVersion;
93
    }
94
95
    /**
96
     * returns the output of the last executed command
97
     *
98
     * @return string
99
     */
100
    public function getOutput(): string
101
    {
102
        return implode("\n", $this->outputLines);
103
    }
104
105
    /**
106
     * returns the output of the last executed command as an array of lines
107
     *
108
     * @param bool $stripBlankLines remove the blank lines
109
     *
110
     * @return array
111
     */
112
    public function getOutputLines(bool $stripBlankLines = false): array
113
    {
114
        if ($stripBlankLines) {
115
            $output = [];
116
            foreach ($this->outputLines as $line) {
117
                if ('' !== $line) {
118
                    $output[] = $line;
119
                }
120
            }
121
122
            return $output;
123
        }
124
125
        return $this->outputLines;
126
    }
127
128
    /**
129
     * Get RawOutput
130
     *
131
     * @return string
132
     */
133
    public function getRawOutput(): string
134
    {
135
        return $this->rawOutput;
136
    }
137
}
138