Completed
Pull Request — master (#268)
by
unknown
01:54
created

Xhgui_Storage_Abstract   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getDateTimeFromString() 0 36 12
1
<?php
2
/**
3
 * Abstract class for all storage drivers.
4
 *
5
 * CAUTION: please use interface as a typehint!
6
 */
7
class Xhgui_Storage_Abstract
8
{
9
    /**
10
     * Try to get date from Y-m-d H:i:s or from timestamp
11
     *
12
     * @param string|int $date
13
     * @param string $direction
14
     * @return \DateTime
15
     */
16
    protected function getDateTimeFromString($date, $direction = 'start')
17
    {
18
        try {
19
            $datetime = \DateTime::createFromFormat('Y-m-d H:i:s', $date);
20
            if (!empty($datetime) && $datetime instanceof \DateTime) {
21
                return $datetime;
22
            }
23
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
24
        }
25
26
        // try without time
27
        try {
28
            $datetime = \DateTime::createFromFormat('Y-m-d', $date);
29
            if (!empty($datetime) && $datetime instanceof \DateTime) {
30
                if ($direction === 'start') {
31
                    $datetime->setTime(0, 0, 0);
32
                } elseif ($direction === 'end') {
33
                    $datetime->setTime(23, 59, 59);
34
                }
35
36
                return $datetime;
37
            }
38
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
39
        }
40
41
        // try using timestamp
42
        try {
43
            $datetime = \DateTime::createFromFormat('U', $date);
44
            if (!empty($datetime) && $datetime instanceof \DateTime) {
45
                return $datetime;
46
            }
47
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
        }
49
50
        throw new \InvalidArgumentException('Unable to parse date');
51
    }
52
}
53