Completed
Pull Request — master (#156)
by
unknown
02:24
created

GitProcess   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 104
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 3
A start() 0 10 2
A wait() 0 27 5
A dispatchGitEvent() 0 7 1
A resolveWorkingDirectory() 0 12 4
1
<?php declare(strict_types=1);
2
3
namespace GitWrapper;
4
5
use GitWrapper\Event\GitEvent;
6
use GitWrapper\Event\GitEvents;
7
use RuntimeException;
8
use Symfony\Component\Process\Process;
9
10
final class GitProcess extends Process
11
{
12
    /**
13
     * @var GitWrapper
14
     */
15
    protected $gitWrapper;
16
17
    /**
18
     * @var GitCommand
19
     */
20
    protected $gitCommand;
21
22
    public function __construct(GitWrapper $gitWrapper, GitCommand $gitCommand, ?string $cwd = null)
23
    {
24
        $this->gitWrapper = $gitWrapper;
25
        $this->gitCommand = $gitCommand;
26
27
        // Build the command line options, flags, and arguments.
28
        $commandLine = $gitCommand->getCommandLine();
29
        $gitBinary = $gitWrapper->getGitBinary();
30
        if (is_string($commandLine)) {
31
            // Support for executing an arbitrary git command.
32
            $commandLine = '"' . $gitBinary . '" ' . $commandLine;
33
        } else {
34
            array_unshift($commandLine, $gitBinary);
35
        }
36
37
        // Resolve the working directory of the Git process. Use the directory
38
        // in the command object if it exists.
39
        $cwd = $this->resolveWorkingDirectory($cwd, $gitCommand);
40
41
        // Finalize the environment variables, an empty array is converted
42
        // to null which enherits the environment of the PHP process.
43
        $env = $gitWrapper->getEnvVars();
44
        if (! $env) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $env of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
            $env = null;
46
        }
47
48
        parent::__construct($commandLine, $cwd, $env, null, (float) $gitWrapper->getTimeout());
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function start(?callable $callback = null, array $env = []): void
55
    {
56
        $this->dispatchGitEvent(GitEvents::GIT_PREPARE);
57
58
        if ($this->gitCommand->notBypassed()) {
59
            parent::start($callback, $env);
60
        } else {
61
            $this->dispatchGitEvent(GitEvents::GIT_BYPASS);
62
        }
63
    }
64
65
    public function wait(?callable $callback = null): int
66
    {
67
        if (! $this->gitCommand->notBypassed()) {
68
            return -1;
69
        }
70
71
        try {
72
            $exitCode = parent::wait($callback);
73
74
            if ($this->isSuccessful()) {
75
                $this->dispatchGitEvent(GitEvents::GIT_SUCCESS);
76
            } else {
77
                $output = $this->getErrorOutput();
78
79
                if (trim($output) === '') {
80
                    $output = $this->getOutput();
81
                }
82
83
                throw new GitException($output);
84
            }
85
        } catch (RuntimeException $runtimeException) {
86
            $this->dispatchGitEvent(GitEvents::GIT_ERROR);
87
            throw new GitException($runtimeException->getMessage(), $runtimeException->getCode(), $runtimeException);
88
        }
89
90
        return $exitCode;
91
    }
92
93
    private function dispatchGitEvent(string $eventName): void
94
    {
95
        $this->gitWrapper->getDispatcher()->dispatch(
96
            $eventName,
97
            new GitEvent($this->gitWrapper, $this, $this->gitCommand)
98
        );
99
    }
100
101
    private function resolveWorkingDirectory(?string $cwd, GitCommand $gitCommand): ?string
102
    {
103
        if ($cwd === null) {
104
            $directory = $gitCommand->getDirectory();
105
            if ($directory !== null) {
106
                if (! $cwd = realpath($directory)) {
107
                    throw new GitException('Path to working directory could not be resolved: ' . $directory);
108
                }
109
            }
110
        }
111
        return $cwd;
112
    }
113
}
114