Create new class to handle app logic.
This commit is contained in:
28
index.php
28
index.php
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Kplaczek\FoodsiBot\BotService;
|
||||
use Kplaczek\FoodsiBot\FoodsiService;
|
||||
use Kplaczek\FoodsiBot\HomeassistantService;
|
||||
|
||||
@@ -10,29 +11,6 @@ $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->load();
|
||||
$client = new Client();
|
||||
$homeassistant = new HomeassistantService($client);
|
||||
if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') {
|
||||
return;
|
||||
}
|
||||
$foodsi = new FoodsiService($client);
|
||||
$packageInfo = $foodsi->getPackageInfo();
|
||||
$receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
|
||||
$notificationSent = false;
|
||||
foreach ($receivers as $receiver) {
|
||||
if (!file_exists('last_notification_date.txt')) {
|
||||
touch('last_notification_date.txt');
|
||||
}
|
||||
$lastNotificationDate = file_get_contents('last_notification_date.txt');
|
||||
if ($packageInfo['current_quantity'] > 0 && $lastNotificationDate !== date('Y-m-d')) {
|
||||
$homeassistant->sendNotification(
|
||||
$receiver,
|
||||
'Dostępna paczka w 3 kromki chleba',
|
||||
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
|
||||
);
|
||||
$notificationSent = true;
|
||||
}
|
||||
}
|
||||
if ($notificationSent) {
|
||||
file_put_contents('last_notification_date.txt', date('Y-m-d'));
|
||||
$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
|
||||
}
|
||||
$homeassistant->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
|
||||
$bot = new BotService($homeassistant, $foodsi);
|
||||
$bot->run();
|
||||
|
||||
70
src/BotService.php
Normal file
70
src/BotService.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Kplaczek\FoodsiBot;
|
||||
|
||||
class BotService
|
||||
{
|
||||
private bool $notificationSent = false;
|
||||
|
||||
public function __construct(
|
||||
private HomeassistantService $homeassistantService,
|
||||
private FoodsiService $foodsiService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (!$this->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
$packageInfo = $this->foodsiService->getPackageInfo();
|
||||
foreach ($this->getReceivers() as $receiver) {
|
||||
$this->initializeNotificationLog();
|
||||
if ($this->shouldSendNotification($packageInfo)) {
|
||||
$this->homeassistantService->sendNotification(
|
||||
$receiver,
|
||||
'Dostępna paczka w 3 kromki chleba',
|
||||
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
|
||||
);
|
||||
$this->notificationSent = true;
|
||||
}
|
||||
}
|
||||
if ($this->notificationSent) {
|
||||
$this->updateNotificationLog();
|
||||
$this->homeassistantService->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
|
||||
}
|
||||
$this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
|
||||
}
|
||||
|
||||
private function isEnabled(): bool
|
||||
{
|
||||
return $this->homeassistantService->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'on';
|
||||
}
|
||||
|
||||
private function getReceivers(): array
|
||||
{
|
||||
return explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
|
||||
}
|
||||
|
||||
private function initializeNotificationLog(): void
|
||||
{
|
||||
if (!file_exists('last_notification_date.txt')) {
|
||||
touch('last_notification_date.txt');
|
||||
}
|
||||
}
|
||||
|
||||
private function updateNotificationLog(): void
|
||||
{
|
||||
file_put_contents('last_notification_date.txt', date('Y-m-d'));
|
||||
}
|
||||
|
||||
private function getNotificationLog(): string
|
||||
{
|
||||
return file_get_contents('last_notification_date.txt');
|
||||
}
|
||||
|
||||
private function shouldSendNotification(array $packageInfo): bool
|
||||
{
|
||||
return $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user