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

Client   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 40
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 19 2
A buildCommand() 0 10 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