Completed
Push — develop ( b29d0d...9c2be0 )
by
unknown
16s queued 11s
created

AbstractCaller::getOutputLines()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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