Code smell.
This commit is contained in:
+19
-19
@@ -11,7 +11,7 @@ use Battles\Database\Db;
|
||||
|
||||
class Bank
|
||||
{
|
||||
private int $user_id = 0;
|
||||
private int $userId = 0;
|
||||
private int $money = 0;
|
||||
private string $error = '';
|
||||
private string $status = '';
|
||||
@@ -31,15 +31,15 @@ class Bank
|
||||
'sellShop' => 'Продажа товара в магазине.'
|
||||
];
|
||||
|
||||
public function __construct(int $user_id = null)
|
||||
public function __construct(int $userId = null)
|
||||
{
|
||||
if (empty($user_id)) {
|
||||
$user_id = User::getInstance()->getId();
|
||||
if (empty($userId)) {
|
||||
$userId = User::getInstance()->getId();
|
||||
}
|
||||
$bank_row = Db::getInstance()->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $user_id);
|
||||
$bankRow = Db::getInstance()->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $userId);
|
||||
foreach ($this as $key => $value) {
|
||||
if (isset($bank_row[$key])) {
|
||||
$this->$key = $bank_row[$key];
|
||||
if (isset($bankRow[$key])) {
|
||||
$this->$key = $bankRow[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,20 +61,20 @@ class Bank
|
||||
/**
|
||||
* Пишем банковское событие в лог в БД
|
||||
*
|
||||
* @param int $receiverId ID получателя.
|
||||
* @param int $amount сумма.
|
||||
* @param int $receiverId ID получателя.
|
||||
* @param int $amount сумма.
|
||||
* @param string $operationType тип банковской операции.
|
||||
* @param ?int $senderId ID отправителя (ID игрока, если не указано иное).
|
||||
* @param ?int $senderId ID отправителя (ID игрока, если не указано иное).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addLog(int $receiverId, int $amount, string $operationType, ?int $senderId = null): void
|
||||
{
|
||||
if (is_null($senderId)) {
|
||||
$senderId = $this->user_id;
|
||||
$senderId = $this->userId;
|
||||
}
|
||||
if ($operationType === 'depositMoney' || $operationType === 'withdrawMoney') {
|
||||
$receiverId = $this->user_id;
|
||||
$receiverId = $this->userId;
|
||||
}
|
||||
$commText = $this->comission ? ' Комиссия: ' . $this->comission : '';
|
||||
$text = self::LOG[$operationType] . $commText;
|
||||
@@ -86,7 +86,7 @@ class Bank
|
||||
* Перевод денег между банковскими счетами игроков с банковской комиссией.
|
||||
*
|
||||
* @param mixed $receiver ID получателя.
|
||||
* @param mixed $amount Cумма.
|
||||
* @param mixed $amount Cумма.
|
||||
*/
|
||||
public function sendMoney($receiver, $amount)
|
||||
{
|
||||
@@ -98,7 +98,7 @@ class Bank
|
||||
if (!is_numeric($amount) || $amount <= 0) {
|
||||
$this->error = self::ERROR_WRONG_AMOUNT;
|
||||
}
|
||||
if (!$rec->user_id) {
|
||||
if (!$rec->userId) {
|
||||
$this->error = self::ERROR_NO_BANK_ACCOUNT;
|
||||
}
|
||||
$amountWithComission = $amount + $this->commission($amount);
|
||||
@@ -110,10 +110,10 @@ class Bank
|
||||
}
|
||||
// Снимаем сумму с комиссией у отправителя
|
||||
$this->modify(-$amountWithComission);
|
||||
$this->addLog($rec->user_id, $this->money, 'sendMoney', $this->user_id);
|
||||
$this->addLog($rec->userId, $this->money, 'sendMoney', $this->userId);
|
||||
// Отдаём сумму на счёт получателю
|
||||
$rec->modify($amount);
|
||||
$rec->addLog($rec->user_id, $rec->money, 'receiveMoney', $this->user_id);
|
||||
$rec->addLog($rec->userId, $rec->money, 'receiveMoney', $this->userId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +128,7 @@ class Bank
|
||||
$this->error = self::ERROR_WRONG_AMOUNT;
|
||||
}
|
||||
// Забираем деньги из кошелька получателя
|
||||
if (!User::getInstance($this->user_id)->money()->spend($amount)) {
|
||||
if (!User::getInstance($this->userId)->money()->spend($amount)) {
|
||||
$this->error = self::ERROR_NO_MONEY_IN_WALLET;
|
||||
}
|
||||
if ($this->error) {
|
||||
@@ -161,12 +161,12 @@ class Bank
|
||||
$this->modify(-$amountWithComission);
|
||||
$this->addLog(0, $this->money, 'withdrawMoney');
|
||||
// Отдаём сумму в кошелёк получателя
|
||||
User::getInstance($this->user_id)->money()->earn($amount);
|
||||
User::getInstance($this->userId)->money()->earn($amount);
|
||||
}
|
||||
|
||||
private function save()
|
||||
{
|
||||
Db::getInstance()->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$this->money, $this->user_id]);
|
||||
Db::getInstance()->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$this->money, $this->userId]);
|
||||
}
|
||||
|
||||
public function getMoney(): int
|
||||
|
||||
Reference in New Issue
Block a user