AddGitInformation::command()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Facade\Ignition\Middleware;
4
5
use Facade\FlareClient\Report;
6
use Symfony\Component\Process\Process;
7
8
class AddGitInformation
9
{
10
    public function handle(Report $report, $next)
11
    {
12
        $report->group('git', [
13
            'hash' => $this->hash(),
14
            'message' => $this->message(),
15
            'tag' => $this->tag(),
16
            'remote' => $this->remote(),
17
            'isDirty' => ! $this->isClean(),
18
        ]);
19
20
        return $next($report);
21
    }
22
23
    public function hash(): ?string
24
    {
25
        return $this->command("git log --pretty=format:'%H' -n 1");
26
    }
27
28
    public function message(): ?string
29
    {
30
        return $this->command("git log --pretty=format:'%s' -n 1");
31
    }
32
33
    public function tag(): ?string
34
    {
35
        return $this->command('git describe --tags --abbrev=0');
36
    }
37
38
    public function remote(): ?string
39
    {
40
        return $this->command('git config --get remote.origin.url');
41
    }
42
43
    public function isClean(): bool
44
    {
45
        return empty($this->command('git status -s'));
46
    }
47
48
    protected function command($command)
49
    {
50
        $process = (new \ReflectionClass(Process::class))->hasMethod('fromShellCommandline')
51
            ? Process::fromShellCommandline($command, base_path())
52
            : new Process($command, base_path());
53
54
        $process->run();
55
56
        return trim($process->getOutput());
57
    }
58
}
59