DateTimeRangeStrategy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 29 4
A runDateTimeStrategy() 0 15 2
1
<?php
2
3
namespace Humweb\Features\Strategy;
4
5
/**
6
 * DateTimeRange strategy.
7
 */
8
class DateTimeRangeStrategy extends AbstractStrategy
9
{
10
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function handle($args = [])
15
    {
16
17
        $strict = $this->argGet($args, 'strict', true);
18
19
        $dateRange = [
20
            [
21
                'date'     => $this->argGet($args, 'start'),
22
                'operator' => '<'
23
            ],
24
            [
25
                'date'     => $this->argGet($args, 'end'),
26
                'operator' => '>'
27
            ]
28
        ];
29
30
        foreach ($dateRange as $range) {
31
32
            if ( ! $strict) {
33
                $range['operator'] .= '=';
34
            }
35
36
            if ($this->runDateTimeStrategy($range['date'], $range['operator']) === false) {
37
                return false;
38
            }
39
        }
40
41
        return true;
42
    }
43
44
45
    /**
46
     * Run datetime strategy
47
     *
48
     * @param string|\DateTime $datetime
49
     * @param string           $operator
50
     *
51
     * @return bool
52
     */
53
    public function runDateTimeStrategy($datetime, $operator = '>=')
54
    {
55
        $strategy = new DateTimeStrategy();
56
57
        $args = [
58
            'datetime' => $datetime,
59
            'operator' => $operator
60
        ];
61
62
        if ( ! call_user_func_array([$strategy, 'handle'], [$args])) {
63
            return false;
64
        }
65
66
        return true;
67
    }
68
}
69