Completed
Pull Request — develop (#147)
by
unknown
02:05
created

CallerSSH2::getBinaryPath()   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
 * 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
 * Caller via ssh2 PECL extension
24
 *
25
 * @author Matteo Giachino <[email protected]>
26
 */
27
class CallerSSH2 implements CallerInterface
28
{
29
    /**
30
     * @var resource
31
     */
32
    private $resource;
33
34
    /**
35
     * @var string
36
     */
37
    private $gitPath;
38
39
    /**
40
     * @var string
41
     */
42
    private $binaryVersion;
43
44
    /**
45
     * the output lines of the command
46
     *
47
     * @var array
48
     */
49
    private $outputLines = array();
50
51
    /**
52
     * @param resource $resource
53
     * @param string   $gitPath path of the git executable on the remote host
54
     *
55
     * @internal param string $host remote host
56
     * @internal param int $port remote port
57
     */
58
    public function __construct($resource, $gitPath = '/usr/bin/git')
59
    {
60
        $this->resource = $resource;
61
        $this->gitPath = $gitPath;
62
        // unix only
63
        $this->binaryVersion = $this->execute('--version | cut -d " " -f 3', true);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->execute('--versio...cut -d " " -f 3', true) of type object<GitElephant\Comma...Caller\CallerInterface> is incompatible with the declared type string of property $binaryVersion.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
    }
65
66
    /**
67
     * execute a command
68
     *
69
     * @param string      $cmd the command
70
     * @param bool        $git prepend git to the command
71
     * @param null|string $cwd directory where the command should be executed
72
     *
73
     * @return CallerInterface
74
     */
75
    public function execute($cmd, $git = true, $cwd = null)
76
    {
77
        if ($git) {
78
            $cmd = $this->gitPath . ' ' . $cmd;
79
        }
80
        $stream = ssh2_exec($this->resource, $cmd);
81
        stream_set_blocking($stream, 1);
82
        $data = stream_get_contents($stream);
83
        fclose($stream);
84
        // rtrim values
85
        $values = array_map('rtrim', explode(PHP_EOL, $data));
86
        $this->outputLines = $values;
87
88
        return $this;
89
    }
90
91
    /**
92
     * after calling execute this method should return the output
93
     *
94
     * @param bool $stripBlankLines strips the blank lines
95
     *
96
     * @return array
97
     */
98
    public function getOutputLines($stripBlankLines = false)
99
    {
100
        if ($stripBlankLines) {
101
            $output = array();
102
            foreach ($this->outputLines as $line) {
103
                if ('' !== $line) {
104
                    $output[] = $line;
105
                }
106
            }
107
108
            return $output;
109
        }
110
111
        return $this->outputLines;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function getBinaryPath()
118
    {
119
        return $this->gitPath;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function getBinaryVersion()
126
    {
127
        return $this->binaryVersion;
128
    }
129
}
130