From 5c916a636f71da08b765dfe1dc8b95d96157cb21 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sat, 1 Apr 2023 21:43:05 +0300 Subject: [PATCH] Retrieving variables bound in the container configuration from environment variables. --- .env | 11 +++++++++++ config/services.yaml | 16 +++++++--------- src/Command/UpdateSubscriptionsCommand.php | 10 +++++----- src/Controller/Api/CrawlerController.php | 4 ++-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/.env b/.env index ca88da4..a4085a4 100644 --- a/.env +++ b/.env @@ -38,3 +38,14 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 ###> symfony/mailer ### # MAILER_DSN=null://null ###< symfony/mailer ### + +# Point tools settings +APP_USER_ID=435 +APP_USER_LOGIN=point-tools +APP_POINT_DOMAIN=point.im +APP_POINT_SCHEME=https +APP_POINT_API_DELAY=5000 +APP_CRAWLER_SECRET=some_secret_for_crawler_change_it_right_away + +# Telegram Bot settings +TELEGRAM_BOT_TOKEN=123:abc diff --git a/config/services.yaml b/config/services.yaml index 1dd9769..a67400b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -11,21 +11,19 @@ services: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. bind: - # TODO: fix retrieval # Point Tools - $appUserId: 435 - $appUserLogin: 'point-tools' + $appUserId: '%env(int:APP_USER_ID)%' + $appUserLogin: 'env(string:APP_USER_LOGIN)' # Telegram Bot API - $telegramToken: '' + $telegramToken: 'env(string:TELEGRAM_BOT_TOKEN)' $debugEnabled: '%kernel.debug%' # Point API - $pointDomain: 'point.im' - $pointScheme: 'https' - $pointApiDelay: 5000 - $pointAppUserId: '' + $pointDomain: 'env(string:APP_POINT_DOMAIN)' + $pointScheme: 'env(string:APP_POINT_SCHEME)' + $pointApiDelay: '%env(int:APP_POINT_API_DELAY)%' $pointApiClient: '@app.point.http_client' # Crawler API - $crawlerToken: '' + $crawlerSecret: 'env(string:APP_CRAWLER_SECRET)' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/src/Command/UpdateSubscriptionsCommand.php b/src/Command/UpdateSubscriptionsCommand.php index 26b1ce4..ca16971 100644 --- a/src/Command/UpdateSubscriptionsCommand.php +++ b/src/Command/UpdateSubscriptionsCommand.php @@ -27,7 +27,7 @@ class UpdateSubscriptionsCommand extends Command private readonly UserApi $api, private readonly SubscriptionsManager $subscriptionManager, private readonly int $pointApiDelay, - private readonly int $pointAppUserId, + private readonly int $appUserId, ) { parent::__construct(); } @@ -155,9 +155,9 @@ class UpdateSubscriptionsCommand extends Command } else { /** @var User $serviceUser */ try { - $serviceUser = $this->userRepo->findActiveUserWithSubscribers($this->pointAppUserId); + $serviceUser = $this->userRepo->findActiveUserWithSubscribers($this->appUserId); } catch (\Exception $e) { - $this->logger->error('Error while getting active user with subscribers', ['app_user_id' => $this->pointAppUserId]); + $this->logger->error('Error while getting active user with subscribers', ['app_user_id' => $this->appUserId]); throw $e; } @@ -166,7 +166,7 @@ class UpdateSubscriptionsCommand extends Command $this->logger->warning('Service user not found or marked as removed. Falling back to API.'); try { - $serviceUser = $this->api->getUserById($this->pointAppUserId); + $serviceUser = $this->api->getUserById($this->appUserId); } catch (UserNotFoundException $e) { throw new \RuntimeException('Service user not found in the database and could not be retrieved from API.'); } @@ -175,7 +175,7 @@ class UpdateSubscriptionsCommand extends Command $this->logger->info('Getting service subscribers'); try { - $usersForUpdate = $this->api->getUserSubscribersById($this->pointAppUserId); + $usersForUpdate = $this->api->getUserSubscribersById($this->appUserId); } catch (UserNotFoundException $e) { $this->logger->critical('Service user deleted or API response is invalid'); diff --git a/src/Controller/Api/CrawlerController.php b/src/Controller/Api/CrawlerController.php index 98c3710..85d3422 100644 --- a/src/Controller/Api/CrawlerController.php +++ b/src/Controller/Api/CrawlerController.php @@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\{Request, Response}; class CrawlerController extends AbstractApiController { public function __construct( - private readonly string $crawlerToken, + private readonly string $crawlerSecret, ) { } @@ -20,7 +20,7 @@ class CrawlerController extends AbstractApiController { $remoteToken = $request->request->get('token'); - if (!$this->crawlerToken || ($this->crawlerToken !== $remoteToken)) { + if (!$this->crawlerSecret || ($this->crawlerSecret !== $remoteToken)) { return $this->createErrorResponse( 'Token error. Please check it in crawler and API parameters.', Response::HTTP_FORBIDDEN,