AddGitInformation   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 1
A hash() 0 4 1
A message() 0 4 1
A tag() 0 4 1
A remote() 0 4 1
A isClean() 0 4 1
A command() 0 10 2
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