Completed
Pull Request — develop (#100)
by
unknown
03:44
created

Caller::getOutputLines()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
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
     * Class constructor
63
     *
64
     * @param \GitElephant\GitBinary $binary         the binary
65
     * @param string                 $repositoryPath the physical base path for the repository
66
     */
67 103
    public function __construct(GitBinary $binary, $repositoryPath)
68
    {
69 103
        $this->binary         = $binary;
70
71 103
        if (!is_dir($repositoryPath)) {
72 1
            throw new InvalidRepositoryPathException($repositoryPath);
73
        }
74
75 103
        $this->repositoryPath = $repositoryPath;
76 103
    }
77
78
    /**
79
     * Get the binary path
80
     *
81
     * @return mixed
82
     */
83 1
    public function getBinaryPath()
84
    {
85 1
        return $this->binary->getPath();
86
    }
87
88
    /**
89
     * Executes a command
90
     *
91
     * @param string $cmd               the command to execute
92
     * @param bool   $git               if the command is git or a generic command
93
     * @param null   $cwd               the directory where the command must be executed
94
     * @param array  $acceptedExitCodes exit codes accepted to consider the command execution successful
95
     *
96
     * @throws \RuntimeException
97
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
98
     * @throws \Symfony\Component\Process\Exception\ProcessTimedOutException
99
     * @throws \Symfony\Component\Process\Exception\RuntimeException
100
     * @throws \Symfony\Component\Process\Exception\LogicException
101
     * @return Caller
102
     */
103 97
    public function execute($cmd, $git = true, $cwd = null, $acceptedExitCodes = array(0))
104
    {
105 97
        if ($git) {
106 97
            $cmd = $this->binary->getPath() . ' ' . $cmd;
107 97
        }
108
109 97
        $process = new Process($cmd, is_null($cwd) ? $this->repositoryPath : $cwd);
110 97
        $process->setTimeout(15000);
111 97
        $process->run();
112 97
        if (!in_array($process->getExitCode(), $acceptedExitCodes)) {
113 1
            $text = 'Exit code: ' . $process->getExitCode();
114 1
            $text .= ' while executing: "' . $cmd;
115 1
            $text .= '" with reason: ' . $process->getErrorOutput();
116 1
            $text .= "\n" . $process->getOutput();
117 1
            throw new \RuntimeException($text);
118
        }
119 96
        $this->rawOutput = $process->getOutput();
120
        // rtrim values
121 96
        $values = array_map('rtrim', explode(PHP_EOL, $process->getOutput()));
122 96
        $this->outputLines = $values;
123
124 96
        return $this;
125
    }
126
127
    /**
128
     * returns the raw output of the last executed command
129
     *
130
     * @return string
131
     */
132 2
    public function getOutput()
133
    {
134 2
        return implode(" ", $this->outputLines);
135
    }
136
137
    /**
138
     * returns the output of the last executed command as an array of lines
139
     *
140
     * @param bool $stripBlankLines remove the blank lines
141
     *
142
     * @return array
143
     */
144 89
    public function getOutputLines($stripBlankLines = false)
145
    {
146 89
        if ($stripBlankLines) {
147 80
            $output = array();
148 80
            foreach ($this->outputLines as $line) {
149 80
                if ('' !== $line) {
150 80
                    $output[] = $line;
151 80
                }
152 80
            }
153
154 80
            return $output;
155
        }
156
157 43
        return $this->outputLines;
158
    }
159
160
    /**
161
     * Get RawOutput
162
     *
163
     * @return string
164
     */
165 1
    public function getRawOutput()
166
    {
167 1
        return $this->rawOutput;
168
    }
169
}
170