Skip to content

Commit 5799372

Browse files
authored
Create UserLocation.php
1 parent 7e032a8 commit 5799372

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/Storage/UserLocation.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/*
3+
the project created by @wizardloop
4+
*/
5+
namespace JewishPulse\Storage;
6+
7+
use Amp\File;
8+
use function Amp\File\read;
9+
use function Amp\File\write;
10+
use function Amp\File\exists;
11+
12+
use Amp\ByteStream\Payload;
13+
use Amp\Http\Client\HttpClientBuilder;
14+
use Amp\Http\Client\Request;
15+
16+
class UserLocation
17+
{
18+
private static string $dataDir = __DIR__ . '/../data';
19+
20+
public static function getGeonameId(int $senderId): string {
21+
$file = self::$dataDir . "/$senderId/location.json";
22+
23+
if (!exists($file)) {
24+
return 281184;
25+
}
26+
27+
$data = json_decode(read($file), true);
28+
29+
return $data['geonameid'] ?? 281184;
30+
}
31+
32+
public static function setGeonameId(int $userId, int $geonameId): void {
33+
$file = self::$dataDir . "/$userId/location.json";
34+
35+
$data = [
36+
'geonameid' => $geonameId,
37+
'updated_at' => date('c')
38+
];
39+
40+
$userDir = dirname($file);
41+
if (!is_dir($userDir)) {
42+
mkdir($userDir, 0777, true);
43+
}
44+
45+
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
46+
}
47+
48+
public static function searchGeoNames(string $query): array {
49+
try {
50+
$env = parse_ini_file(__DIR__ . '/../.env');
51+
$client = HttpClientBuilder::buildDefault();
52+
$username = $env['GEONAMES_USERNAME'];
53+
$url = "http://api.geonames.org/searchJSON?q=" . urlencode($query) . "&maxRows=10&username=$username";
54+
$response = $client->request(new Request($url));
55+
$body = json_decode($response->getBody()->buffer(), true);
56+
57+
return $body['geonames'] ?? [];
58+
} catch (\Throwable $e) {
59+
return [];
60+
}
61+
}
62+
63+
public static function getCandleSetting(int $senderId): int {
64+
$file = self::$dataDir . "/$senderId/candles.json";
65+
66+
if (!file_exists($file)) {
67+
return 40;
68+
}
69+
70+
$data = json_decode(read($file), true);
71+
72+
return $data['candles'] ?? 40;
73+
}
74+
75+
public static function setCandleSetting(int $senderId, int $candlesMinutes): void {
76+
$file = self::$dataDir . "/$senderId/candles.json";
77+
78+
$data = [
79+
'candles' => $candlesMinutes,
80+
'updated_at' => date('c')
81+
];
82+
83+
$userDir = dirname($file);
84+
if (!is_dir($userDir)) {
85+
mkdir($userDir, 0777, true);
86+
}
87+
88+
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
89+
}
90+
91+
public static function getHavdalahSetting(int $senderId): float {
92+
$file = self::$dataDir . "/$senderId/havdalah.json";
93+
94+
if (!file_exists($file)) {
95+
return 8.5;
96+
}
97+
98+
$data = json_decode(read($file), true);
99+
100+
return $data['havdalah'] ?? 8.5;
101+
}
102+
103+
public static function setHavdalahSetting(int $senderId, float $havdalahHours): void {
104+
$file = self::$dataDir . "/$senderId/havdalah.json";
105+
106+
$data = [
107+
'havdalah' => $havdalahHours,
108+
'updated_at' => date('c')
109+
];
110+
111+
$userDir = dirname($file);
112+
if (!is_dir($userDir)) {
113+
mkdir($userDir, 0777, true);
114+
}
115+
116+
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
117+
}
118+
119+
}

0 commit comments

Comments
 (0)