-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdatamanager.class.php
137 lines (123 loc) · 4.48 KB
/
datamanager.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
include_once("conf.php");
include_once("booking.class.php");
class DataManager
{
/**
* @param DateTime $start_date
* @param int $length_days
* @param array<string> $stations
* @return array<Booking>
*/
static function get_matched_station_array_bookings(DateTime $start_date, int $length_days, array $stations): array
{
$match_function = function (string $station_ident) use ($stations): bool {
for ($j = 0; $j < count($stations); $j++) {
if (str_contains($station_ident, $stations[$j])) {
return true;
}
}
return false;
};
return self::get_matched_bookings($start_date, $length_days, $match_function);
}
/**
* @param DateTime $start_date
* @param int $length_days
* @param $match_function
* @return array<Booking>
*/
static function get_matched_bookings(DateTime $start_date, int $length_days, $match_function): array
{
$atc_bookings = self::get_data($start_date, $length_days);
$requested_bookings = [];
foreach ($atc_bookings as $key => $booking) {
if (isset($booking->station)) {
$station_ident = $booking->station->ident;
if ($match_function($station_ident)) {
$requested_bookings[] = new Booking(
$booking->controller->username,
$booking->starts_at,
$booking->ends_at,
$station_ident,
$booking->training,
$booking->event
);
}
}
}
return $requested_bookings;
}
private static function get_data(DateTime $date_start, int $length_days): mixed
{
try {
return self::get_json($date_start, $length_days);
} catch (Exception $e) {
return [];
}
}
/**
* Get the bookings as json object
* @throws Exception
*/
private static function get_json(DateTime $date_start, int $length_days): mixed
{
$fileName = _BASE_PATH_ . "cache/booking_" . $date_start->format("Y-m-d") . ".json";
if (!file_exists($fileName) || filemtime($fileName) + 60 < time()) {
$response_json = self::do_curl_request($date_start, $length_days);
if ($response_json != false) {
file_put_contents($fileName, $response_json);
self::cleanup_cache();
}
}
$file_content = file_get_contents($fileName);
return json_decode($file_content);
}
/**
* @throws Exception
*/
private static function do_curl_request(DateTime $date_start, int $length_days): bool|string
{
$curl = curl_init();
$date_start_string = $date_start->format("Y-m-d");
$date_end = $date_start->add(new DateInterval('P' . $length_days . 'D'));
$date_end_string = $date_end->format("Y-m-d");
$url = API_PATH . "booking/$date_start_string/$date_end_string";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_FOLLOWLOCATION => true,
//CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
'X-Requested-With: XMLHttpRequest',
'Content-Type: application/json',
'Sec-Fetch-Mode: cors',
'Sec-Fetch-Site: same-origin',
'Sec-Fetch-Dest: empty',
'Accept-Language: de,en;q=0.9',
'Accept: application/json, text/plain, */*',
'Authorization: Token ' . API_KEY
),
));
$response = curl_exec($curl);
if (!$response) throw new Exception("Unable CURL REQ");
curl_close($curl);
return $response;
}
private static function cleanup_cache(): void
{
$files = scandir(_BASE_PATH_ . "cache/");
if (!$files) return;
foreach ($files as $f) {
$fileName = _BASE_PATH_ . "cache/" . $f;
if (is_dir($fileName)) continue;
if (filemtime($fileName) + 60 * 30 < time()) unlink($fileName);
}
}
}