Completed
Pull Request — master (#345)
by
unknown
05:39
created

OpeningTime   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromNativeDateTime() 0 7 1
A fromNativeString() 0 6 1
A toNativeString() 0 4 1
A getHour() 0 4 1
A getMinute() 0 4 1
A sameValueAs() 0 5 2
A __toString() 0 4 1
A toNativeDateTime() 0 10 1
A fromUdb3ModelTime() 0 6 1
1
<?php
2
3
namespace CultuurNet\UDB3\Calendar;
4
5
use CultuurNet\UDB3\Model\ValueObject\Calendar\OpeningHours\Time;
6
use ValueObjects\DateTime\Hour;
7
use ValueObjects\DateTime\Minute;
8
9
class OpeningTime
10
{
11
    /**
12
     * @var Hour
13
     */
14
    private $hour;
15
16
    /**
17
     * @var Minute
18
     */
19
    private $minute;
20
21
    /**
22
     * Custom value object for opening times without seconds.
23
     *
24
     * @param Hour $hour
25
     * @param Minute $minute
26
     */
27
    public function __construct(Hour $hour, Minute $minute)
28
    {
29
        $this->hour = $hour;
30
        $this->minute = $minute;
31
    }
32
33
    /**
34
     * @param \DateTimeInterface $dateTime
35
     * @return OpeningTime
36
     */
37
    public static function fromNativeDateTime(\DateTimeInterface $dateTime)
38
    {
39
        $hour = new Hour(\intval($dateTime->format('H')));
40
        $minute = new Minute(\intval($dateTime->format('i')));
41
42
        return new OpeningTime($hour, $minute);
43
    }
44
45
    /**
46
     * The supported string format is H:i
47
     *
48
     * @param string $time
49
     * @return OpeningTime
50
     */
51
    public static function fromNativeString($time)
52
    {
53
        return self::fromNativeDateTime(
54
            \DateTime::createFromFormat('H:i', $time)
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFormat('H:i', $time) targeting DateTime::createFromFormat() can also be of type false; however, CultuurNet\UDB3\Calendar...e::fromNativeDateTime() does only seem to accept object<DateTimeInterface>, did you maybe forget to handle an error condition?
Loading history...
55
        );
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function toNativeString()
62
    {
63
        return (string) $this;
64
    }
65
66
    /**
67
     * @return Hour
68
     */
69
    public function getHour()
70
    {
71
        return $this->hour;
72
    }
73
74
    /**
75
     * @return Minute
76
     */
77
    public function getMinute()
78
    {
79
        return $this->minute;
80
    }
81
82
    /**
83
     * @param OpeningTime $time
84
     * @return bool
85
     */
86
    public function sameValueAs(OpeningTime $time)
87
    {
88
        return $this->getHour()->sameValueAs($time->getHour()) &&
89
            $this->getMinute()->sameValueAs($time->getMinute());
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function __toString()
96
    {
97
        return $this->toNativeDateTime()->format('H:i');
98
    }
99
100
    /**
101
     * @return \DateTimeInterface
102
     */
103
    private function toNativeDateTime()
104
    {
105
        $hour   = $this->getHour()->toNative();
106
        $minute = $this->getMinute()->toNative();
107
108
        $time = new \DateTime('now');
109
        $time->setTime($hour, $minute);
110
111
        return $time;
112
    }
113
114
    /**
115
     * @param Time $time
116
     * @return self
117
     */
118
    public static function fromUdb3ModelTime(Time $time)
119
    {
120
        $hour = new Hour($time->getHour()->toInteger());
121
        $minute = new Minute($time->getMinute()->toInteger());
122
        return new self($hour, $minute);
123
    }
124
}
125