From c8ff5cb067bfd1b00f77da8aedbec1265ec25b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Tue, 12 Nov 2024 16:03:22 +0100 Subject: [PATCH] Create new class to handle app logic. --- index.php | 28 ++----------------- src/BotService.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/BotService.php diff --git a/index.php b/index.php index c999106..72c41e0 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ 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(); diff --git a/src/BotService.php b/src/BotService.php new file mode 100644 index 0000000..b2ee43e --- /dev/null +++ b/src/BotService.php @@ -0,0 +1,70 @@ +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'); + } +}