Completed
Push — master ( e32077...7e7359 )
by Tõnis
02:36
created

QuickGit::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace andmemasin\helpers;
4
5
6
class QuickGit
7
{
8
    /** @var int */
9
    private $major = 1;
10
    /** @var int */
11
    private $minor = 0;
12
    /** @var string */
13
    private $patch = '';
14
    /** @var int */
15
    private $commits = 0;
16
    /** @var string */
17
    private $commit = '';
18
19
    /**
20
     * @method __construct
21
     */
22
    public function __construct()
23
    {
24
        // Collect git data.
25
        exec('git describe --always', $gitHashShort);
26
        $this->patch = $gitHashShort;
27
        exec('git rev-list HEAD | wc -l', $gitCommits);
28
        $this->commits = $gitCommits;
29
        exec('git log -1', $gitHashLong);
30
        $this->commit = $gitHashLong;
31
    }
32
    /**
33
     * @return string
34
     */
35
    public function toString($format = 'short')
36
    {
37
        switch ($format) {
38
            case 'long':
39
                return sprintf(
40
                    '%d.%d.%s (#%d, %s)',
41
                    $this->major,
42
                    $this->minor,
43
                    $this->patch,
44
                    $this->commits,
45
                    $this->commit
46
                );
47
            default:
48
                return sprintf(
49
                    '%d.%d.%s',
50
                    $this->major,
51
                    $this->minor,
52
                    $this->patch
53
                );
54
        }
55
    }
56
57
    /**
58
     * @method __toString
59
     */
60
    public function __toString()
61
    {
62
        return $this->toString();
63
    }
64
}