Skip to content

Commit

Permalink
RentSystem add response method for every class, due to the difference
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 17, 2024
1 parent e72d3bf commit c60f20b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 68 deletions.
8 changes: 8 additions & 0 deletions src/Rent/AbstractRentSystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BikeShare\Rent;

abstract class AbstractRentSystem
{
abstract protected function response($message, $error = 0, $additional = '', $log = 1);
}
69 changes: 49 additions & 20 deletions src/Rent/RentSystemQR.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BikeShare\Rent;

class RentSystemQR implements RentSystemInterface
class RentSystemQR extends AbstractRentSystem implements RentSystemInterface
{
public function rentBike($userId, $bike, $force = false)
{
Expand All @@ -14,16 +14,13 @@ public function rentBike($userId, $bike, $force = false)

$result = $db->query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum");
if ($result->num_rows != 1) {
response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR);
return;
return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR);
}

if ($force == false) {
if (!$creditSystem->isEnoughCreditForRent($userId)) {
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR);

return;
return $this->response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR);
}

checktoomany(0, $userId);
Expand All @@ -38,11 +35,11 @@ public function rentBike($userId, $bike, $force = false)

if ($countRented >= $limit) {
if ($limit == 0) {
response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR);
return $this->response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR);
} elseif ($limit == 1) {
response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR);
return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR);
} else {
response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR);
return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR);
}
}

Expand All @@ -57,7 +54,7 @@ public function rentBike($userId, $bike, $force = false)
$serviceTag = $row['serviceTag'];

if ($serviceTag != 0) {
response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR);
return $this->response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR);
}

if ($watches['stack'] and $stacktopbike != $bike) {
Expand All @@ -68,7 +65,7 @@ public function rentBike($userId, $bike, $force = false)
notifyAdmins(_('Bike') . ' ' . $bike . ' ' . _('rented out of stack by') . ' ' . $userName . '. ' . $stacktopbike . ' ' . _('was on the top of the stack at') . ' ' . $stand . '.', ERROR);
}
if ($forcestack and $stacktopbike != $bike) {
response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR);
return $this->response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR);
}
}
}
Expand All @@ -88,12 +85,10 @@ public function rentBike($userId, $bike, $force = false)

if ($force == false) {
if ($currentUser == $userId) {
response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR);
return;
return $this->response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR);
}
if ($currentUser != 0) {
response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR);
return;
return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR);
}
}

Expand All @@ -108,7 +103,7 @@ public function rentBike($userId, $bike, $force = false)
} else {
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERENT',parameter=$newCode");
}
response($message);
return $this->response($message);
}

public function returnBike($userId, $bike, $stand, $note = '', $force = false)
Expand All @@ -122,7 +117,7 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false)

$result = $db->query("SELECT standId FROM stands WHERE standName='$stand'");
if (!$result->num_rows) {
response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR);
return $this->response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR);
}
$row = $result->fetch_assoc();
$standId = $row["standId"];
Expand All @@ -132,14 +127,14 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false)
$bikenumber = $result->num_rows;

if ($bikenumber == 0) {
response(_('You currently have no rented bikes.'), ERROR);
return $this->response(_('You currently have no rented bikes.'), ERROR);
} elseif ($bikenumber > 1) {
$message = _('You have') . ' ' . $bikenumber . ' ' . _('rented bikes currently. QR code return can be used only when 1 bike is rented. Please, use web');
if ($connectors["sms"]) {
$message .= _(' or SMS');
}
$message .= _(' to return the bikes.');
response($message, ERROR);
return $this->response($message, ERROR);
}
}

Expand Down Expand Up @@ -173,6 +168,40 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false)
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERETURN',parameter=$standId");
}

response($message);
return $this->response($message);
}

protected function response($message, $error = 0, $additional = '', $log = 1)
{
global $db, $systemname, $systemURL, $user, $auth;
if ($log == 1 and $message) {
$userid = $auth->getUserId();
$number = $user->findPhoneNumber($userid);
logresult($number, $message);
}
$db->commit();
echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>',$systemname,'</title>';
echo '<base href="',$systemURL,'" />';
echo '<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />';
echo '<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />';
echo '<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">';
echo '<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">';
echo '<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">';
echo '<link rel="manifest" href="/site.webmanifest">';
echo '<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">';
echo '<meta name="msapplication-TileColor" content="#da532c">';
echo '<meta name="theme-color" content="#ffffff">';
if (file_exists("analytics.php")) require("analytics.php");
echo '</head><body><div class="container">';
if ($error)
{
echo '<div class="alert alert-danger" role="alert">',$message,'</div>';
}
else
{
echo '<div class="alert alert-success" role="alert">',$message,'</div>';
}
echo '</div></body></html>';
exit;
}
}
60 changes: 31 additions & 29 deletions src/Rent/RentSystemSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@

namespace BikeShare\Rent;

class RentSystemSms implements RentSystemInterface
class RentSystemSms extends AbstractRentSystem implements RentSystemInterface
{
private $number;

public function rentBike($number, $bike, $force = false)
{
global $db, $forcestack, $watches, $smsSender, $user, $creditSystem;

$this->number = $number;

$stacktopbike = false;
$userId = $user->findUserIdByNumber($number);

$bikeNum = intval($bike);

$result = $db->query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum");
if ($result->num_rows != 1) {
$smsSender->send($number, "Bike $bikeNum does not exist.");
return;
return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR);
}

if ($force == false) {
if (!$creditSystem->isEnoughCreditForRent($userId)) {
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
$userRemainingCredit = $creditSystem->getUserCredit($userId);
$smsSender->send(
$number,
return $this->response(
_('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . "."
);
return;
}

checktoomany(0, $userId);
Expand All @@ -42,13 +43,12 @@ public function rentBike($number, $bike, $force = false)

if ($countRented >= $limit) {
if ($limit == 0) {
$smsSender->send($number, _('You can not rent any bikes. Contact the admins to lift the ban.'));
return $this->response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR);
} elseif ($limit == 1) {
$smsSender->send($number, _('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.');
return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR);
} else {
$smsSender->send($number, _('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.');
return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR);
}
return;
}

if ($forcestack or $watches['stack']) {
Expand All @@ -62,8 +62,7 @@ public function rentBike($number, $bike, $force = false)
$serviceTag = $row['serviceTag'];

if ($serviceTag != 0) {
$smsSender->send($number, "Renting from service stands is not allowed: The bike probably waits for a repair.");
return;
return $this->response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR);
}

if ($watches['stack'] and $stacktopbike != $bike) {
Expand All @@ -74,8 +73,7 @@ public function rentBike($number, $bike, $force = false)
notifyAdmins(_('Bike') . ' ' . $bike . ' ' . _('rented out of stack by') . ' ' . $userName . '. ' . $stacktopbike . ' ' . _('was on the top of the stack at') . ' ' . $stand . '.', ERROR);
}
if ($forcestack and $stacktopbike != $bike) {
$smsSender->send($number, _('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.');
return;
return $this->response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR);
}
}
}
Expand All @@ -95,12 +93,10 @@ public function rentBike($number, $bike, $force = false)

if ($force == false) {
if ($currentUser == $userId) {
$smsSender->send($number, _('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '. ' . _('Return bike with command:') . ' RETURN ' . _('bikenumber') . ' ' . _('standname') . '.');
return;
return $this->response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '. ' . _('Return bike with command:') . ' RETURN ' . _('bikenumber') . ' ' . _('standname') . '.');
}
if ($currentUser != 0) {
$smsSender->send($number, _('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.');
return;
return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR);
}
}

Expand All @@ -114,22 +110,24 @@ public function rentBike($number, $bike, $force = false)
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RENT',parameter=$newCode");
} else {
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERENT',parameter=$newCode");
$smsSender->send($number, _('System override') . ": " . _('Your rented bike') . " " . $bikeNum . " " . _('has been rented by admin') . ".");
$this->response(_('System override') . ": " . _('Your rented bike') . " " . $bikeNum . " " . _('has been rented by admin') . ".");
}
$smsSender->send($number, $message);
return $this->response($message);
}

public function returnBike($number, $bike, $stand, $message = '', $force = false)
{
global $db, $smsSender, $user, $creditSystem;

$this->number = $number;

$userId = $user->findUserIdByNumber($number);
$bikeNum = intval($bike);
$stand = strtoupper($stand);

$result = $db->query("SELECT standId FROM stands WHERE standName='$stand'");
if (!$result->num_rows) {
$smsSender->send($number, _('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'));
return;
return $this->response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR);
}
$row = $result->fetch_assoc();
$standId = $row["standId"];
Expand All @@ -139,8 +137,7 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false
$bikenumber = $result->num_rows;

if ($bikenumber == 0) {
$smsSender->send($number, _('You currently have no rented bikes.'));
return;
return $this->response(_('You currently have no rented bikes.'), ERROR);
}

$listBikes = [];
Expand All @@ -155,14 +152,12 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false
if ($force == false) {
$result = $db->query("SELECT currentCode FROM bikes WHERE currentUser=$userId AND bikeNum=$bikeNum");
if ($result->num_rows != 1) {
$smsSender->send($number, _('You does not have bike') . " " . $bikeNum . " rented. " . _('You have rented the following') . " " . sprintf(ngettext('%d bike', '%d bikes', $bikenumber), $bikenumber) . ": $listBikes");
return;
return $this->response(_('You does not have bike') . " " . $bikeNum . " rented. " . _('You have rented the following') . " " . sprintf(ngettext('%d bike', '%d bikes', $bikenumber), $bikenumber) . ": $listBikes");
}
} else {
$result = $db->query("SELECT currentCode FROM bikes WHERE bikeNum=$bikeNum");
if ($result->num_rows != 1) {
$smsSender->send($number, _('Bike') . " " . $bikeNum . " " . _('is not rented. Saint Thomas, the patronus of all unrented bikes, prohibited returning unrented bikes.'));
return;
return $this->response(_('Bike') . " " . $bikeNum . " " . _('is not rented. Saint Thomas, the patronus of all unrented bikes, prohibited returning unrented bikes.'));
}
}
$row = $result->fetch_assoc();
Expand Down Expand Up @@ -201,6 +196,13 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERETURN',parameter=$standId");
}

$smsSender->send($number, $message);
return $this->response($message);
}

protected function response($message, $error = 0, $additional = '', $log = 1)
{
global $smsSender;

$smsSender->send($this->number, $message);
}
}
Loading

0 comments on commit c60f20b

Please sign in to comment.