Skip to content

Commit

Permalink
Merge pull request #10 from bluzphp/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Anton authored Sep 3, 2021
2 parents 3ccf9f4 + 369d6b8 commit 992767b
Show file tree
Hide file tree
Showing 26 changed files with 399 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ script:
# Check code style
- php vendor/bin/phpcs ./application --standard=PSR12 --encoding=utf-8 --ignore=./application/_loader.php
# Run built-in web-server
- php vendor/bin/bluzman server:start --host 127.0.0.1 -b
- php vendor/bin/bluzman server:start --host 127.0.0.1 -b -e testing
# Run CodeCeption tests
- php vendor/bin/bluzman test module-$BLUZ_MODULE
notifications:
Expand Down
3 changes: 2 additions & 1 deletion application/models/Transactions/Crud.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Transactions;
Expand Down
11 changes: 5 additions & 6 deletions application/models/Transactions/Grid.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Transactions;
Expand Down Expand Up @@ -31,24 +32,22 @@ public function init(): void
{
// Build select
$select = Table::select()
->addSelect('p.id AS paymentId', 'p.amount AS money', 'p.currency')
->addSelect('users.login AS login')
->leftJoin('transactions', 'payments', 'p', 'p.transactionId = transactions.id')
->addSelect('usersChain.login AS chainLogin')
->leftJoin('transactions', 'users', 'users', 'users.id = transactions.userId')
->leftJoin('transactions', 'users', 'usersChain', 'usersChain.id = transactions.chainUserId')
;

// Current table as source of grid
$adapter = new SelectSource();
$adapter->setSource($select);

$this->addAlias('transactions.id', 'id');
$this->addAlias('users.id', 'user');
$this->addAlias('users.login', 'login');

$this->setAdapter($adapter);
$this->setDefaultLimit(25);
$this->setAllowFilters(['users.id', 'type']);
$this->setAllowOrders(['transactions.id', 'type', 'amount', 'created', 'users.login']);
$this->setDefaultOrder('transactions.id', self::ORDER_DESC);
$this->setAllowOrders(['type', 'amount', 'created', 'users.login']);
}
}
15 changes: 10 additions & 5 deletions application/models/Transactions/Row.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Transactions;

use Bluz\Db\Exception\RelationNotFoundException;
use Bluz\Db\Exception\TableNotFoundException;

/**
* Class Row for `transactions`
*
* @package Application\Transactions
*
* @property integer $id
* @property integer $userId
* @property integer $chainUserId
* @property integer $amount
* @property string $type
* @property string $created
Expand Down Expand Up @@ -41,20 +46,20 @@ public function beforeUpdate(): void
* getUser
*
* @return \Application\Users\Row|null
* @throws \Bluz\Db\Exception\RelationNotFoundException
* @throws \Bluz\Db\Exception\TableNotFoundException
* @throws RelationNotFoundException
* @throws TableNotFoundException
*/
public function getUser(): ?\Application\Users\Row
{
return $this->getRelation('Users');
}

/**
* getAmount
* Get amount as string
*
* @return string
*/
public function getAmount()
public function getAmountString(): string
{
// switch statement for $transaction->type
switch ($this->type) {
Expand Down
5 changes: 2 additions & 3 deletions application/models/Transactions/Table.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Transactions;
Expand Down Expand Up @@ -65,8 +66,6 @@ public function init(): void
public static function getInfo($id): ?Row
{
$select = self::select()
->addSelect('p.id AS paymentId', 'p.amount AS money', 'p.currency')
->leftJoin('transactions', 'payments', 'p', 'p.transactionId = transactions.id')
->where('transactions.id = ?', [$id]);

return current($select->execute(Row::class));
Expand Down
3 changes: 2 additions & 1 deletion application/models/Wallets/Crud.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Wallets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Wallets\Exceptions;

/**
Expand Down
4 changes: 2 additions & 2 deletions application/models/Wallets/Exceptions/WalletsException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Wallets\Exceptions;

use Application\Exception;
Expand Down
3 changes: 2 additions & 1 deletion application/models/Wallets/Grid.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Wallets;
Expand Down
3 changes: 2 additions & 1 deletion application/models/Wallets/Row.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* @namespace
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/

namespace Application\Wallets;
Expand Down
202 changes: 202 additions & 0 deletions application/models/Wallets/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
/**
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/skeleton
*/
namespace Application\Wallets;

use Application\Transactions\Table as TransactionsTable;
use Application\Wallets\Exceptions\InsufficientFundsException;
use Application\Wallets\Exceptions\WalletsException;
use Bluz\Proxy\Db;

/**
* Class Table for `wallets`
*
* @package Application\Wallets
*
* @method static Row findRow($primaryKey)
* @see \Bluz\Db\Table::findRow()
* @method static Row findRowWhere($whereList)
* @see \Bluz\Db\Table::findRowWhere()
*
* @author Anton Shevchuk
* @created 2017-10-19 15:21:27
*/
class Service extends \Bluz\Db\Table
{
/**
* Add Debit record
*
* @param int $userId
* @param int $amount
*
* @return \Application\Transactions\Row|false
*/
public static function addDebit(int $userId, int $amount)
{
return Db::transaction(static function () use ($userId, $amount) {
$wallet = Table::getWallet($userId);

$row = TransactionsTable::create(
[
'userId' => $userId,
'amount' => $amount,
'type' => TransactionsTable::TYPE_DEBIT
]
);
$row->save();

$wallet->amount += $amount;
$wallet->save();

return $row;
});
}

/**
* Add Credit record
*
* @param int $userId
* @param int $amount
*
* @return \Application\Transactions\Row|false
*/
public static function addCredit(int $userId, int $amount)
{
return Db::transaction(static function () use ($userId, $amount) {

$wallet = Table::getWallet($userId);
if ($wallet->amount - $wallet->blocked < $amount) {
throw new InsufficientFundsException;
}

$row = TransactionsTable::create(
[
'userId' => $userId,
'amount' => $amount,
'type' => TransactionsTable::TYPE_CREDIT
]
);
$row->save();

$wallet->amount -= $amount;
$wallet->save();

return $row;
});
}

/**
* Block some money
*
* @param int $userId
* @param int $amount
*
* @return \Application\Transactions\Row|false
*/
public static function addBlock(int $userId, int $amount)
{
return Db::transaction(static function () use ($userId, $amount) {
$wallet = Table::getWallet($userId);
if ($wallet->amount - $wallet->blocked < $amount) {
throw new InsufficientFundsException;
}

$row = TransactionsTable::create(
[
'userId' => $userId,
'amount' => $amount,
'type' => TransactionsTable::TYPE_BLOCK
]
);
$row->save();

$wallet->blocked += $amount;
return $row;
});
}

/**
* Add Credit record
*
* @param int $userId
* @param int $amount
*
* @return \Application\Transactions\Row|false
*/
public static function removeBlock(int $userId, int $amount)
{
return Db::transaction(static function () use ($userId, $amount) {
$wallet = Table::getWallet($userId);

$row = TransactionsTable::create(
[
'userId' => $userId,
'amount' => $amount,
'type' => TransactionsTable::TYPE_UNBLOCK
]
);
$row->save();

if ($wallet->blocked < $amount) {
$wallet->blocked = 0;
} else {
$wallet->blocked -= $amount;
}

return $wallet->save();
});
}

/**
* Send money from wallet to wallet
*
* @param int $fromUserId
* @param int $toUserId
* @param int $amount
*
* @return bool
* @throws WalletsException
*/
public static function send(int $fromUserId, int $toUserId, int $amount): bool
{
if ($amount < 0) {
throw new WalletsException('Amount is lower than zero');
}

return Db::transaction(static function () use ($fromUserId, $toUserId, $amount) {
$fromWallet = Table::getWallet($fromUserId);

if ($fromWallet->amount - $fromWallet->blocked < $amount) {
throw new InsufficientFundsException;
}

// Update wallets
$fromWallet->amount -= $amount;
$fromWallet->save();

$toWallet = Table::getWallet($toUserId);
$toWallet->amount += $amount;
$toWallet->save();

// Credit transaction
$creditTransaction = TransactionsTable::create();
$creditTransaction->userId = $fromUserId;
$creditTransaction->chainUserId = $toUserId;
$creditTransaction->amount = $amount;
$creditTransaction->type = TransactionsTable::TYPE_CREDIT;
$creditTransaction->save();

// Debit transaction
$debitTransaction = TransactionsTable::create();
$debitTransaction->userId = $toUserId;
$debitTransaction->chainUserId = $fromUserId;
$debitTransaction->amount = $amount;
$debitTransaction->type = TransactionsTable::TYPE_DEBIT;
$debitTransaction->save();

return true;
});
}
}
Loading

0 comments on commit 992767b

Please sign in to comment.