Completed
Push — develop ( 488b09...0de0ee )
by Mathias
131:05 queued 122:54
created

Utils   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
eloc 23
dl 0
loc 47
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createDateTime() 0 18 4
A createDateInterval() 0 25 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Queue;
12
13
/**
14
 * ${CARET}
15
 * 
16
 * @author Mathias Gelhausen <[email protected]>
17
 * @todo write test 
18
 */
19
final class Utils
20
{
21
    public static function createDateInterval($value)
22
    {
23
        if ($value instanceOf \DateInterval) {
24
            return $value;
25
        }
26
27
        if (is_numeric($value)) {
28
            $delay = new \DateInterval(sprintf("PT%dS", abs((int) $value)));
29
            $delay->invert = ($value < 0) ? 1 : 0;
30
31
            return $delay;
32
        }
33
34
        if (is_string($value)) {
35
            try {
36
                // first try ISO 8601 duration specification
37
                $delay = new \DateInterval($value);
38
            } catch (\Exception $e) {
39
                // then try normal date parser
40
                $delay = \DateInterval::createFromDateString($value);
41
            }
42
            return $delay;
43
        }
44
45
        return new \DateInterval('PT0S');
46
    }
47
48
    public static function createDateTime($value)
49
    {
50
        if ($value instanceOf \DateTime) {
51
            return $value;
52
        }
53
54
        if (is_numeric($value)) {
55
            return new \DateTime(
56
                sprintf("@%d", (int) $value),
57
                new \DateTimeZone(date_default_timezone_get())
58
            );
59
        }
60
61
        if (is_string($value)) {
62
            return new \DateTime($value, new \DateTimeZone(date_default_timezone_get()));
63
        }
64
65
        return new \DateTime();
66
    }
67
}
68