Passed
Pull Request — develop (#1)
by Jimmy
03:57
created

Version::__toString()   A

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
3
namespace Spinen\Version;
4
5
use Illuminate\Support\Facades\Config;
6
7
/**
8
 * Class Version
9
 *
10
 * Parse the version file into the parts
11
 *
12
 * @package App\Support
13
 */
14
class Version
15
{
16
    /**
17
     * Major version
18
     *
19
     * @var integer|null
20
     */
21
    public $major = null;
22
23
    /**
24
     * Meta
25
     *
26
     * Extra data about the version with dots separating key data and underscores for spaces
27
     *
28
     * @var string|null
29
     */
30
    public $meta = null;
31
32
    /**
33
     * Minor version
34
     *
35
     * @var integer|null
36
     */
37
    public $minor = null;
38
39
    /**
40
     * Patch version
41
     *
42
     * @var integer|null
43
     */
44
    public $patch = null;
45
46
    /**
47
     * Pre release
48
     *
49
     * This will most likely be the branch version, unless we are on the master branch
50
     *
51
     * @var string|null
52
     */
53
    public $pre_release = null;
54
55
    /**
56
     * Version in SemVer format
57
     *
58
     * @var string
59
     */
60
    public $semver;
61
62
    /**
63
     * The version of the application
64
     *
65
     * @var string
66
     */
67
    public $version = "UNDEFINED";
68
69
    /**
70
     * The file that holds the version info
71
     *
72
     * @var string
73
     */
74
    protected $version_file;
75
76
    /**
77
     * Version constructor.
78
     *
79
     * @param string|null $file
80
     */
81 10
    public function __construct($file = null)
82
    {
83 10
        $this->version_file = $file ? : base_path(Config::get('version.file', 'VERSION'));
84
85 10
        $this->parseVersionFile();
86
87 10
        $this->generateSemVer();
88
    }
89
90
    /**
91
     * If the object is used as a string, then return the full version
92
     *
93
     * @return string
94
     */
95 8
    public function __toString()
96
    {
97 8
        return $this->semver;
98
    }
99
100
    /**
101
     * Put together version parts into the SemVer format
102
     */
103 10
    protected function generateSemVer()
104
    {
105 10
        $this->semver = $this->version;
106
107 10
        if ($this->pre_release) {
108 4
            $this->semver .= '-' . $this->pre_release;
109
        }
110
111 10
        if ($this->meta) {
112 4
            $this->semver .= '+' . $this->meta;
113
        }
114
    }
115
116
    /**
117
     * If the read in line matches a version, then parse it
118
     *
119
     * @param string $version
120
     *
121
     * @return bool
122
     */
123 8
    protected function parseVersion($version)
124
    {
125 8
        if (!preg_match('/\\d+\\.\\d+\\.\\d+/u', $version)) {
126 1
            return false;
127
        }
128
129 7
        $this->version = $version;
130
131 7
        list($this->major, $this->minor, $this->patch) = array_map('intval', explode(".", $this->version));
132
133 7
        return true;
134
    }
135
136
    /**
137
     * Read the version file into the various parts
138
     */
139 10
    protected function parseVersionFile()
140
    {
141 10
        if (!file_exists($this->version_file)) {
142 1
            return;
143
        }
144
145
        // Read file in as an array & remove any empty lines
146 9
        $version_parts = array_filter(explode("\n", @file_get_contents($this->version_file)));
147
148
        // First line is the version
149 9
        if (empty($version_parts) or !$this->parseVersion(array_shift($version_parts))) {
150 2
            return;
151
        }
152
153
        // Next line is branch/pre release
154 7
        $pre_release = array_shift($version_parts);
155
156 7
        $this->pre_release = ($pre_release !== 'master') ? $pre_release : null;
157
158
        // Is there anything left in the file for meta?
159 7
        if (empty($version_parts)) {
160 3
            return;
161
        }
162
163
        // Everything left is the meta, so concatenate it with .'s & replace the spaces with _'s
164 4
        $this->meta = preg_replace("/\\s+/u", "_", implode('.', $version_parts));
165
    }
166
}
167