Completed
Push — 2.0 ( dd3689...ee0168 )
by Marco
12:16
created

Validator::multithread()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 2
nc 2
nop 1
1
<?php namespace Comodojo\Extender\Utils;
2
3
use \Cron\CronExpression;
4
use \Exception;
5
6
/**
7
 * Basic signaling for extender components
8
 *
9
 * @package     Comodojo Framework
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class Validator {
30
31
    /**
32
     * Validate a cron expression and, if valid, return next run timestamp plus
33
     * an array of expression parts
34
     *
35
     * @param   string  $expression
36
     *
37
     * @return  array   Next run timestamp at first position, expression parts at second
38
     * @throws  \Exception
39
     */
40 View Code Duplication
    public static function cronExpression($expression) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
42
        try {
43
44
            $cron = CronExpression::factory($expression);
45
46
            $s = $cron->getNextRunDate()->format('c');
47
48
            $e = $cron->getExpression();
49
50
            $e_array = preg_split('/\s/', $e, -1, PREG_SPLIT_NO_EMPTY);
51
52
            $e_count = count($e_array);
53
54
            if ( $e_count < 5 || $e_count > 6 ) throw new Exception($e." is not a valid cron expression");
55
56
            if ( $e_count == 5 ) $e_array[] = "*";
57
58
        }
59
        catch (Exception $e) {
60
61
            throw $e;
62
63
        }
64
65
        return array($s, $e_array);
66
67
    }
68
69
    public static function laggerTimeout($timeout) {
70
71
        return filter_var($timeout, FILTER_VALIDATE_INT, array(
72
            'options' => array(
73
                'default' => 5,
74
                'min_range' => 0
75
            )
76
        ));
77
78
    }
79
80
    public static function maxChildRuntime($runtime) {
81
82
        return filter_var($runtime, FILTER_VALIDATE_INT, array(
83
            'options' => array(
84
                'default' => 600,
85
                'min_range' => 1
86
            )
87
        ));
88
89
    }
90
91
    public static function forkLimit($limit) {
92
93
        return filter_var($limit, FILTER_VALIDATE_INT, array(
94
            'options' => array(
95
                'default' => 0,
96
                'min_range' => 0
97
            )
98
        ));
99
100
    }
101
102
    public static function multithread($multithread) {
103
104
        return $multithread === true && Checks::multithread();
105
106
    }
107
108
}
109