Passed
Branch master (a5e63d)
by Peter
04:12 queued 01:47
created

Dates::matchDateRange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace Themes\Matchers;
4
5
use RunTimeException;
6
7
class Dates implements MatcherInterface
8
{
9
    public function handle($request, $match)
10
    {
11
        if (!$match) {
12
            throw new RunTimeException('Missing dates for matching');
13
        }
14
15
        $date = explode(',', $match);
16
17
        if (count($date) == 1) {
18
            return $this->matchSingleDate($date[0]);
19
        }
20
21
        return $this->matchDateRange($date[0], $date[1]);
22
    }
23
24
    protected function matchSingleDate($date)
25
    {
26
        $now = date('Y-m-d');
27
28
        if ($date < $now) {
29
            return false;
30
        }
31
32
        return true;
33
    }
34
35
    protected function matchDateRange($begin, $end)
36
    {
37
        if ($begin > $end) {
38
            throw new RunTimeException('End date cannot be before start date');
39
        }
40
41
        $now = date('Y-m-d');
42
43
        if ($now < $begin) {
44
            return false;
45
        }
46
47
        if ($end < $now) {
48
            return false; 
49
        }
50
51
        return true;
52
    }
53
}
54