Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

MyDateInterval::getDatePattern()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 13
rs 8.8571
1
<?php
2
namespace mod_stats\classes;
3
4
/**
5
 * Class MyDateInterval
6
 * @package mod_stats\classes
7
 */
8
class MyDateInterval
9
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
10
11
    /**
12
     * Returns date pattern for mysql (select part)
13
     * @param int|string $dateInterval
14
     * @return string
15
     */
16
    public static function getDatePattern($dateInterval) {
17
        // date pattern for mysql
18
        switch ($dateInterval) {
19
            case 1:
20
            case 'month':
21
                return '%Y-%m';
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
22
            case 2:
23
            case 'year':
24
                return '%Y';
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
25
            default: // 0: day
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed after DEFAULT statements
Loading history...
26
                return '%Y-%m-%d';
27
        }
28
    }
29
30
    /**
31
     *
32
     * @param string $field
33
     * @param array $params (all are optional)
34
     *  - table
0 ignored issues
show
introduced by
Parameter comment must start with a capital letter
Loading history...
35
     *  - dateFrom
36
     *  - dateTo
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
37
     * @return string condition of date range
38
     */
39 View Code Duplication
    public static function prepareDateBetweenCondition($field, array $params = []) {
40
        $betweenCondition = '';
41
        if (isset($params['dateFrom']) || isset($params['dateTo'])) {
42
            $dateFrom = isset($params['dateFrom']) ? $params['dateFrom'] : '2005-01-01';
43
            $dateTo = isset($params['dateTo']) ? $params['dateTo'] : date('Y-m-d');
44
            $betweenCondition = "AND FROM_UNIXTIME(`{$field}`) BETWEEN '{$dateFrom} 00:00:00' AND '{$dateTo} 23:59:59'";
45
        }
46
        return $betweenCondition;
47
    }
48
49
}