Passed
Pull Request — master (#211)
by Alex
03:41
created

Version::raiseVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace POData\Common;
4
5
/**
6
 * Class Version Type to represents the version number of data service and edmx.
7
 */
8
class Version
9
{
10
    /**
11
     * The major component of the version.
12
     *
13
     * @var int
14
     */
15
    private $major;
16
17
    /**
18
     * The minor component of the version.
19
     *
20
     * @var int
21
     */
22
    private $minor;
23
24
    /**
25
     * Constructs a new instance of Version class.
26
     *
27
     * @param int $major The major component of the version
28
     * @param int $minor The minor component of the version
29
     */
30
    public function __construct($major, $minor)
31
    {
32
        $this->major = $major;
33
        $this->minor = $minor;
34
    }
35
36
    /**
37
     * Gets the major component of the version.
38
     *
39
     * @return int
40
     */
41
    public function getMajor()
42
    {
43
        return $this->major;
44
    }
45
46
    /**
47
     * Gets the minor component of the version.
48
     *
49
     * @return int
50
     */
51
    public function getMinor()
52
    {
53
        return $this->minor;
54
    }
55
56
    /**
57
     * If necessary raises version to the version given.
58
     *
59
     * @param int $major The major component of the new version
60
     * @param int $minor The minor component of the new version
61
     *
62
     * @return bool true if the version was raised, false otherwise
63
     */
64
    public function raiseVersion($major, $minor)
65
    {
66
        if ($major > $this->major) {
67
            $this->major = $major;
68
            $this->minor = $minor;
69
70
            return true;
71
        } elseif ($major == $this->major && $minor > $this->minor) {
72
            $this->minor = $minor;
73
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Compare this version with a target version.
82
     *
83
     * @param Version $targetVersion The target version to compare with
84
     *
85
     * @return int Return 1 if this version is greater than target version
86
     *             -1 if this version is less than the target version
87
     *             0 if both are equal
88
     */
89
    public function compare(Version $targetVersion)
90
    {
91
        if ($this->major > $targetVersion->major) {
92
            return 1;
93
        }
94
95
        if ($this->major == $targetVersion->major) {
96
            if ($this->minor == $targetVersion->minor) {
97
                return 0;
98
            }
99
100
            if ($this->minor > $targetVersion->minor) {
101
                return 1;
102
            }
103
        }
104
105
        return -1;
106
    }
107
108
    /**
109
     * Gets the value of the current Version object as string.
110
     *
111
     * @return string
112
     */
113
    public function toString()
114
    {
115
        return $this->major . '.' . $this->minor;
116
    }
117
118
    //Is there a better way to do static const of complex type?
119
120
    /** @var Version[] */
121
    private static $fixedVersion = null;
122
123
    private static function fillVersions()
124
    {
125
        if (null == self::$fixedVersion) {
126
            self::$fixedVersion = [1 => new self(1, 0), 2 => new self(2, 0), 3 => new self(3, 0)];
127
        }
128
    }
129
130
    public static function v1()
131
    {
132
        self::fillVersions();
133
134
        return self::$fixedVersion[1];
135
    }
136
137
    public static function v2()
138
    {
139
        self::fillVersions();
140
141
        return self::$fixedVersion[2];
142
    }
143
144
    public static function v3()
145
    {
146
        self::fillVersions();
147
148
        return self::$fixedVersion[3];
149
    }
150
151
    public static function reset()
152
    {
153
        self::$fixedVersion = null;
154
    }
155
}
156