Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move watches config to env #231

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
CREDIT_SYSTEM_VIOLATION_FEE=5

#notification email for notifications such as notes etc., blank if notifications not required
[email protected]
#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
9 changes: 0 additions & 9 deletions config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]"; // 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
Expand Down
22 changes: 20 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down Expand Up @@ -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')
Expand All @@ -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'));
};
11 changes: 5 additions & 6 deletions src/Command/LongRentalCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace BikeShare\Command;

use BikeShare\App\Configuration;
use BikeShare\Notifier\AdminNotifier;
use BikeShare\Repository\BikeRepository;
use BikeShare\Repository\HistoryRepository;
Expand All @@ -22,28 +21,28 @@ 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;
private LoggerInterface $logger;

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;
Expand All @@ -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,
Expand All @@ -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'] . ' '
Expand Down
2 changes: 0 additions & 2 deletions src/Controller/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +72,6 @@ public function update(
bool $isSmsSystemEnabled,
Request $request,
UserRepository $userRepository,
Configuration $configuration,
LoggerInterface $logger
): Response {
if (!$this->isGranted('ROLE_ADMIN')) {
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
Expand Down
16 changes: 9 additions & 7 deletions src/EventListener/TooManyBikeRentEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace BikeShare\EventListener;

use BikeShare\App\Configuration;
use BikeShare\Event\BikeRentEvent;
use BikeShare\Notifier\AdminNotifier;
use BikeShare\Repository\HistoryRepository;
Expand All @@ -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;
}
Expand All @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/Rent/AbstractRentSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
</script>
</head>
Expand Down
Loading