DateIntervalField   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A handles() 0 3 1
A inflate() 0 17 3
1
<?php namespace rtens\domin\delivery\cli\fields;
2
3
use rtens\domin\delivery\cli\CliField;
4
use rtens\domin\Parameter;
5
use watoki\reflect\type\ClassType;
6
7
class DateIntervalField implements CliField {
8
9
    /**
10
     * @param Parameter $parameter
11
     * @return null|string
12
     */
13
    public function getDescription(Parameter $parameter) {
14
        return "[d['d']] hh:mm (eg '3d 12:42')";
15
    }
16
17
    /**
18
     * @param Parameter $parameter
19
     * @return bool
20
     */
21
    public function handles(Parameter $parameter) {
22
        return $parameter->getType() == new ClassType(\DateInterval::class);
23
    }
24
25
    /**
26
     * @param Parameter $parameter
27
     * @param string $serialized
28
     * @return \DateInterval
29
     */
30
    public function inflate(Parameter $parameter, $serialized) {
31
        if (!$serialized) {
32
            return null;
33
        }
34
35
        $days = 0;
36
        if (strpos($serialized, ' ')) {
37
            list($days, $serialized) = explode(' ', $serialized);
38
        }
39
        list($hours, $minutes) = explode(':', $serialized);
40
41
        $days = intval(rtrim($days, "d"));
42
        $hours = intval($hours);
43
        $minutes = intval($minutes);
44
45
        return new \DateInterval("P{$days}DT{$hours}H{$minutes}M");
46
    }
47
}