VersionHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B compareVersions() 0 18 11
1
<?php
2
3
namespace Xima\DepmonBundle\Util;
4
5
6
use Composer\Semver\Semver;
7
8
class VersionHelper
9
{
10
11
12
    /**
13
     * State constants
14
     * @var int
15
     */
16
    const STATE_UP_TO_DATE = 1;
17
    const STATE_PINNED_OUT_OF_DATE = 2;
18
    const STATE_OUT_OF_DATE = 3;
19
    const STATE_INSECURE = 4;
20
21
    /**
22
     * Compare versions to check if they are:
23
     * 1 - Up to date
24
     * 2 - Pinned, out of date
25
     * 3 - Out of date
26
     *
27
     * @param $stable
28
     * @param $latest
29
     * @param $required
30
     * @return int
31
     */
32
    public static function compareVersions($stable, $latest, $required = null)
33
    {
34
        $state = self::STATE_UP_TO_DATE;
35
36
        if (explode('.', $stable)[0] != explode('.', $latest)[0] || (isset(explode('.', $stable)[1]) && isset(explode('.', $latest)[1]) && explode('.', $stable)[1] != explode('.', $latest)[1])) {
37
            $state =  self::STATE_OUT_OF_DATE;
38
        } else if (isset(explode('.', $stable)[2]) && isset(explode('.', $latest)[2]) && explode('.', $stable)[2] != explode('.', $latest)[2]) {
39
            $state =  self::STATE_PINNED_OUT_OF_DATE;
40
        }
41
42
        if ($state != self::STATE_UP_TO_DATE && $required != null) {
43
44
            if (Semver::satisfies($stable,$required)) {
45
                $state = self::STATE_UP_TO_DATE;
46
            }
47
        }
48
49
        return $state;
50
    }
51
}