Completed
Pull Request — master (#397)
by De Cramer
02:34
created

Version::getExpansionVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use PackageVersions\Versions;
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Class Version
10
 *
11
 * @author    de Cramer Oliver<[email protected]>
12
 * @copyright 2018 Oliverde8
13
 * @package eXpansion\Framework\Core\Helpers
14
 */
15
class Version
16
{
17
    protected $expansionVersion = null;
18
19
    protected $expansionBranch = null;
20
21
    protected $expansionSha = null;
22
23
    /**
24
     * Get current expansion version.
25
     *
26
     * @return null|string
27
     */
28
    public function getExpansionVersion()
29
    {
30
        if (!is_null($this->expansionVersion)) {
31
            return $this->expansionVersion;
32
        }
33
34
        // First check if we are in a git directory. This would be the case if we are in a
35
        // full manual install(not recommended) or on a dev machine.
36
        $process = new Process('git --version');
37
        $process->run();
38
        if ($process->isSuccessful()) {
39
            $branch = $this->getBranch();
40
            $sha = $this->getSha();
41
            $this->expansionVersion = "2.0-$branch@$sha";
42
        } else {
43
            $this->expansionVersion = Versions::getVersion('expansion-mp/expansion');
44
        }
45
46
        return $this->expansionVersion;
47
    }
48
49
    /**
50
     * Get a shorten version of the expansion version.
51
     *
52
     * @return mixed
53
     */
54
    public function getShortExpansionVersion()
55
    {
56
        return explode('@', $this->getExpansionVersion())[0];
57
    }
58
59
    /**
60
     * Get current branch.
61
     *
62
     * @return null|string
63
     */
64 View Code Duplication
    protected function getBranch()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        if (is_null($this->expansionBranch)) {
67
            $process = new Process('git rev-parse --abbrev-ref HEAD');
68
            $process->run();
69
70
            $this->expansionBranch = trim($process->getOutput());
71
        }
72
73
        return $this->expansionBranch;
74
    }
75
76
    /**
77
     * Get current commit sha.
78
     *
79
     * @return null|string
80
     */
81 View Code Duplication
    protected function getSha()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        if (is_null($this->expansionSha)) {
84
            $process = new Process('git rev-parse HEAD');
85
            $process->run();
86
87
            $this->expansionSha = trim($process->getOutput());
88
        }
89
90
        return $this->expansionSha;
91
    }
92
}