Passed
Push — master ( 9c106a...7b4ed8 )
by Jay
05:53 queued 11s
created

ProjectVersion::getEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
/**
5
 * Class ProjectVersion
6
 * @package LogiCE
7
 */
8
class ProjectVersion
9
{
10
    private static $instance;
11
    private $structure;
12
13
    /**
14
     * @return $this
15
     */
16 2
    final public static function getInstance()
17
    {
18 2
        if (!self::$instance) {
19 1
            $class = get_called_class();
20 1
            self::$instance = new $class;
21 1
        }
22 2
        return self::$instance;
23
    }
24
25
    /**
26
     * private construct - singleton
27
     */
28 1
    private function __construct()
29
    {
30 1
    }
31
32
    /**
33
     * private clone - singleton
34
     * @codeCoverageIgnore
35
     */
36
    private function __clone()
37
    {
38
    }
39
40
    /**
41
     * Get version name
42
     * @return string
43
     */
44 6
    public function get()
45
    {
46 6
        $version = (string)$this->getEnv('PROJECT_VERSION');
47 6
        if ($version) {
48 1
            return $version;
49
        }
50
        try {
51 5
            if (isset($this->getStructure()->version)) {
52 1
                return $this->getStructure()->version;
53
            }
54 4
        } catch (\LogicException $e) {
55
            //Do nothing since we have a fallback
56
        }
57 4
        $rootPath = $this->getGitHeadFilePath();
58 4
        if (file_exists($rootPath)) {
59 1
            $stringFromFile = file($rootPath);
60 1
            $firstLine = $stringFromFile[0]; //get the string from the array
61 1
            $explodedString = explode("/", $firstLine, 3); //seperate out by the "/" in the string
62 1
            return trim($explodedString[2]); //get the one that is always the branch name
63
        }
64 3
        return 'v0.0.0';
65
    }
66
67
    /**
68
     * @return string
69
     * @codeCoverageIgnore
70
     */
71
    protected function getGitHeadFilePath()
72
    {
73
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', '.git', 'HEAD'));
74
    }
75
76
    /**
77
     * Retrieve environment variable
78
     * @param string $name
79
     * @return array|false|string
80
     * @codeCoverageIgnore
81
     */
82
    protected function getEnv($name)
83
    {
84
        return getenv($name);
85
    }
86
87 1
    public function name()
88
    {
89 1
        return $this->getStructure()->name;
90
    }
91
92
    /**
93
     * Return composer.json path to project
94
     * @return string
95
     */
96 1
    protected function getComposerPath()
97
    {
98 1
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.json'));
99
    }
100
101
    /**
102
     * Get composer file structure
103
     * @return Object
104
     */
105 3 View Code Duplication
    protected function getStructure()
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...
106
    {
107 3
        if (!$this->structure) {
108 3
            if (!is_file($this->getComposerPath())) {
109 1
                throw new \LogicException('composer.json does not exist');
110
            }
111 2
            $this->structure = json_decode(file_get_contents($this->getComposerPath()));
112 2
            if (!$this->structure) {
113 1
                throw new \LogicException('composer.json invalid structure');
114
            }
115 1
        }
116 1
        return $this->structure;
117
    }
118
}
119