diff --git a/.env.dist b/.env.dist index 59ea4c2..5119413 100644 --- a/.env.dist +++ b/.env.dist @@ -47,4 +47,24 @@ CREDIT_SYSTEM_LONG_RENTAL_FEE=5 #credit needed to temporarily increase limit, applicable only when $limits["increase"]>0 CREDIT_SYSTEM_LIMIT_INCREASE_FEE=10 #credit deduction for rule violations (applied by admins) -CREDIT_SYSTEM_VIOLATION_FEE=5 \ No newline at end of file +CREDIT_SYSTEM_VIOLATION_FEE=5 + +#notification email for notifications such as notes etc., blank if notifications not required +WATCHES_EMAIL=email@example.com +#0 - do not watch stack +#1 - notify if other than the top of the stack bike is rented from a stand (independent from forcestack) +WATCHES_STACK=1 +#in hours (bike rented for more than X h) +WATCHES_LONG_RENTAL=24 +#in hours (high number of rentals by one person in a short time) +WATCHES_TIME_TOO_MANY=1 +#if userlimit+numbertooomany reached in timetoomany, then notify +WATCHES_NUMBER_TOO_MANY=1 +#in minutes (rental changes from free to paid after this time and $credit["rent"] is deducted) +WATCHES_FREE_TIME=30 +#in minutes (uses flat price $credit["rent"] every $watches["flatpricecycle"] minutes after first paid period, i.e. $watches["freetime"]*2) +WATCHES_FLAT_PRICE_CYCLE=60 +#in minutes (doubles the rental price $credit["rent"] every $watches["doublepricecycle"] minutes after first paid period, i.e. $watches["freetime"]*2) +WATCHES_DOUBLE_PRICE_CYCLE=60 +#number of cycles after doubling of rental price $credit["rent"] is capped and stays flat (but reached cycle multiplier still applies) +WATCHES_DOUBLE_PRICE_CYCLE_CAP=3 diff --git a/config.example.php b/config.example.php index 178c117..bc882e0 100644 --- a/config.example.php +++ b/config.example.php @@ -12,15 +12,6 @@ $systemrules="http://example.com/rules.htm"; // system rules / help URL $forcestack=0; // 0 = allow renting any bike at stand, 1 = allow renting last bicycle returned only (top of stack) -$watches["email"]="email@example.com"; // notification email for notifications such as notes etc., blank if notifications not required -$watches["stack"]=1; // 0 - do not watch stack, 1 - notify if other than the top of the stack bike is rented from a stand (independent from forcestack) -$watches["longrental"]=24; // in hours (bike rented for more than X h) -$watches["timetoomany"]=1; // in hours (high number of rentals by one person in a short time) -$watches["numbertoomany"]=1; // if userlimit+numbertooomany reached in timetoomany, then notify -$watches["freetime"]=30; // in minutes (rental changes from free to paid after this time and $credit["rent"] is deducted) -$watches["flatpricecycle"]=60; // in minutes (uses flat price $credit["rent"] every $watches["flatpricecycle"] minutes after first paid period, i.e. $watches["freetime"]*2) -$watches["doublepricecycle"]=60; // in minutes (doubles the rental price $credit["rent"] every $watches["doublepricecycle"] minutes after first paid period, i.e. $watches["freetime"]*2) -$watches["doublepricecyclecap"]=3; // number of cycles after doubling of rental price $credit["rent"] is capped and stays flat (but reached cycle multiplier still applies) $limits["registration"]=0; // number of bikes user can rent after he registered: 0 = no bike, 1 = 1 bike etc. $limits["increase"]=0; // allow more bike rentals in addition to user limit: 0 = not allowed, otherwise: temporary limit increase - number of bikes diff --git a/config/services.php b/config/services.php index 939335a..253a654 100644 --- a/config/services.php +++ b/config/services.php @@ -71,11 +71,15 @@ ]); $services->get(\BikeShare\Command\LongRentalCheckCommand::class) - ->bind('$notifyUser', env('bool:NOTIFY_USER_ABOUT_LONG_RENTAL')); + ->bind('$notifyUser', env('bool:NOTIFY_USER_ABOUT_LONG_RENTAL')) + ->bind('$longRentalHours', env('int:WATCHES_LONG_RENTAL')); $services->get(\BikeShare\Controller\SmsRequestController::class) ->bind('$commandLocator', tagged_locator('smsCommand', null, 'getName')); + $services->get(\BikeShare\Controller\HomeController::class) + ->bind('$freeTimeHours', env('int:WATCHES_FREE_TIME')); + $services->get(\BikeShare\SmsCommand\CommandExecutor::class) ->bind('$commandLocator', tagged_locator('smsCommand', null, 'getName')); @@ -124,7 +128,17 @@ ->bind('$violationFee', env('float:CREDIT_SYSTEM_VIOLATION_FEE')); $services->load('BikeShare\\Rent\\', '../src/Rent') - ->bind('$watchesConfig', expr("service('BikeShare\\\App\\\Configuration').get('watches')")) + ->bind( + '$watchesConfig', + [ + 'stack' => env('int:WATCHES_STACK'), + 'longrental' => env('int:WATCHES_LONG_RENTAL'), + 'freetime' => env('int:WATCHES_FREE_TIME'), + 'flatpricecycle' => env('int:WATCHES_FLAT_PRICE_CYCLE'), + 'doublepricecycle' => env('int:WATCHES_DOUBLE_PRICE_CYCLE'), + 'doublepricecyclecap' => env('int:WATCHES_DOUBLE_PRICE_CYCLE_CAP'), + ] + ) ->bind('$forceStack', expr("service('BikeShare\\\App\\\Configuration').get('forceStack')")); $services->load('BikeShare\\SmsConnector\\', '../src/SmsConnector') @@ -141,4 +155,8 @@ $services->load('BikeShare\\EventListener\\', '../src/EventListener') ->tag('kernel.event_listener'); + + $services->get(\BikeShare\EventListener\TooManyBikeRentEventListener::class) + ->bind('$timeTooManyHours', env('int:WATCHES_TIME_TOO_MANY')) + ->bind('$numberToMany', env('int:WATCHES_NUMBER_TOO_MANY')); }; diff --git a/src/Command/LongRentalCheckCommand.php b/src/Command/LongRentalCheckCommand.php index e61f602..522cd4c 100644 --- a/src/Command/LongRentalCheckCommand.php +++ b/src/Command/LongRentalCheckCommand.php @@ -4,7 +4,6 @@ namespace BikeShare\Command; -use BikeShare\App\Configuration; use BikeShare\Notifier\AdminNotifier; use BikeShare\Repository\BikeRepository; use BikeShare\Repository\HistoryRepository; @@ -22,9 +21,9 @@ class LongRentalCheckCommand extends Command protected static $defaultName = 'app:long_rental_check'; private bool $notifyUser; + private int $longRentalHours; private BikeRepository $bikeRepository; private HistoryRepository $historyRepository; - private Configuration $configuration; private SmsSenderInterface $smsSender; private TranslatorInterface $translator; private AdminNotifier $adminNotifier; @@ -32,18 +31,18 @@ class LongRentalCheckCommand extends Command public function __construct( bool $notifyUser, + int $longRentalHours, BikeRepository $bikeRepository, HistoryRepository $historyRepository, - Configuration $configuration, SmsSenderInterface $smsSender, TranslatorInterface $translator, AdminNotifier $adminNotifier, LoggerInterface $logger ) { $this->notifyUser = $notifyUser; + $this->longRentalHours = $longRentalHours; $this->bikeRepository = $bikeRepository; $this->historyRepository = $historyRepository; - $this->configuration = $configuration; $this->smsSender = $smsSender; $this->translator = $translator; $this->adminNotifier = $adminNotifier; @@ -70,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int continue; } $time = strtotime($lastRent['time']); - if ($time + ($this->configuration->get('watches')['longrental'] * 3600) <= time()) { + if ($time + ($this->longRentalHours * 3600) <= time()) { $abusers[] = [ 'userId' => $userId, 'bikeNumber' => $bikeNumber, @@ -92,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!empty($abusers)) { $message = $this->translator->trans( 'Bike rental exceed {hour} hours', - ['hour' => $this->configuration->get('watches')['longrental']] + ['hour' => $this->longRentalHours] ); foreach ($abusers as $abuser) { $message .= PHP_EOL . 'B' . $abuser['bikeNumber'] . ' ' diff --git a/src/Controller/Api/UserController.php b/src/Controller/Api/UserController.php index a6e5802..b9353fa 100644 --- a/src/Controller/Api/UserController.php +++ b/src/Controller/Api/UserController.php @@ -4,7 +4,6 @@ namespace BikeShare\Controller\Api; -use BikeShare\App\Configuration; use BikeShare\Repository\UserRepository; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -73,7 +72,6 @@ public function update( bool $isSmsSystemEnabled, Request $request, UserRepository $userRepository, - Configuration $configuration, LoggerInterface $logger ): Response { if (!$this->isGranted('ROLE_ADMIN')) { diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 6432e1d..ddd9f5e 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -16,11 +16,13 @@ class HomeController extends AbstractController */ public function index( Request $request, + int $freeTimeHours, CreditSystemInterface $creditSystem, Configuration $configuration ): Response { return $this->render('index.html.twig', [ 'configuration' => $configuration, + 'freeTime' => $freeTimeHours, 'creditSystem' => $creditSystem, 'error' => $request->get('error', null), ]); diff --git a/src/EventListener/TooManyBikeRentEventListener.php b/src/EventListener/TooManyBikeRentEventListener.php index 7e8f3ec..0298609 100644 --- a/src/EventListener/TooManyBikeRentEventListener.php +++ b/src/EventListener/TooManyBikeRentEventListener.php @@ -4,7 +4,6 @@ namespace BikeShare\EventListener; -use BikeShare\App\Configuration; use BikeShare\Event\BikeRentEvent; use BikeShare\Notifier\AdminNotifier; use BikeShare\Repository\HistoryRepository; @@ -13,22 +12,25 @@ class TooManyBikeRentEventListener { + private int $timeTooManyHours; + private int $numberToMany; private UserRepository $userRepository; private HistoryRepository $historyRepository; - private Configuration $configuration; private TranslatorInterface $translator; private AdminNotifier $adminNotifier; public function __construct( + int $timeTooManyHours, + int $numberToMany, UserRepository $userRepository, HistoryRepository $historyRepository, - Configuration $configuration, TranslatorInterface $translator, AdminNotifier $adminNotifier ) { + $this->timeTooManyHours = $timeTooManyHours; + $this->numberToMany = $numberToMany; $this->userRepository = $userRepository; $this->historyRepository = $historyRepository; - $this->configuration = $configuration; $this->translator = $translator; $this->adminNotifier = $adminNotifier; } @@ -38,14 +40,14 @@ public function __invoke(BikeRentEvent $event): void $user = $this->userRepository->findItem($event->getUserId()); $offsetTime = date( 'Y-m-d H:i:s', - time() - $this->configuration->get('watches')['timetoomany'] * 3600 + time() - $this->timeTooManyHours * 3600 ); $rentCount = $this->historyRepository->findRentCountByUser($event->getUserId(), $offsetTime); - if ($rentCount >= ($user['userLimit'] + $this->configuration->get('watches')['numbertoomany'])) { + if ($rentCount >= ($user['userLimit'] + $this->numberToMany)) { $message = $this->translator->trans( 'Bike rental over limit in {hour} hours', - ['hour' => $this->configuration->get('watches')['timetoomany']] + ['hour' => $this->timeTooManyHours] ); $message .= PHP_EOL . $this->translator->trans( '{userName} ({phone}) rented {count} bikes', diff --git a/src/Rent/AbstractRentSystem.php b/src/Rent/AbstractRentSystem.php index 7879de9..8868bfc 100644 --- a/src/Rent/AbstractRentSystem.php +++ b/src/Rent/AbstractRentSystem.php @@ -391,7 +391,8 @@ private function changecreditendrental($bike, $userid) $this->watchesConfig['freetime'] = 1; } // for further calculations - if ($this->creditSystem->getPriceCycle() && $timediff > $this->watchesConfig['freetime'] * 60 * 2) { // after first paid period, i.e. freetime*2; if pricecycle enabled + if ($this->creditSystem->getPriceCycle() && $timediff > $this->watchesConfig['freetime'] * 60 * 2) { + // after first paid period, i.e. freetime*2; if pricecycle enabled $temptimediff = $timediff - ($this->watchesConfig['freetime'] * 60 * 2); if ($this->creditSystem->getPriceCycle() == 1) { // flat price per cycle $cycles = ceil($temptimediff / ($this->watchesConfig['flatpricecycle'] * 60)); diff --git a/templates/index.html.twig b/templates/index.html.twig index 16c2680..09dc3e0 100644 --- a/templates/index.html.twig +++ b/templates/index.html.twig @@ -47,7 +47,7 @@ var standselected = 0; var creditsystem = {{ creditSystem.isEnabled ? 1 : 0 }}; - var freeTimeSeconds = {{ configuration.watches.freetime * 60 }}; // and convert to seconds + var freeTimeSeconds = {{ freeTime * 60 }}; // and convert to seconds var serverTimeSeconds = {{ date().timestamp }}; // using the server timestamp for time difference calculation