1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class SqlLiteDBStore |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Store; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\EventInterface; |
11
|
|
|
use Roomify\Bat\Event\Event; |
12
|
|
|
use Roomify\Bat\Event\EventItemizer; |
13
|
|
|
use Roomify\Bat\Store\SqlDBStore; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is a SqlLite implementation of the Store. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class SqlLiteDBStore extends SqlDBStore { |
20
|
|
|
|
21
|
|
|
protected $pdo; |
22
|
|
|
|
23
|
|
|
public function __construct(\PDO $pdo, $event_type, $event_data = 'state') { |
24
|
|
|
parent::__construct($event_type, $event_data); |
25
|
|
|
|
26
|
|
|
$this->pdo = $pdo; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @param \DateTime $start_date |
32
|
|
|
* @param \DateTime $end_date |
33
|
|
|
* @param $unit_ids |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
38
|
|
|
|
39
|
|
|
$queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
40
|
|
|
|
41
|
|
|
$results = array(); |
42
|
|
|
// Run each query and store results |
43
|
|
|
foreach ($queries as $type => $query) { |
44
|
|
|
$results[$type] = $this->pdo->query($query); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$db_events = array(); |
48
|
|
|
|
49
|
|
|
// Cycle through day results and setup an event array |
50
|
|
|
foreach ($results[Event::BAT_DAY]->fetchAll() as $data) { |
51
|
|
|
// Figure out how many days the current month has |
52
|
|
|
$temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
53
|
|
|
$days_in_month = (int)$temp_date->format('t'); |
54
|
|
View Code Duplication |
for ($i = 1; $i <= $days_in_month; $i++) { |
|
|
|
|
55
|
|
|
$db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d' . $i]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// With the day events taken care off let's cycle through hours |
60
|
|
|
foreach ($results[Event::BAT_HOUR]->fetchAll() as $data) { |
61
|
|
View Code Duplication |
for ($i = 0; $i <= 23; $i++) { |
|
|
|
|
62
|
|
|
$db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']]['d' . $data['day']]['h' . $i] = $data['h' . $i]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// With the hour events taken care off let's cycle through minutes |
67
|
|
|
foreach ($results[Event::BAT_MINUTE]->fetchAll() as $data) { |
68
|
|
View Code Duplication |
for ($i = 0; $i <= 59; $i++) { |
|
|
|
|
69
|
|
|
if ($i <= 9) { |
70
|
|
|
$index = 'm0' . $i; |
71
|
|
|
} else { |
72
|
|
|
$index = 'm' . $i; |
73
|
|
|
} |
74
|
|
|
$db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']]['d' . $data['day']]['h' . $data['hour']][$index] = $data[$index]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $db_events; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Roomify\Bat\Event\EventInterface $event |
83
|
|
|
* @param $granularity |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOURLY) { |
88
|
|
|
$stored = TRUE; |
89
|
|
|
$unit_id = $event->getUnitId(); |
90
|
|
|
|
91
|
|
|
// Get existing event data from db |
92
|
|
|
$existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($unit_id)); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
// Itemize an event so we can save it |
96
|
|
|
$itemized = $event->itemize(new EventItemizer($event, $granularity)); |
97
|
|
|
|
98
|
|
|
// Write days |
99
|
|
|
foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
100
|
|
|
foreach ($months as $month => $days) { |
101
|
|
|
$values = array_values($days); |
102
|
|
|
$keys = array_keys($days); |
103
|
|
|
// 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 |
104
|
|
|
if (isset($existing_events[$unit_id][EVENT::BAT_DAY][$year][$month])) { |
105
|
|
|
$command = "UPDATE $this->day_table SET "; |
106
|
|
|
foreach ($days as $day => $value) { |
107
|
|
|
if ($granularity === Event::BAT_HOURLY) { |
108
|
|
|
$this->itemizeSplitDay($existing_events, $itemized, $value, $unit_id, $year, $month, $day); |
109
|
|
|
} |
110
|
|
|
$command .= "$day = $value,"; |
111
|
|
|
} |
112
|
|
|
$command = rtrim($command, ','); |
113
|
|
|
$command .= " WHERE unit_id = " . $unit_id . " AND year = $year AND month = $month"; |
114
|
|
|
$this->pdo->exec($command); |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
$this->pdo->exec("INSERT INTO $this->day_table (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $unit_id . ", $year, $month, " . implode(', ', $values) . ")"); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (($granularity == Event::BAT_HOURLY) && isset($itemized[Event::BAT_HOUR])) { |
123
|
|
|
// Write Hours |
124
|
|
|
foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
125
|
|
|
foreach ($months as $month => $days) { |
126
|
|
|
foreach ($days as $day => $hours) { |
127
|
|
|
// Count required as we may receive empty hours for granular events that start and end on midnight |
128
|
|
|
if (count($hours) > 0) { |
129
|
|
|
$values = array_values($hours); |
130
|
|
|
$keys = array_keys($hours); |
131
|
|
|
if (isset($existing_events[$unit_id][EVENT::BAT_HOUR][$year][$month][$day])) { |
132
|
|
|
$command = "UPDATE $this->hour_table SET "; |
133
|
|
|
foreach ($hours as $hour => $value){ |
134
|
|
|
$this->itemizeSplitHour($existing_events, $itemized, $value, $unit_id, $year, $month, $day, $hour); |
135
|
|
|
$command .= "$hour = $value,"; |
136
|
|
|
} |
137
|
|
|
$command = rtrim($command, ','); |
138
|
|
|
$command .= " WHERE unit_id = " . $unit_id . " AND year = $year AND month = $month AND day = " . substr($day,1); |
139
|
|
|
$this->pdo->exec($command); |
140
|
|
|
} else { |
141
|
|
|
if (isset($existing_events[$unit_id][EVENT::BAT_DAY][$year][$month][$day])) { |
142
|
|
|
foreach ($hours as $hour => $value) { |
143
|
|
|
$this->itemizeSplitHour($existing_events, $itemized, $value, $unit_id, $year, $month, $day, $hour); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
$this->pdo->exec("INSERT INTO $this->hour_table (unit_id, year, month, day, " . implode(', ', $keys) . ") VALUES (" . $unit_id . ", $year, $month, " . substr($day, 1) . ", " . implode(', ', $values) . ")"); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// If we have minutes write minutes |
154
|
|
|
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
155
|
|
|
foreach ($months as $month => $days) { |
156
|
|
|
foreach ($days as $day => $hours) { |
157
|
|
|
foreach ($hours as $hour => $minutes) { |
158
|
|
|
$values = array_values($minutes); |
159
|
|
|
$keys = array_keys($minutes); |
160
|
|
|
if (isset($existing_events[$unit_id][EVENT::BAT_MINUTE][$year][$month][$day][$hour])) { |
161
|
|
|
$command = "UPDATE $this->minute_table SET "; |
162
|
|
|
foreach ($minutes as $minute => $value){ |
163
|
|
|
$command .= "$minute = $value,"; |
164
|
|
|
} |
165
|
|
|
$command = rtrim($command, ','); |
166
|
|
|
$command .= " WHERE unit_id = " . $unit_id . " AND year = $year AND month = $month AND day = " . substr($day,1) . " AND hour = " . substr($hour,1); |
167
|
|
|
$this->pdo->exec($command); |
168
|
|
|
} else { |
169
|
|
|
$this->pdo->exec("INSERT INTO $this->minute_table (unit_id, year, month, day, hour, " . implode(', ', $keys) . ") VALUES (" . $unit_id . ", $year, $month, " . substr($day, 1) . ", " . substr($hour, 1) . ", " . implode(', ', $values) . ")"); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} catch (\Exception $e) { |
177
|
|
|
$stored = FALSE; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $stored; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
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.