Completed
Pull Request — develop (#135)
by
unknown
12:31 queued 07:26
created

Caller::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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
use GitElephant\Exception\InvalidRepositoryPathException;
23
use \GitElephant\GitBinary;
24
use \Symfony\Component\Process\Process;
25
26
/**
27
 * Caller
28
 *
29
 * @author Matteo Giachino <[email protected]>
30
 */
31
class Caller implements CallerInterface
32
{
33
    /**
34
     * GitBinary instance
35
     *
36
     * @var \GitElephant\GitBinary
37
     */
38
    private $binary;
39
40
    /**
41
     * the repository path
42
     *
43
     * @var string
44
     */
45
    private $repositoryPath;
46
47
    /**
48
     * the output lines of the command
49
     *
50
     * @var array
51
     */
52
    private $outputLines = array();
53
54
    /**
55
     * raw output
56
     *
57
     * @var string
58
     */
59
    private $rawOutput;
60
61
    /**
62
     * the output lines of the command
63
     *
64
     * @var array
65
     */
66
    private $errorLines = array();
67
68
    /**
69
     * raw output
70
     *
71
     * @var string
72
     */
73
    private $rawErrorOutput;
74
75
    /**
76
     * Class constructor
77
     *
78
     * @param \GitElephant\GitBinary $binary         the binary
79
     * @param string                 $repositoryPath the physical base path for the repository
80
     */
81 105
    public function __construct(GitBinary $binary, $repositoryPath)
82
    {
83 105
        $this->binary         = $binary;
84
85 105
        if (!is_dir($repositoryPath)) {
86 1
            throw new InvalidRepositoryPathException($repositoryPath);
87
        }
88
89 105
        $this->repositoryPath = $repositoryPath;
90 105
    }
91
92
    /**
93
     * Get the binary path
94
     *
95
     * @return mixed
96
     */
97 1
    public function getBinaryPath()
98
    {
99 1
        return $this->binary->getPath();
100
    }
101
102
    /**
103
     * Executes a command
104
     *
105
     * @param string $cmd               the command to execute
106
     * @param bool   $git               if the command is git or a generic command
107
     * @param null   $cwd               the directory where the command must be executed
108
     * @param array  $acceptedExitCodes exit codes accepted to consider the command execution successful
109
     *
110
     * @throws \RuntimeException
111
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
112
     * @throws \Symfony\Component\Process\Exception\ProcessTimedOutException
113
     * @throws \Symfony\Component\Process\Exception\RuntimeException
114
     * @throws \Symfony\Component\Process\Exception\LogicException
115
     * @return Caller
116
     */
117 99
    public function execute($cmd, $git = true, $cwd = null, $acceptedExitCodes = array(0))
118
    {
119 99
        if ($git) {
120 99
            $cmd = $this->binary->getPath() . ' ' . $cmd;
121 99
        }
122
123 99
        $process = new Process($cmd, is_null($cwd) ? $this->repositoryPath : $cwd);
124 99
        $process->setTimeout(15000);
125 99
        $process->run();
126 99
        if (!in_array($process->getExitCode(), $acceptedExitCodes)) {
127 1
            $text = 'Exit code: ' . $process->getExitCode();
128 1
            $text .= ' while executing: "' . $cmd;
129 1
            $text .= '" with reason: ' . $process->getErrorOutput();
130 1
            $text .= "\n" . $process->getOutput();
131 1
            throw new \RuntimeException($text);
132
        }
133 98
        $this->rawOutput = $process->getOutput();
134
        // rtrim values
135 98
        $values = array_map('rtrim', explode(PHP_EOL, $process->getOutput()));
136 98
        $this->outputLines = $values;
137
138 98
        return $this;
139
    }
140
141
    /**
142
     * returns the raw output of the last executed command
143
     *
144
     * @return string
145
     */
146 2
    public function getOutput()
147
    {
148 2
        return implode(" ", $this->outputLines);
149
    }
150
151
    /**
152
     * returns the output of the last executed command as an array of lines
153
     *
154
     * @param bool $stripBlankLines remove the blank lines
155
     *
156
     * @return array
157
     */
158 91
    public function getOutputLines($stripBlankLines = false)
159
    {
160 91
        if ($stripBlankLines) {
161 81
            $output = array();
162 81
            foreach ($this->outputLines as $line) {
163 81
                if ('' !== $line) {
164 81
                    $output[] = $line;
165 81
                }
166 81
            }
167
168 81
            return $output;
169
        }
170
171 45
        return $this->outputLines;
172
    }
173
174
    /**
175
     * Get RawOutput
176
     *
177
     * @return string
178
     */
179 1
    public function getRawOutput()
180
    {
181 1
        return $this->rawOutput;
182
    }
183
184
    /**
185
     * returns the error output of the last executed command
186
     *
187
     * @return string
188
     */
189
    public function getErrorOutput()
190
    {
191
        return implode("\n", $this->errorLines);
192
    }
193
194
    /**
195
     * returns the error output of the last executed command as an array of lines
196
     *
197
     * @param bool $stripBlankLines remove the blank lines
198
     *
199
     * @return array
200
     */
201
    public function getErrorLines($stripBlankLines = false)
202
    {
203
        if ($stripBlankLines) {
204
            $output = array();
205
            foreach ($this->errorLines as $line) {
206
                if ('' !== $line) {
207
                    $output[] = $line;
208
                }
209
            }
210
211
            return $output;
212
        }
213
214
        return $this->errorLines;
215
    }
216
217
    /**
218
     * Get RawErrorOutput
219
     *
220
     * @return string
221
     */
222
    public function getRawErrorOutput()
223
    {
224
        return $this->rawErrorOutput;
225
    }
226
}
227