Version::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
/**
5
 * Retrieve FMUP Version
6
 * @package FMUP
7
 */
8
class Version
9
{
10
    private static $instance;
11
    private $structure;
12
13
    /**
14
     * @return $this
15
     */
16 3
    final public static function getInstance()
17
    {
18 3
        if (!self::$instance) {
19 1
            $class = get_called_class();
20 1
            self::$instance = new $class;
21
        }
22 3
        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 4
    public function get()
45
    {
46 4
        foreach ($this->getStructure()->packages as $package) {
47 2
            if ($package->name == 'fmup/fmup') {
48 2
                return $package->version;
49
            }
50
        }
51 1
        return "v0.0.0";
52
    }
53
54
    /**
55
     * Get composer json path
56
     * @return string
57
     * @codeCoverageIgnore
58
     */
59
    protected function getComposerPath()
60
    {
61
        return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', '..', '..', 'composer.lock'));
62
    }
63
64
    /**
65
     * Get composer file structure
66
     * @return Object
67
     * @throws Exception
68
     */
69 4 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...
70
    {
71 4
        if (!$this->structure) {
72 4
            if (!is_file($this->getComposerPath())) {
73 1
                throw new Exception('composer.lock does not exist');
74
            }
75 3
            $this->structure = json_decode(file_get_contents($this->getComposerPath()));
76 3
            if (!$this->structure) {
77 1
                throw new Exception('composer.lock invalid structure');
78
            }
79
        }
80 2
        return $this->structure;
81
    }
82
}
83