DateTimeStrategy   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 2
B compare() 0 21 9
1
<?php
2
3
namespace Humweb\Features\Strategy;
4
5
use DateTime;
6
7
/**
8
 * DateTime strategy.
9
 */
10
class DateTimeStrategy extends AbstractStrategy
11
{
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function handle($args = [])
17
    {
18
        $operator = $this->argGet($args, 'operator', '>=');
19
        $datetime = $this->argGet($args, 'datetime', time());
20
21
        if ( ! ($datetime instanceof DateTime)) {
22
            $datetime = new DateTime($datetime);
23
        }
24
25
        return $this->compare($datetime, new DateTime(), $operator);
26
    }
27
28
29
    /**
30
     * Compare two datetime objects
31
     *
32
     * @param DateTime $a
33
     * @param DateTime $b
34
     * @param string   $operator
35
     *
36
     * @return bool
37
     */
38
    protected function compare($a, $b, $operator = '>=')
39
    {
40
        switch ($operator) {
41
            case '<':
42
                return $a < $b;
43
            case '<=':
44
                return $a <= $b;
45
            case '>=':
46
                return $a >= $b;
47
            case '>':
48
                return $a > $b;
49
            case '==':
50
            case '===':
51
                return $a == $b;
52
            case '!=':
53
            case '!==':
54
                return $a != $b;
55
            default:
56
                throw new \InvalidArgumentException('Invalid comparison operator.');
57
        }
58
    }
59
}
60