Completed
Push — master ( 1e33b0...2a2836 )
by Pieter
02:53
created

Client::buildCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Gitilicious\GitClient\Cli;
4
5
use Gitilicious\GitClient\Cli\Input\Argument;
6
use Gitilicious\GitClient\Cli\Input\Binary;
7
use Gitilicious\GitClient\Cli\Output\Result;
8
use Gitilicious\GitClient\FileSystem\Directory;
9
10
class Client
11
{
12
    private $binary;
13
14 2
    public function __construct(Binary $binary)
15
    {
16 2
        $this->binary = $binary;
17 2
    }
18
19 2
    public function run(Directory $workingDirectory, Argument ...$arguments): Result
20
    {
21
        $descriptorSpec = [
22 2
            ['pipe', 'r'],
23
            ['pipe', 'w'],
24
            ['pipe', 'w'],
25
        ];
26
27 2
        $process = proc_open($this->buildCommand(...$arguments), $descriptorSpec, $pipes, $workingDirectory->getPath());
28
29 2
        $stdOut = stream_get_contents($pipes[1]);
30 2
        $stdErr = stream_get_contents($pipes[2]);
31
32 2
        foreach ($pipes as $pipe) {
33 2
            fclose($pipe);
34
        }
35
36 2
        return new Result(proc_close($process), $stdOut, $stdErr);
37
    }
38
39 2
    private function buildCommand(Argument ...$arguments): string
40
    {
41 2
        $fullCommand = [];
42
43 2
        foreach ($arguments as $argument) {
44 2
            $fullCommand[] = escapeshellarg($argument->getArgument());
45
        }
46
47 2
        return $this->binary->getExecutable() . ' ' . implode(' ', $fullCommand);
48
    }
49
}
50