SemVerManager::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of questocat/version-comparator package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Questocat\VersionComparator;
13
14
class SemVerManager
15
{
16
    /**
17
     * The Version instance.
18
     *
19
     * @var Version
20
     */
21
    protected $version;
22
23
    /**
24
     * The VersionComparator instance.
25
     *
26
     * @var VersionComparator
27
     */
28
    protected $versionComparator;
29
30
    /**
31
     * SemVerManager construct.
32
     *
33
     * @param string $versionStr
34
     */
35 3
    public function __construct($versionStr = null)
36
    {
37 3
        $this->setVersion(new Version());
38 3
        $this->setVersionComparator(new VersionComparator());
39
40 3
        if (!is_null($versionStr)) {
41 3
            $this->mapVersionObject($versionStr);
42 3
        }
43 3
    }
44
45
    /**
46
     * Set version.
47
     *
48
     * @param Version $version
49
     *
50
     * @return SemVerManager
51
     */
52 3
    public function setVersion(Version $version)
53
    {
54 3
        $this->version = $version;
55
56 3
        return $this;
57
    }
58
59
    /**
60
     * Get version.
61
     *
62
     * @return Version
63
     */
64 3
    public function getVersion()
65
    {
66 3
        return $this->version;
67
    }
68
69
    /**
70
     * Set VersionComparator.
71
     *
72
     * @param VersionComparator $versionComparator
73
     *
74
     * @return SemVerManager
75
     */
76 3
    public function setVersionComparator(VersionComparator $versionComparator)
77
    {
78 3
        $this->versionComparator = $versionComparator;
79
80 3
        return $this;
81
    }
82
83
    /**
84
     * Compares one version number string to another.
85
     *
86
     * @param string      $versionStr
87
     * @param string|null $operator
88
     *
89
     * @return int|bool
90
     */
91 1
    public function compareTo($versionStr, $operator = null)
92
    {
93 1
        $version = static::create($versionStr)->getVersion();
94 1
        $compare = $this->versionComparator->versionCompare($this->version, $version);
95
96 1
        return $operator ? $this->versionComparator->returnBool($compare, $operator) : $compare;
97
    }
98
99
    /**
100
     * Compares two version number strings.
101
     *
102
     * @param string      $version1
103
     * @param string      $version2
104
     * @param string|null $operator
105
     *
106
     * @return int|bool
107
     */
108 2
    public function compare($version1, $version2, $operator = null)
109
    {
110 2
        $version1 = static::create($version1)->getVersion();
111 2
        $version2 = static::create($version2)->getVersion();
112 2
        $compare = $this->versionComparator->versionCompare($version1, $version2);
113
114 2
        return $operator ? $this->versionComparator->returnBool($compare, $operator) : $compare;
115
    }
116
117
    /**
118
     * Map the parse's raw to Semantic Version object.
119
     *
120
     * @param string $versionStr
121
     */
122 3
    protected function mapVersionObject($versionStr)
123
    {
124 3
        $version = VersionParser::parse($versionStr);
125
126 3
        $this->version->map($version);
127 3
    }
128
129
    /**
130
     * Creates a new Version instance.
131
     *
132
     * @param string $versionStr
133
     *
134
     * @return VersionCompare
135
     */
136 3
    protected static function create($versionStr)
137
    {
138 3
        return new static($versionStr);
139
    }
140
}
141