Completed
Pull Request — master (#296)
by Luc
12:58
created

OpeningHour   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getOpens() 0 4 1
A getCloses() 0 4 1
A getWeekDays() 0 4 1
A deserialize() 0 19 1
A serialize() 0 15 1
1
<?php
2
3
namespace CultuurNet\UDB3\Calendar;
4
5
use Broadway\Serializer\SerializableInterface;
6
use ValueObjects\DateTime\Time;
7
use ValueObjects\DateTime\WeekDay;
8
9
class OpeningHour implements SerializableInterface
10
{
11
    const FORMAT = 'H:i:s';
12
13
    /**
14
     * @var Time
15
     */
16
    private $opens;
17
18
    /**
19
     * @var Time
20
     */
21
    private $closes;
22
23
    /**
24
     * @var WeekDay[]
25
     */
26
    private $weekDays;
27
28
    /**
29
     * OpeningHour constructor.
30
     * @param Time $opens
31
     * @param Time $closes
32
     * @param WeekDay[] $weekDays
33
     */
34
    public function __construct(
35
        Time $opens,
36
        Time $closes,
37
        WeekDay ...$weekDays
38
    ) {
39
        $this->weekDays = $weekDays;
0 ignored issues
show
Documentation Bug introduced by
It seems like $weekDays of type array<integer,array<inte...cts\DateTime\WeekDay>>> is incompatible with the declared type array<integer,object<Val...ects\DateTime\WeekDay>> of property $weekDays.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $this->opens = $opens;
41
        $this->closes = $closes;
42
    }
43
44
    /**
45
     * @return Time
46
     */
47
    public function getOpens()
48
    {
49
        return $this->opens;
50
    }
51
52
    /**
53
     * @return Time
54
     */
55
    public function getCloses()
56
    {
57
        return $this->closes;
58
    }
59
60
    /**
61
     * @return WeekDay[]
62
     */
63
    public function getWeekDays()
64
    {
65
        return $this->weekDays;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public static function deserialize(array $data)
72
    {
73
        $weekDays = array_map(
74
            function ($dayOfWeek) {
75
                return WeekDay::fromNative($dayOfWeek);
76
            },
77
            $data['dayOfWeek']
78
        );
79
80
        return new static(
81
            Time::fromNativeDateTime(
82
                \DateTime::createFromFormat(self::FORMAT, $data['opens'])
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...FORMAT, $data['opens']) targeting DateTime::createFromFormat() can also be of type false; however, ValueObjects\DateTime\Time::fromNativeDateTime() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
83
            ),
84
            Time::fromNativeDateTime(
85
                \DateTime::createFromFormat(self::FORMAT, $data['closes'])
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...ORMAT, $data['closes']) targeting DateTime::createFromFormat() can also be of type false; however, ValueObjects\DateTime\Time::fromNativeDateTime() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
86
            ),
87
            ...$weekDays
88
        );
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function serialize()
95
    {
96
        $serializedWeekDays = array_map(
97
            function (WeekDay $weekDay) {
98
                return $weekDay->getValue();
99
            },
100
            $this->weekDays
101
        ) ;
102
103
        return [
104
            'opens' => $this->opens->toNativeDateTime()->format(self::FORMAT),
105
            'closes' => $this->closes->toNativeDateTime()->format(self::FORMAT),
106
            'dayOfWeek' => $serializedWeekDays
107
        ];
108
    }
109
}
110