Completed
Push — master ( 9ea25e...b0fe89 )
by Kristof
06:49
created

Calendar::toJsonLd()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 6.8963
cc 9
eloc 28
nc 16
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Contains CultuurNet\UDB3\Calendar.
6
 */
7
8
namespace CultuurNet\UDB3;
9
10
use Broadway\Serializer\SerializableInterface;
11
12
/**
13
 * a Calendar for events and places.
14
 */
15
class Calendar implements CalendarInterface, JsonLdSerializableInterface, SerializableInterface
16
{
17
18
    /**
19
     * @var string
20
     */
21
    protected $type = null;
22
23
    /**
24
     * @var string
25
     */
26
    protected $startDate = null;
27
28
    /**
29
     * @var string
30
     */
31
    protected $endDate = null;
32
33
    /**
34
     * @var \CultuurNet\UDB3\Timestamp[]
35
     */
36
    protected $timestamps = array();
37
38
    /**
39
     * @var Array
40
     */
41
    protected $openingHours = array();
42
43
    const SINGLE = "single";
44
    const MULTIPLE = "multiple";
45
    const PERIODIC = "periodic";
46
    const PERMANENT = "permanent";
47
48
    /**
49
     * Constructor.
50
     */
51
    public function __construct($calendarType, $startDate = '', $endDate = '', $timestamps = array(), $openingHours = array())
52
    {
53
        if ($calendarType != self::PERMANENT && $calendarType != self::MULTIPLE && $calendarType != self::PERIODIC && $calendarType != self::SINGLE) {
54
            throw new \UnexpectedValueException('Invalid calendar type: ' . $calendarType . '==' . self::PERMANENT . ' given.');
55
        }
56
57
        if (($calendarType == self::MULTIPLE || $calendarType == self::SINGLE) && empty($startDate)) {
58
            throw new \UnexpectedValueException('Start date can not be empty for calendar type: ' . $calendarType . '.');
59
        }
60
61
        $this->type = $calendarType;
62
        $this->startDate = $startDate;
63
        $this->endDate = $endDate;
64
        $this->timestamps = $timestamps;
65
        $this->openingHours = $openingHours;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function getType()
72
    {
73
        return $this->type;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function serialize()
80
    {
81
        return [
82
          'type' => $this->getType(),
83
          'startDate' => $this->startDate,
84
          'endDate' => $this->endDate,
85
          'timestamps' => $this->timestamps,
86
          'openingHours' => $this->openingHours,
87
        ];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public static function deserialize(array $data)
94
    {
95
        return new static(
96
            $data['type'], $data['startDate'], $data['endDate'], $data['timestamps'], $data['openingHours']
97
        );
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function getStartDate()
104
    {
105
        return $this->startDate;
106
    }
107
108
    /**
109
     * Get the end date
110
     */
111
    public function getEndDate()
112
    {
113
        return $this->endDate;
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function getOpeningHours()
120
    {
121
        return $this->openingHours;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function getTimestamps()
128
    {
129
        return $this->timestamps;
130
    }
131
132
    /**
133
     * Return the jsonLD version of a calendar.
134
     */
135
    public function toJsonLd()
136
    {
137
        $jsonLd = [];
138
139
        $startDate = $this->getStartDate();
140
        $endDate = $this->getEndDate();
141
142
        $jsonLd['calendarType'] = $this->getType();
143
        // All calendar types allow startDate (and endDate).
144
        // One timestamp - full day.
145
        // One timestamp - start hour.
146
        // One timestamp - start and end hour.
147
        if (!empty($startDate)) {
148
            $jsonLd['startDate'] = $startDate;
149
        }
150
151
        if (!empty($endDate)) {
152
            $jsonLd['endDate'] = $endDate;
153
        }
154
155
        $timestamps = $this->getTimestamps();
156
        if (!empty($timestamps)) {
157
            $jsonLd['subEvent'] = array();
158
            foreach ($timestamps as $timestamp) {
159
                $jsonLd['subEvent'][] = array(
160
                  '@type' => 'Event',
161
                  'startDate' => $timestamp->getStartDate(),
162
                  'endDate' => $timestamp->getEndDate(),
163
                );
164
            }
165
        }
166
167
        // Period.
168
        // Period with openingtimes.
169
        // Permanent - "altijd open".
170
        // Permanent - with openingtimes
171
        $openingHours = $this->getOpeningHours();
172
        if (!empty($openingHours)) {
173
            $jsonLd['openingHours'] = array();
174
            foreach ($openingHours as $openingHour) {
175
                $schedule = array('dayOfWeek' => $openingHour->dayOfWeek);
176
                if (!empty($openingHour->opens)) {
177
                    $schedule['opens'] = $openingHour->opens;
178
                }
179
                if (!empty($openingHour->closes)) {
180
                    $schedule['closes'] = $openingHour->closes;
181
                }
182
                $jsonLd['openingHours'][] = $schedule;
183
            }
184
        }
185
186
        return $jsonLd;
187
    }
188
}
189