1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class SqlLiteDBStore |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Store; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\Event; |
11
|
|
|
use Roomify\Bat\Store\SqlDBStore; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a SqlLite implementation of the Store. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class SqlLiteDBStore extends SqlDBStore { |
18
|
|
|
|
19
|
|
|
protected $pdo; |
20
|
|
|
|
21
|
|
|
public function __construct(\PDO $pdo, $event_type, $event_data = 'state') { |
22
|
|
|
parent::__construct($event_type, $event_data); |
23
|
|
|
|
24
|
|
|
$this->pdo = $pdo; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @param \DateTime $start_date |
30
|
|
|
* @param \DateTime $end_date |
31
|
|
|
* @param $unit_ids |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
36
|
|
|
|
37
|
|
|
$queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
38
|
|
|
|
39
|
|
|
$results = array(); |
40
|
|
|
// Run each query and store results |
41
|
|
|
foreach ($queries as $type => $query) { |
42
|
|
|
$results[$type] = $this->pdo->query($query); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$db_events = array(); |
46
|
|
|
|
47
|
|
|
// Cycle through day results and setup an event array |
48
|
|
|
foreach ($results[Event::BAT_DAY]->fetchAll() as $data) { |
49
|
|
|
// Figure out how many days the current month has |
50
|
|
|
$temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
51
|
|
|
$days_in_month = (int)$temp_date->format('t'); |
52
|
|
View Code Duplication |
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
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// With the day events taken care off let's cycle through hours |
58
|
|
|
foreach ($results[Event::BAT_HOUR]->fetchAll() as $data) { |
59
|
|
View Code Duplication |
for ($i = 0; $i<=23; $i++) { |
|
|
|
|
60
|
|
|
$db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']][$data['day']]['h'. $i] = $data['h'.$i]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// With the hour events taken care off let's cycle through minutes |
65
|
|
|
foreach ($results[Event::BAT_MINUTE]->fetchAll() as $data) { |
66
|
|
View Code Duplication |
for ($i = 0; $i<=59; $i++) { |
|
|
|
|
67
|
|
|
if ($i <= 9) { |
68
|
|
|
$index = 'm0'.$i; |
69
|
|
|
} |
70
|
|
|
else { |
71
|
|
|
$index = 'm'.$i; |
72
|
|
|
} |
73
|
|
|
$db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']][$data['day']][$data['hour']][$index] = $data[$index]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $db_events; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \Roomify\Bat\Event\Event $event |
82
|
|
|
* @param $granularity |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { |
87
|
|
|
$stored = TRUE; |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
// Itemize an event so we can save it |
91
|
|
|
$itemized = $event->itemizeEvent($granularity); |
92
|
|
|
|
93
|
|
|
// Write days |
94
|
|
|
foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
95
|
|
|
foreach ($months as $month => $days) { |
96
|
|
|
$values = array_values($days); |
97
|
|
|
$keys = array_keys($days); |
98
|
|
|
$this->pdo->exec("INSERT OR REPLACE INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . implode(', ', $values) . ")"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($granularity == Event::BAT_HOURLY) { |
103
|
|
|
// Write Hours |
104
|
|
|
foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
105
|
|
View Code Duplication |
foreach ($months as $month => $days) { |
|
|
|
|
106
|
|
|
foreach ($days as $day => $hours) { |
107
|
|
|
// Count required as we may receive empty hours for granular events that start and end on midnight |
108
|
|
|
if (count($hours) > 0) { |
109
|
|
|
$values = array_values($hours); |
110
|
|
|
$keys = array_keys($hours); |
111
|
|
|
$this->pdo->exec("INSERT OR REPLACE INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// If we have minutes write minutes |
118
|
|
|
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
119
|
|
|
foreach ($months as $month => $days) { |
120
|
|
View Code Duplication |
foreach ($days as $day => $hours) { |
|
|
|
|
121
|
|
|
foreach ($hours as $hour => $minutes) { |
122
|
|
|
$values = array_values($minutes); |
123
|
|
|
$keys = array_keys($minutes); |
124
|
|
|
$this->pdo->exec("INSERT OR REPLACE 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) . ")"); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
catch (\Exception $e) { |
132
|
|
|
$saved = FALSE; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $stored; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.