Completed
Push — master ( 5fe85d...a4e09c )
by Michael
01:40
created

VersionChecks   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D checkVerXoops() 0 39 9
A checkVerPhp() 0 16 4
1
<?php namespace Xoopsmodules\instruction\common;
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright   XOOPS Project (https://xoops.org)
14
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author      mamba <[email protected]>
16
 */
17
trait VersionChecks
18
{
19
    /**
20
     *
21
     * Verifies XOOPS version meets minimum requirements for this module
22
     * @static
23
     * @param \XoopsModule|null $module
24
     *
25
     * @param null|string $requiredVer
26
     * @return bool true if meets requirements, false if not
27
     */
28
    public static function checkVerXoops(\XoopsModule $module = null, $requiredVer = null)
29
    {
30
        $moduleDirName = basename(dirname(dirname(__DIR__)));
31
        if (null === $module) {
32
            $module = XoopsModule::getByDirname($moduleDirName);
33
        }
34
        xoops_loadLanguage('admin', $moduleDirName);
35
        //check for minimum XOOPS version
36
        $currentVer = substr(XOOPS_VERSION, 6); // get the numeric part of string
37
        $currArray  = explode('.', $currentVer);
38
        if (null === $requiredVer) {
39
            $requiredVer = '' . $module->getInfo('min_xoops'); //making sure it's a string
40
        }
41
        $reqArray = explode('.', $requiredVer);
42
        $success  = true;
43
        foreach ($reqArray as $k => $v) {
44
            if (isset($currArray[$k])) {
45
                if ($currArray[$k] > $v) {
46
                    break;
47
                } elseif ($currArray[$k] == $v) {
48
                    continue;
49
                } else {
50
                    $success = false;
51
                    break;
52
                }
53
            } else {
54
                if ((int)$v > 0) { // handles versions like x.x.x.0_RC2
55
                    $success = false;
56
                    break;
57
                }
58
            }
59
        }
60
61
        if (false === $success) {
62
            $module->setErrors(sprintf(_AM_WFL_ERROR_BAD_XOOPS, $requiredVer, $currentVer));
63
        }
64
65
        return $success;
66
    }
67
68
    /**
69
     *
70
     * Verifies PHP version meets minimum requirements for this module
71
     * @static
72
     * @param \XoopsModule $module
73
     *
74
     * @return bool true if meets requirements, false if not
75
     */
76
    public static function checkVerPhp(\XoopsModule $module)
77
    {
78
        xoops_loadLanguage('admin', $module->dirname());
79
        // check for minimum PHP version
80
        $success = true;
81
        $verNum  = PHP_VERSION;
82
        $reqVer  = $module->getInfo('min_php');
83
        if (false !== $reqVer && '' !== $reqVer) {
84
            if (version_compare($verNum, $reqVer, '<')) {
85
                $module->setErrors(sprintf(_AM_WFL_ERROR_BAD_PHP, $reqVer, $verNum));
86
                $success = false;
87
            }
88
        }
89
90
        return $success;
91
    }
92
}
93