Passed
Push — master ( 13b287...8ae221 )
by Victor
02:00 queued 12s
created

Date   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 52
c 0
b 0
f 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A intvg() 0 11 1
A intv() 0 8 2
A rdm() 0 6 2
1
<?php
2
3
4
namespace Vctls\IntervalGraph\Util;
5
6
use DateInterval;
7
use DateTime;
8
use Exception;
9
use Vctls\IntervalGraph\IntervalGraph;
10
11
/**
12
 * Utility class for creating interval graphs.
13
 * 
14
 * @package Vctls\IntervalGraph
15
 */
16
class Date
17
{
18
    /**
19
     * Create an interval of dates from two integers and a value.
20
     *
21
     * @param $start
22
     * @param $end
23
     * @param mixed $value
24
     * @return array
25
     */
26
    public static function intv(int $start, int $end, $value = null)
27
    {
28
        try {
29
            $start = new DateTime("today + $start days");
30
            $end = (new DateTime("today + $end days"))->setTime(23,59,59);
31
        } catch (Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
        
33
        return [$start, $end, $value];
34
    }
35
36
    /**
37
     * Create an IntervalGraph for handling dates.
38
     *
39
     * @param $args
40
     * @return IntervalGraph
41
     */
42
    public static function intvg($args) {
43
        $substractStep = function (DateTime $bound) {
44
            return (clone $bound)->sub(new DateInterval('PT1S'));
45
        };
46
47
        $addStep = function (DateTime $bound) {
48
            return (clone $bound)->add(new DateInterval('PT1S'));
49
        };
50
        return (new IntervalGraph($args))
51
            ->setAddStep($addStep)
52
            ->setSubstractStep($substractStep);
53
    }
54
55
    /**
56
     * Generate a random date.
57
     * 
58
     * @param int $min
59
     * @param int $max
60
     * @return DateTime
61
     */
62
    public static function rdm($min = 1514764800, $max = 1577750400)
63
    {
64
        try {
65
            return $date = (new DateTime)->setTimestamp(mt_rand($min, $max));
0 ignored issues
show
Unused Code introduced by
The assignment to $date is dead and can be removed.
Loading history...
66
        } catch (Exception $e) {
67
            return null;
68
        }
69
    }
70
}