Completed
Push — master ( d66b80...474a37 )
by Konrad
05:52
created

VersionHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
C compareVersions() 0 30 14
1
<?php
2
3
namespace Xima\DepmonBundle\Util;
4
5
use vierbergenlars\SemVer\SemVerException;
6
use vierbergenlars\SemVer\version;
7
use vierbergenlars\SemVer\expression;
8
9
class VersionHelper
10
{
11
12
13
    /**
14
     * State constants
15
     * @var int
16
     */
17
    const STATE_UP_TO_DATE = 1;
18
    const STATE_PINNED_OUT_OF_DATE = 2;
19
    const STATE_OUT_OF_DATE = 3;
20
    const STATE_INSECURE = 4;
21
    const STATE_NEW_VERSION = 5;
22
23
    /**
24
     * Compare versions to check if they are:
25
     * 1 - Up to date
26
     * 2 - Pinned, out of date
27
     * 3 - Out of date
28
     *
29
     * @param $stable
30
     * @param $latest
31
     * @param $required
32
     * @return int
33
     */
34
    public static function compareVersions($stable, $latest, $required = null)
35
    {
36
        $state = self::STATE_UP_TO_DATE;
37
38
        // prepare stable
39
        $stableArray = explode('.', $stable);
40
        $stableArray[0] = $stableArray[0][0] == 'v' ? substr($stableArray[0], 1) : $stableArray[0];
41
        // prepare latest
42
        $latestArray = explode('.', $latest);
43
        $latestArray[0] = $latestArray[0][0] == 'v' ? substr($latestArray[0], 1) : $latestArray[0];
44
45
        if ($stableArray[0] != $latestArray[0] || (isset($stableArray[1]) && isset($latestArray[1]) && $stableArray[1] != $latestArray[1])) {
46
            $state =  self::STATE_OUT_OF_DATE;
47
        } else if (isset($stableArray[2]) && isset($latestArray[2]) && $stableArray[2] != $latestArray[2]) {
48
            $state =  self::STATE_PINNED_OUT_OF_DATE;
49
        }
50
51
        // ToDo: The satisfies function didn't worked as expected. For example the constraint ~4.1 is not satisfying the version 4.9.5.
52
        if ($state != self::STATE_UP_TO_DATE && $required != null) {
53
            try {
54
                $latestVersion = new version($latest);
55
                if (!$latestVersion->satisfies(new expression($required))) {
56
                    $state = self::STATE_UP_TO_DATE;
57
                }
58
            } catch (SemVerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
59
60
            }
61
        }
62
63
        return $state;
64
    }
65
}