Call::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/*
3
 * Copyright (C) 2017 by TEQneers GmbH & Co. KG
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
/**
25
 * Git Stream Wrapper for PHP
26
 *
27
 * @category   TQ
28
 * @package    TQ_VCS
29
 * @subpackage VCS
30
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
31
 */
32
33
namespace TQ\Vcs\Cli;
34
35
/**
36
 * Represents a single CLI call
37
 *
38
 * @author     Stefan Gehrig <gehrigteqneers.de>
39
 * @category   TQ
40
 * @package    TQ_VCS
41
 * @subpackage VCS
42
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
43
 */
44
class Call
45
{
46
    /**
47
     * The CLI command to execute
48
     *
49
     * @var string
50
     */
51
    protected $cmd;
52
53
    /**
54
     * The working directory in which the call will be executed
55
     *
56
     * @var string|null
57
     */
58
    protected $cwd;
59
60
    /**
61
     * Environment variables - defaults to the current environment
62
     *
63
     * @var array|null
64
     */
65
    protected $env;
66
67
    /**
68
     * Factory method to create a call
69
     *
70
     * @param   string      $cmd    The CLI command to execute
71
     * @param   string|null $cwd    The working directory in which the call will be executed
72
     * @param   array|null  $env    Environment variables - defaults to the current environment
73
     * @return  static
74
     */
75 175
    public static function create($cmd, $cwd = null, array $env = null)
76
    {
77 175
        return new static($cmd, $cwd, $env);
78
    }
79
80
    /**
81
     * Creates a new instance of a CLI call
82
     *
83
     * @param   string      $cmd    The CLI command to execute
84
     * @param   string|null $cwd    The working directory in which the call will be executed
85
     * @param   array|null  $env    Environment variables - defaults to the current environment
86
     */
87 175
    public function __construct($cmd, $cwd = null, array $env = null)
88
    {
89 175
        $this->setCmd($cmd);
90 175
        $this->setCwd($cwd);
91 175
        $this->setEnv($env);
92 175
    }
93
94
    /**
95
     * Returns the CLI command
96
     *
97
     * @return  string
98
     */
99 175
    public function getCmd()
100
    {
101 175
        return $this->cmd;
102
    }
103
104
    /**
105
     * Sets the CLI command
106
     *
107
     * @param   string  $cmd    The CLI command to execute
108
     * @return  Call
109
     */
110 175
    public function setCmd($cmd)
111
    {
112 175
        $this->cmd  = (string)$cmd;
113 175
        return $this;
114
    }
115
116
    /**
117
     * Returns the working directory for the call
118
     *
119
     * @return  string|null
120
     */
121 157
    public function getCwd()
122
    {
123 157
        return $this->cwd;
124
    }
125
126
    /**
127
     * Sets the working directory for the call
128
     *
129
     * @param   string|null  $cwd   The working directory in which the call will be executed
130
     * @return  Call
131
     */
132 175
    public function setCwd($cwd)
133
    {
134 175
        if (empty($cwd)) {
135
            $cwd    = null;
136
        } else {
137 175
            $cwd    = (string)$cwd;
138
        }
139 175
        $this->cwd  = $cwd;
140 175
        return $this;
141
    }
142
143
    /**
144
     * Returns the environment variables for the call - if overridden
145
     *
146
     * @return  array|null
147
     */
148 157
    public function getEnv()
149
    {
150 157
        return $this->env;
151
    }
152
153
    /**
154
     * Sets the environment variables for the call
155
     *
156
     * @param   array|null  $env    Environment variables - defaults to the current environment
157
     * @return  Call
158
     */
159 175
    public function setEnv(array $env = null)
160
    {
161 175
        $this->env  = $env;
162 175
        return $this;
163
    }
164
165
    /**
166
     * Executes the call using the preconfigured command
167
     *
168
     * @param   string|null  $stdIn     Content that will be piped to the command
169
     * @return  CallResult
170
     * @throws  \RuntimeException       If the command cannot be executed
171
     */
172 157
    public function execute($stdIn = null)
173
    {
174 157
        $stdOut = fopen('php://temp', 'r');
175 157
        $stdErr = fopen('php://temp', 'r');
176
177
        $descriptorSpec = array(
178 157
           0 => array("pipe", "r"), // stdin is a pipe that the child will read from
179 157
           1 => $stdOut,            // stdout is a temp file that the child will write to
180 157
           2 => $stdErr             // stderr is a temp file that the child will write to
181
        );
182 157
        $pipes   = array();
183 157
        $process = proc_open(
184 157
            $this->getCmd(),
185 157
            $descriptorSpec,
186 157
            $pipes,
187 157
            $this->getCwd(),
188 157
            $this->getEnv()
189
        );
190
191 157
        if (is_resource($process)) {
192 157
            if ($stdIn !== null) {
193 6
                fwrite($pipes[0], (string)$stdIn);
194
            }
195 157
            fclose($pipes[0]);
196 157
            $returnCode = proc_close($process);
197 157
            return new CallResult($this, $stdOut, $stdErr, $returnCode);
198
        } else {
199
            fclose($stdOut);
200
            fclose($stdErr);
201
            throw new \RuntimeException(sprintf('Cannot execute "%s"', $this->getCmd()));
202
        }
203
    }
204
}
205
206