Completed
Pull Request — master (#150)
by Sullivan
01:15
created

GitProcess::wait()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 10
nop 1
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
        $gitCommandLine = $gitCommand->getCommandLine();
29
        $commandLine = array_merge([$gitWrapper->getGitBinary()], (array) $gitCommandLine);
30
31
        // Support for executing an arbitrary git command.
32
        if (is_string($gitCommandLine)) {
33
            $commandLine = implode(' ', $commandLine);
34
        }
35
36
        // Resolve the working directory of the Git process. Use the directory
37
        // in the command object if it exists.
38
        if ($cwd === null) {
39
            $directory = $gitCommand->getDirectory();
40
            if ($directory !== null) {
41
                if (! $cwd = realpath($directory)) {
42
                    throw new GitException('Path to working directory could not be resolved: ' . $directory);
43
                }
44
            }
45
        }
46
47
        // Finalize the environment variables, an empty array is converted
48
        // to null which enherits the environment of the PHP process.
49
        $env = $gitWrapper->getEnvVars();
50
        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...
51
            $env = null;
52
        }
53
54
        parent::__construct($commandLine, $cwd, $env, null, (float) $gitWrapper->getTimeout());
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function start(?callable $callback = null, array $env = [])
61
    {
62
        // Throw the "git.command.prepare" event prior to executing.
63
        $this->dispatchGitEvent(GitEvents::GIT_PREPARE);
64
65
        // Execute command if it is not flagged to be bypassed and throw the
66
        // "git.command.success" event, otherwise do not execute the command
67
        // and throw the "git.command.bypass" event.
68
        if ($this->gitCommand->notBypassed()) {
69
            parent::start($callback, $env);
70
        } else {
71
            $this->dispatchGitEvent(GitEvents::GIT_BYPASS);
72
        }
73
    }
74
75
    public function wait(?callable $callback = null): int
76
    {
77
        if ($this->gitCommand->notBypassed()) {
78
            try {
79
                $exitCode = parent::wait($callback);
80
81
                if ($this->isSuccessful()) {
82
                    $this->dispatchGitEvent(GitEvents::GIT_SUCCESS);
83
                } else {
84
                    $output = $this->getErrorOutput();
85
86
                    if (trim($output) === '') {
87
                        $output = $this->getOutput();
88
                    }
89
90
                    throw new GitException($output);
91
                }
92
            } catch (RuntimeException $runtimeException) {
93
                $this->dispatchGitEvent(GitEvents::GIT_ERROR);
94
                throw new GitException($runtimeException->getMessage(), $runtimeException->getCode(), $runtimeException);
95
            }
96
97
            return $exitCode;
98
        }
99
100
        return -1;
101
    }
102
103
    private function dispatchGitEvent(string $eventName)
104
    {
105
        $this->gitWrapper->getDispatcher()->dispatch(
106
            $eventName,
107
            new GitEvent($this->gitWrapper, $this, $this->gitCommand)
108
        );
109
    }
110
}
111