1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class JsonStore |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Store; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\Event; |
11
|
|
|
use Roomify\Bat\Store\Store; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a JSON implementation of the Store. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class JsonStore extends Store { |
18
|
|
|
|
19
|
|
|
// There are two types of stores - for event ids and status |
20
|
|
|
const BAT_EVENT = 'event'; |
21
|
|
|
const BAT_STATE = 'state'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The file that holds day data. |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
public $day_file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The file that holds hour data. |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
public $hour_file; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The file that holds minute data. |
37
|
|
|
* @var |
38
|
|
|
*/ |
39
|
|
|
public $minute_file; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The event type we are dealing with. |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
public $event_type; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* JsonStore constructor. |
50
|
|
|
* |
51
|
|
|
* Provided with the event type it will determine the appropriate file names to |
52
|
|
|
* store data in. This assumes standard behaviour from Bat_Event |
53
|
|
|
* @param $event_type |
54
|
|
|
* @param string $event_data |
55
|
|
|
*/ |
56
|
|
|
public function __construct($event_type, $event_data = 'state') { |
57
|
|
|
$this->event_type = $event_type; |
58
|
|
|
|
59
|
|
View Code Duplication |
if ($event_data == JsonStore::BAT_STATE) { |
|
|
|
|
60
|
|
|
$this->day_file = 'build/' . $event_type . '_day_' . JsonStore::BAT_STATE . '.json'; |
61
|
|
|
$this->hour_file = 'build/' . $event_type . '_hour_' . JsonStore::BAT_STATE . '.json'; |
62
|
|
|
$this->minute_file = 'build/' . $event_type . '_minute_' . JsonStore::BAT_STATE . '.json'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if ($event_data == JsonStore::BAT_EVENT) { |
|
|
|
|
66
|
|
|
$this->day_file = 'build/' . $event_type . '_day_' . JsonStore::BAT_EVENT . '.json'; |
67
|
|
|
$this->hour_file = 'build/' . $event_type . '_hour_' . JsonStore::BAT_EVENT . '.json'; |
68
|
|
|
$this->minute_file = 'build/' . $event_type . '_minute_' . JsonStore::BAT_EVENT . '.json'; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @param \DateTime $start_date |
75
|
|
|
* @param \DateTime $end_date |
76
|
|
|
* @param $unit_ids |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
81
|
|
|
|
82
|
|
|
$dayfile = (array)json_decode(file_get_contents($this->day_file)); |
83
|
|
|
$hourfile = (array)json_decode(file_get_contents($this->hour_file)); |
84
|
|
|
$minutefile = (array)json_decode(file_get_contents($this->minute_file)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$events = array(); |
87
|
|
|
|
88
|
|
|
// Cycle through day results and setup an event array |
89
|
|
|
foreach ($dayfile as $unit_id => $row) { |
90
|
|
|
foreach ($row as $year => $row2) { |
91
|
|
|
foreach ($row2 as $month => $row3) { |
92
|
|
|
foreach ($row3 as $day => $value) { |
93
|
|
|
$events[$unit_id][Event::BAT_DAY][$year][$month][$day] = $value; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// With the day events taken care off let's cycle through hours |
100
|
|
View Code Duplication |
foreach ($hourfile as $unit_id => $row) { |
|
|
|
|
101
|
|
|
foreach ($row as $year => $row2) { |
102
|
|
|
foreach ($row2 as $month => $row3) { |
103
|
|
|
foreach ($row3 as $day => $row4) { |
104
|
|
|
foreach ($row4 as $hour => $value) { |
105
|
|
|
$events[$unit_id][Event::BAT_HOUR][$year][$month][$day][$hour] = $value; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// With the hour events taken care off let's cycle through minutes |
113
|
|
|
foreach ($hourfile as $unit_id => $row) { |
114
|
|
View Code Duplication |
foreach ($row as $year => $row2) { |
|
|
|
|
115
|
|
|
foreach ($row2 as $month => $row3) { |
116
|
|
|
foreach ($row3 as $day => $row4) { |
117
|
|
|
foreach ($row4 as $hour => $row5) { |
118
|
|
|
foreach ((array)$row5 as $min => $value) { |
119
|
|
|
$events[$unit_id][Event::BAT_MINUTE][$year][$month][$day][$hour][$min] = $value; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $events; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \Roomify\Bat\Event\Event $event |
132
|
|
|
* @param $granularity |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { |
137
|
|
|
$stored = TRUE; |
138
|
|
|
|
139
|
|
|
$dayfile_content = (array)json_decode(file_get_contents($this->day_file)); |
140
|
|
|
$hourfile_content = (array)json_decode(file_get_contents($this->hour_file)); |
141
|
|
|
$minutefile_content = (array)json_decode(file_get_contents($this->minute_file)); |
142
|
|
|
|
143
|
|
|
try { |
144
|
|
|
// Itemize an event so we can save it |
145
|
|
|
$itemized = $event->itemizeEvent($granularity); |
146
|
|
|
|
147
|
|
|
$dayfile_content[$event->unit_id] = $itemized[Event::BAT_DAY]; |
|
|
|
|
148
|
|
|
|
149
|
|
|
// Write days |
150
|
|
|
$dayfile = fopen($this->day_file, 'w'); |
151
|
|
|
fwrite($dayfile, json_encode($dayfile_content)); |
152
|
|
|
fclose($dayfile); |
153
|
|
|
|
154
|
|
|
if ($granularity == Event::BAT_HOURLY) { |
155
|
|
|
$hourfile_content[$event->unit_id] = $itemized[Event::BAT_HOUR]; |
|
|
|
|
156
|
|
|
|
157
|
|
|
// Write Hours |
158
|
|
|
$hourfile = fopen($this->hour_file, 'w'); |
159
|
|
|
fwrite($hourfile, json_encode($hourfile_content)); |
160
|
|
|
fclose($hourfile); |
161
|
|
|
|
162
|
|
|
$minutefile_content[$event->unit_id] = $itemized[Event::BAT_MINUTE]; |
|
|
|
|
163
|
|
|
|
164
|
|
|
// Write minutes |
165
|
|
|
$minutefile = fopen($this->minute_file, 'w'); |
166
|
|
|
fwrite($minutefile, json_encode($minutefile_content)); |
167
|
|
|
fclose($minutefile); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
catch (\Exception $e) { |
171
|
|
|
$saved = FALSE; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $stored; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
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.