@@ -527,7 +527,7 @@ |
||
527 | 527 | * Returns the unit object. |
528 | 528 | * |
529 | 529 | * @param $unit_id |
530 | - * @return mixed |
|
530 | + * @return Unit |
|
531 | 531 | */ |
532 | 532 | protected function getUnit($unit_id) { |
533 | 533 | $keyed = $this->keyUnitsById(); |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | $current_states = array_keys($unit_states); |
125 | 125 | // Compare the current states with the set of valid states |
126 | 126 | $remaining_states = array_diff($current_states, $valid_states); |
127 | - if (count($remaining_states) == 0 ) { |
|
127 | + if (count($remaining_states) == 0) { |
|
128 | 128 | // Unit is in a state that is within the set of valid states so add to result set |
129 | 129 | $units[$unit] = $unit; |
130 | 130 | $response->addMatch($keyed_units[$unit], CalendarResponse::VALID_STATE); |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | // Create a mock itemized event for the period in question - since event data is either |
164 | 164 | // in the database or the default value we first create a mock event and then fill it in |
165 | 165 | // accordingly |
166 | - $mock_event = new Event($start_date, $end_date, new Unit(0,0,null), $this->default_value); |
|
166 | + $mock_event = new Event($start_date, $end_date, new Unit(0, 0, null), $this->default_value); |
|
167 | 167 | $itemized = $mock_event->itemizeEvent(); |
168 | 168 | |
169 | 169 | // Cycle through each unit retrieved and provide it with a fully configured itemized mock event |
@@ -253,11 +253,11 @@ discard block |
||
253 | 253 | foreach ($days as $day => $hours) { |
254 | 254 | foreach ($hours as $hour => $value) { |
255 | 255 | if (isset($db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour])) { |
256 | - $result[$year][$month][$day][$hour] = ((int) $db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour]); |
|
256 | + $result[$year][$month][$day][$hour] = ((int)$db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int)$db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour]); |
|
257 | 257 | } |
258 | 258 | else { |
259 | 259 | // If nothing from db - then revert to the defaults |
260 | - $result[$year][$month][$day][$hour] = (int) $keyed_units[$unit]->getDefaultValue(); |
|
260 | + $result[$year][$month][$day][$hour] = (int)$keyed_units[$unit]->getDefaultValue(); |
|
261 | 261 | } |
262 | 262 | } |
263 | 263 | } |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | foreach ($months as $month => $days) { |
271 | 271 | foreach ($days as $day => $hours) { |
272 | 272 | foreach ($hours as $hour => $value) { |
273 | - $result[$year][$month][$day][$hour] = ((int) $value == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $value); |
|
273 | + $result[$year][$month][$day][$hour] = ((int)$value == 0 ? $keyed_units[$unit]->getDefaultValue() : (int)$value); |
|
274 | 274 | } |
275 | 275 | ksort($result[$year][$month][$day], SORT_NATURAL); |
276 | 276 | } |
@@ -300,11 +300,11 @@ discard block |
||
300 | 300 | foreach ($hours as $hour => $minutes) { |
301 | 301 | foreach ($minutes as $minute => $value) { |
302 | 302 | if (isset($db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute])) { |
303 | - $result[$year][$month][$day][$hour][$minute] = ((int) $db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute]); |
|
303 | + $result[$year][$month][$day][$hour][$minute] = ((int)$db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int)$db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute]); |
|
304 | 304 | } |
305 | 305 | else { |
306 | 306 | // If nothing from db - then revert to the defaults |
307 | - $result[$year][$month][$day][$hour][$minute] = (int) $keyed_units[$unit]->getDefaultValue(); |
|
307 | + $result[$year][$month][$day][$hour][$minute] = (int)$keyed_units[$unit]->getDefaultValue(); |
|
308 | 308 | } |
309 | 309 | } |
310 | 310 | } |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | foreach ($days as $day => $hours) { |
320 | 320 | foreach ($hours as $hour => $minutes) { |
321 | 321 | foreach ($minutes as $minute => $value) { |
322 | - $result[$year][$month][$day][$hour][$minute] = ((int) $value == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $value); |
|
322 | + $result[$year][$month][$day][$hour][$minute] = ((int)$value == 0 ? $keyed_units[$unit]->getDefaultValue() : (int)$value); |
|
323 | 323 | } |
324 | 324 | ksort($result[$year][$month][$day][$hour], SORT_NATURAL); |
325 | 325 | } |
@@ -382,12 +382,12 @@ discard block |
||
382 | 382 | // Value just switched - let us wrap up with current event and start a new one |
383 | 383 | $normalized_events[$unit_id][] = new Event($start_event, $end_event, $this->getUnit($unit_id), $current_value); |
384 | 384 | $start_event = clone($end_event->add(new \DateInterval('PT1M'))); |
385 | - $end_event = new \DateTime($year . '-' . $month . '-' . substr($day, 1) . ' ' . substr($hour, 1) . ':' . substr($minute,1)); |
|
385 | + $end_event = new \DateTime($year . '-' . $month . '-' . substr($day, 1) . ' ' . substr($hour, 1) . ':' . substr($minute, 1)); |
|
386 | 386 | $current_value = $minute_value; |
387 | 387 | } |
388 | 388 | if ($current_value === NULL) { |
389 | 389 | // We are down to minutes and haven't created and event yet - do one now |
390 | - $start_event = new \DateTime($year . '-' . $month . '-' . substr($day, 1) . ' ' . substr($hour, 1) . ':' . substr($minute,1)); |
|
390 | + $start_event = new \DateTime($year . '-' . $month . '-' . substr($day, 1) . ' ' . substr($hour, 1) . ':' . substr($minute, 1)); |
|
391 | 391 | $end_event = clone($start_event); |
392 | 392 | } |
393 | 393 | $current_value = $minute_value; |
@@ -530,7 +530,7 @@ discard block |
||
530 | 530 | * @return mixed |
531 | 531 | */ |
532 | 532 | protected function getUnit($unit_id) { |
533 | - $keyed = $this->keyUnitsById(); |
|
533 | + $keyed = $this->keyUnitsById(); |
|
534 | 534 | return $keyed[$unit_id]; |
535 | 535 | } |
536 | 536 |
@@ -128,8 +128,7 @@ discard block |
||
128 | 128 | // Unit is in a state that is within the set of valid states so add to result set |
129 | 129 | $units[$unit] = $unit; |
130 | 130 | $response->addMatch($keyed_units[$unit], CalendarResponse::VALID_STATE); |
131 | - } |
|
132 | - else { |
|
131 | + } else { |
|
133 | 132 | $response->addMiss($keyed_units[$unit], CalendarResponse::INVALID_STATE); |
134 | 133 | } |
135 | 134 | |
@@ -223,8 +222,7 @@ discard block |
||
223 | 222 | foreach ($days as $day => $value) { |
224 | 223 | $result[$year][$month][$day] = ((int)$db_events[$unit][Event::BAT_DAY][$year][$month][$day] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int)$db_events[$unit][Event::BAT_DAY][$year][$month][$day]); |
225 | 224 | } |
226 | - } |
|
227 | - else { |
|
225 | + } else { |
|
228 | 226 | foreach ($days as $day => $value) { |
229 | 227 | $result[$year][$month][$day] = $keyed_units[$unit]->getDefaultValue(); |
230 | 228 | } |
@@ -254,8 +252,7 @@ discard block |
||
254 | 252 | foreach ($hours as $hour => $value) { |
255 | 253 | if (isset($db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour])) { |
256 | 254 | $result[$year][$month][$day][$hour] = ((int) $db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $db_events[$unit][Event::BAT_HOUR][$year][$month][$day][$hour]); |
257 | - } |
|
258 | - else { |
|
255 | + } else { |
|
259 | 256 | // If nothing from db - then revert to the defaults |
260 | 257 | $result[$year][$month][$day][$hour] = (int) $keyed_units[$unit]->getDefaultValue(); |
261 | 258 | } |
@@ -301,8 +298,7 @@ discard block |
||
301 | 298 | foreach ($minutes as $minute => $value) { |
302 | 299 | if (isset($db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute])) { |
303 | 300 | $result[$year][$month][$day][$hour][$minute] = ((int) $db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute] == 0 ? $keyed_units[$unit]->getDefaultValue() : (int) $db_events[$unit][Event::BAT_MINUTE][$year][$month][$day][$hour][$minute]); |
304 | - } |
|
305 | - else { |
|
301 | + } else { |
|
306 | 302 | // If nothing from db - then revert to the defaults |
307 | 303 | $result[$year][$month][$day][$hour][$minute] = (int) $keyed_units[$unit]->getDefaultValue(); |
308 | 304 | } |
@@ -377,8 +373,7 @@ discard block |
||
377 | 373 | if ($current_value === $minute_value) { |
378 | 374 | // We are still in minutes and going through so add a minute |
379 | 375 | $end_event->add(new \DateInterval('PT1M')); |
380 | - } |
|
381 | - elseif (($current_value != $minute_value) && ($current_value !== NULL)) { |
|
376 | + } elseif (($current_value != $minute_value) && ($current_value !== NULL)) { |
|
382 | 377 | // Value just switched - let us wrap up with current event and start a new one |
383 | 378 | $normalized_events[$unit_id][] = new Event($start_event, $end_event, $this->getUnit($unit_id), $current_value); |
384 | 379 | $start_event = clone($end_event->add(new \DateInterval('PT1M'))); |
@@ -392,12 +387,10 @@ discard block |
||
392 | 387 | } |
393 | 388 | $current_value = $minute_value; |
394 | 389 | } |
395 | - } |
|
396 | - elseif ($current_value === $hour_value) { |
|
390 | + } elseif ($current_value === $hour_value) { |
|
397 | 391 | // We are in hours and can add something |
398 | 392 | $end_event->add(new \DateInterval('PT1H')); |
399 | - } |
|
400 | - elseif (($current_value != $hour_value) && ($current_value !== NULL)) { |
|
393 | + } elseif (($current_value != $hour_value) && ($current_value !== NULL)) { |
|
401 | 394 | // Value just switched - let us wrap up with current event and start a new one |
402 | 395 | $normalized_events[$unit_id][] = new Event($start_event, $end_event, $this->getUnit($unit_id), $current_value); |
403 | 396 | // Start event becomes the end event with a minute added |
@@ -414,12 +407,10 @@ discard block |
||
414 | 407 | $current_value = $hour_value; |
415 | 408 | } |
416 | 409 | } |
417 | - } |
|
418 | - elseif ($current_value === $value) { |
|
410 | + } elseif ($current_value === $value) { |
|
419 | 411 | // We are adding a whole day so the end event gets moved to the end of the day we are adding |
420 | 412 | $end_event = new \DateTime($year . '-' . $month . '-' . substr($day, 1) . ' ' . '23:59'); |
421 | - } |
|
422 | - elseif (($current_value !== $value) && ($current_value !== NULL)) { |
|
413 | + } elseif (($current_value !== $value) && ($current_value !== NULL)) { |
|
423 | 414 | // Value just switched - let us wrap up with current event and start a new one |
424 | 415 | $normalized_events[$unit_id][] = new Event($start_event, $end_event, $this->getUnit($unit_id), $current_value); |
425 | 416 | // Start event becomes the end event with a minute added |
@@ -454,8 +445,7 @@ discard block |
||
454 | 445 | if ($event->endsLater($end_date)) { |
455 | 446 | $event->setEndDate($end_date); |
456 | 447 | } |
457 | - } |
|
458 | - else { |
|
448 | + } else { |
|
459 | 449 | // Event completely not in range so unset it |
460 | 450 | unset($normalized_events[$unit_id][$key]); |
461 | 451 | } |
@@ -486,8 +476,7 @@ discard block |
||
486 | 476 | $e++; |
487 | 477 | $flipped[$e][$value][$datum] = $datum; |
488 | 478 | $old_value = $value; |
489 | - } |
|
490 | - else { |
|
479 | + } else { |
|
491 | 480 | $flipped[$e][$value][$datum] = $datum; |
492 | 481 | } |
493 | 482 | } |
@@ -9,7 +9,6 @@ |
||
9 | 9 | |
10 | 10 | use Roomify\Bat\Event\EventInterface; |
11 | 11 | use Roomify\Bat\Store\Store; |
12 | -use Roomify\Bat\Unit\Unit; |
|
13 | 12 | |
14 | 13 | abstract class AbstractEvent implements EventInterface { |
15 | 14 |
@@ -28,7 +28,7 @@ |
||
28 | 28 | * @param $unit |
29 | 29 | * @param $value |
30 | 30 | */ |
31 | - public function __construct(\DateTime $start_date, \DateTime $end_date, Unit $unit , $value = 0) { |
|
31 | + public function __construct(\DateTime $start_date, \DateTime $end_date, Unit $unit, $value = 0) { |
|
32 | 32 | $this->unit_id = $unit->getUnitId(); |
33 | 33 | $this->unit = $unit; |
34 | 34 | $this->start_date = clone($start_date); |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | $minutes_query = TRUE; |
90 | 90 | |
91 | 91 | // Create a mock event which we will use to determine how to query the database |
92 | - $mock_event = new Event($start_date, $end_date, new Unit(0,0,null), -10); |
|
92 | + $mock_event = new Event($start_date, $end_date, new Unit(0, 0, null), -10); |
|
93 | 93 | // We don't need a granular event even if we are retrieving granular data - since we don't |
94 | 94 | // know what the event break-down is going to be we need to get the full range of data from |
95 | 95 | // days, hours and minutes. |
@@ -101,16 +101,16 @@ discard block |
||
101 | 101 | |
102 | 102 | $query_parameters = ''; |
103 | 103 | |
104 | - foreach($itemized[Event::BAT_DAY] as $year => $months) { |
|
104 | + foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
|
105 | 105 | if ($year_count > 0) { |
106 | 106 | // We are dealing with multiple years so add an OR |
107 | 107 | $query_parameters .= ' OR '; |
108 | 108 | } |
109 | 109 | $query_parameters .= 'year IN (' . $year . ') '; |
110 | - $query_parameters .= 'AND month IN (' . implode("," ,array_keys($months)) .') '; |
|
110 | + $query_parameters .= 'AND month IN (' . implode(",", array_keys($months)) . ') '; |
|
111 | 111 | if (count($unit_ids) > 0) { |
112 | 112 | // Unit ids are defined so add this as a filter |
113 | - $query_parameters .= 'AND unit_id in (' . implode("," , $unit_ids) .') '; |
|
113 | + $query_parameters .= 'AND unit_id in (' . implode(",", $unit_ids) . ') '; |
|
114 | 114 | } |
115 | 115 | $year_count++; |
116 | 116 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
36 | 36 | |
37 | - $queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
|
37 | + $queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
|
38 | 38 | |
39 | 39 | $results = array(); |
40 | 40 | // Run each query and store results |
@@ -49,28 +49,28 @@ discard block |
||
49 | 49 | // Figure out how many days the current month has |
50 | 50 | $temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
51 | 51 | $days_in_month = (int)$temp_date->format('t'); |
52 | - for ($i = 1; $i<=$days_in_month; $i++) { |
|
53 | - $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i]; |
|
52 | + for ($i = 1; $i <= $days_in_month; $i++) { |
|
53 | + $db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d' . $i]; |
|
54 | 54 | } |
55 | 55 | } |
56 | 56 | |
57 | 57 | // With the day events taken care off let's cycle through hours |
58 | 58 | foreach ($results[Event::BAT_HOUR]->fetchAll() as $data) { |
59 | - for ($i = 0; $i<=23; $i++) { |
|
60 | - $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']]['d'.$data['day']]['h'. $i] = $data['h'.$i]; |
|
59 | + for ($i = 0; $i <= 23; $i++) { |
|
60 | + $db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']]['d' . $data['day']]['h' . $i] = $data['h' . $i]; |
|
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // With the hour events taken care off let's cycle through minutes |
65 | 65 | foreach ($results[Event::BAT_MINUTE]->fetchAll() as $data) { |
66 | - for ($i = 0; $i<=59; $i++) { |
|
66 | + for ($i = 0; $i <= 59; $i++) { |
|
67 | 67 | if ($i <= 9) { |
68 | - $index = 'm0'.$i; |
|
68 | + $index = 'm0' . $i; |
|
69 | 69 | } |
70 | 70 | else { |
71 | - $index = 'm'.$i; |
|
71 | + $index = 'm' . $i; |
|
72 | 72 | } |
73 | - $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']]['d'.$data['day']]['h'.$data['hour']][$index] = $data[$index]; |
|
73 | + $db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']]['d' . $data['day']]['h' . $data['hour']][$index] = $data[$index]; |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 | |
@@ -102,11 +102,11 @@ discard block |
||
102 | 102 | // Because SQLite does not have a nice merge first we have to check if a row exists to determine whether to do an insert or an update |
103 | 103 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month])) { |
104 | 104 | $command = "UPDATE $this->day_table SET "; |
105 | - foreach ($days as $day => $value){ |
|
106 | - $command .= "$day = $value,"; |
|
105 | + foreach ($days as $day => $value) { |
|
106 | + $command .= "$day = $value,"; |
|
107 | 107 | } |
108 | 108 | $command = rtrim($command, ','); |
109 | - $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month"; |
|
109 | + $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = $year AND month = $month"; |
|
110 | 110 | $this->pdo->exec($command); |
111 | 111 | } else { |
112 | 112 | $this->pdo->exec("INSERT INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . implode(', ', $values) . ")"); |
@@ -125,11 +125,11 @@ discard block |
||
125 | 125 | $keys = array_keys($hours); |
126 | 126 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_HOUR][$year][$month][$day])) { |
127 | 127 | $command = "UPDATE $this->hour_table SET "; |
128 | - foreach ($hours as $hour => $value){ |
|
129 | - $command .= "$hour = $value,"; |
|
128 | + foreach ($hours as $hour => $value) { |
|
129 | + $command .= "$hour = $value,"; |
|
130 | 130 | } |
131 | 131 | $command = rtrim($command, ','); |
132 | - $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1); |
|
132 | + $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = $year AND month = $month AND day = " . substr($day, 1); |
|
133 | 133 | $this->pdo->exec($command); |
134 | 134 | } else { |
135 | 135 | $this->pdo->exec("INSERT INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")"); |
@@ -148,11 +148,11 @@ discard block |
||
148 | 148 | $keys = array_keys($minutes); |
149 | 149 | if (isset($existing_events[$event->getUnitId()][EVENT::BAT_MINUTE][$year][$month][$day][$hour])) { |
150 | 150 | $command = "UPDATE $this->minute_table SET "; |
151 | - foreach ($minutes as $minute => $value){ |
|
152 | - $command .= "$minute = $value,"; |
|
151 | + foreach ($minutes as $minute => $value) { |
|
152 | + $command .= "$minute = $value,"; |
|
153 | 153 | } |
154 | 154 | $command = rtrim($command, ','); |
155 | - $command .= " WHERE unit_id = " . $event->getUnitId() ." AND year = $year AND month = $month AND day = ". substr($day,1) . " AND hour = " . substr($hour,1); |
|
155 | + $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = $year AND month = $month AND day = " . substr($day, 1) . " AND hour = " . substr($hour, 1); |
|
156 | 156 | $this->pdo->exec($command); |
157 | 157 | } else { |
158 | 158 | $this->pdo->exec("INSERT INTO $this->minute_table (unit_id, year, month, day, hour, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . substr($hour, 1) . ", " . implode(', ', $values) . ")"); |