Skip to content

Commit

Permalink
Merge pull request #107 from bavix/extends
Browse files Browse the repository at this point in the history
#106 Extends
  • Loading branch information
rez1dent3 committed Sep 19, 2019
2 parents a95bd17 + 349c82b commit f59eaf4
Show file tree
Hide file tree
Showing 22 changed files with 120 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Services\WalletService;
use Bavix\Wallet\Services\MakeService;

override(\app(0), map([
MakeService::class => MakeService::class,
ExchangeService::class => ExchangeService::class,
CommonService::class => CommonService::class,
ProxyService::class => ProxyService::class,
Expand Down
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.3.0] - 2019-09-10
### Added
- Added the ability to easily overload the main classes #106

## [3.2.1] - 2019-09-10
### Fixed
- Fixed calculation of commission for exchange #101 @haojingliu
Expand Down Expand Up @@ -399,7 +403,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.2.1...HEAD
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.3.0...HEAD
[3.3.0]: https://github.com/bavix/laravel-wallet/compare/3.2.1...3.3.0
[3.2.1]: https://github.com/bavix/laravel-wallet/compare/3.2.0...3.2.1
[3.2.0]: https://github.com/bavix/laravel-wallet/compare/3.1.6...3.2.0
[3.1.6]: https://github.com/bavix/laravel-wallet/compare/3.1.5...3.1.6
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\MakeService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Services\WalletService;
use Bavix\Wallet\Services\LockService;
Expand Down Expand Up @@ -49,6 +50,7 @@
'proxy' => ProxyService::class,
'wallet' => WalletService::class,
'lock' => LockService::class,
'make' => MakeService::class,
],

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/RefreshBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Class RefreshBalance
* @package Bavix\Wallet\Commands
* @codeCoverageIgnore
* @codeCoverageIgnore
*/
class RefreshBalance extends Command
{
Expand Down
1 change: 1 addition & 0 deletions src/Objects/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Cart implements Countable

/**
* @return static
* @deprecated use app(MakeService::class)->makeCart()
*/
public static function make(): self
{
Expand Down
6 changes: 3 additions & 3 deletions src/Objects/EmptyLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EmptyLock implements Lock
/**
* Attempt to acquire the lock.
*
* @param callable|null $callback
* @param callable|null $callback
* @return mixed
*/
public function get($callback = null)
Expand All @@ -31,8 +31,8 @@ public function get($callback = null)
/**
* Attempt to acquire the lock for the given number of seconds.
*
* @param int $seconds
* @param callable|null $callback
* @param int $seconds
* @param callable|null $callback
* @return bool
*/
public function block($seconds, $callback = null): bool
Expand Down
10 changes: 6 additions & 4 deletions src/Services/CommonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet as WalletModel;
use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Objects\Operation;
use Bavix\Wallet\Traits\HasWallet;
use Illuminate\Support\Facades\DB;
use function app;
Expand Down Expand Up @@ -53,7 +52,8 @@ public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $met
->getWallet($from);

$transfers = $this->multiBrings([
(new Bring())
app(MakeService::class)
->makeBring()
->setStatus($status)
->setDeposit($deposit)
->setWithdraw($withdraw)
Expand Down Expand Up @@ -84,7 +84,8 @@ public function forceWithdraw(Wallet $wallet, int $amount, ?array $meta, bool $c
$wallet = $walletService->getWallet($wallet);

$transactions = $this->multiOperation($wallet, [
(new Operation())
app(MakeService::class)
->makeOperation()
->setType(Transaction::TYPE_WITHDRAW)
->setConfirmed($confirmed)
->setAmount(-$amount)
Expand Down Expand Up @@ -114,7 +115,8 @@ public function deposit(Wallet $wallet, int $amount, ?array $meta, bool $confirm
$wallet = $walletService->getWallet($wallet);

$transactions = $this->multiOperation($wallet, [
(new Operation())
app(MakeService::class)
->makeOperation()
->setType(Transaction::TYPE_DEPOSIT)
->setConfirmed($confirmed)
->setAmount($amount)
Expand Down
6 changes: 3 additions & 3 deletions src/Services/LockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Bavix\Wallet\Services;

use Bavix\Wallet\Objects\EmptyLock;
use Illuminate\Contracts\Cache\Lock;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
Expand Down Expand Up @@ -34,7 +33,7 @@ public function __construct()
*/
public function lock($self, string $name, \Closure $closure)
{
return $this->lockProvider($self, $name, (int) config('wallet.lock.seconds'))
return $this->lockProvider($self, $name, (int)config('wallet.lock.seconds'))
->get($this->bindTo($self, $closure));
}

Expand Down Expand Up @@ -88,7 +87,8 @@ protected function lockProvider($self, string $name, int $seconds): Lock
}
// @codeCoverageIgnoreEnd

return new EmptyLock();
return app(MakeService::class)
->makeEmptyLock();
}

}
42 changes: 42 additions & 0 deletions src/Services/MakeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Bavix\Wallet\Services;

use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Objects\EmptyLock;
use Bavix\Wallet\Objects\Operation;

class MakeService
{

/**
* @return Bring
*/
public function makeBring(): Bring
{
return new Bring();
}

public function makeCart(): Cart
{
return new Cart();
}

/**
* @return EmptyLock
*/
public function makeEmptyLock(): EmptyLock
{
return new EmptyLock();
}

/**
* @return Operation
*/
public function makeOperation(): Operation
{
return new Operation();
}

}
4 changes: 2 additions & 2 deletions src/Services/WalletService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function fee(Wallet $wallet, int $amount): int
{
$fee = 0;
if ($wallet instanceof Taxable) {
$fee = (int) ($amount * $wallet->getFeePercent() / 100);
$fee = (int)($amount * $wallet->getFeePercent() / 100);
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function getBalance(Wallet $object): int
$wallet = $this->getWallet($object);
$proxy = app(ProxyService::class);
if (!$proxy->has($wallet->getKey())) {
$proxy->set($wallet->getKey(), (int) $wallet->getOriginal('balance', 0));
$proxy->set($wallet->getKey(), (int)$wallet->getOriginal('balance', 0));
}

return $proxy[$wallet->getKey()];
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/CanExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\LockService;
use Bavix\Wallet\Services\MakeService;
use Bavix\Wallet\Services\WalletService;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -62,7 +62,8 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
->deposit($to, $amount * $rate, $meta);

$transfers = app(CommonService::class)->multiBrings([
(new Bring())
app(MakeService::class)
->makeBring()
->setStatus(Transfer::STATUS_EXCHANGE)
->setDeposit($deposit)
->setWithdraw($withdraw)
Expand Down
22 changes: 11 additions & 11 deletions src/Traits/CanPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Bavix\Wallet\Interfaces\Product;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Services\MakeService;
use function current;

trait CanPay
Expand All @@ -19,7 +19,7 @@ trait CanPay
*/
public function payFree(Product $product): Transfer
{
return current($this->payFreeCart(Cart::make()->addItem($product)));
return current($this->payFreeCart(app(MakeService::class)->makeCart()->addItem($product)));
}

/**
Expand All @@ -29,7 +29,7 @@ public function payFree(Product $product): Transfer
*/
public function safePay(Product $product, bool $force = null): ?Transfer
{
return current($this->safePayCart(Cart::make()->addItem($product), $force)) ?: null;
return current($this->safePayCart(app(MakeService::class)->makeCart()->addItem($product), $force)) ?: null;
}

/**
Expand All @@ -40,7 +40,7 @@ public function safePay(Product $product, bool $force = null): ?Transfer
*/
public function pay(Product $product, bool $force = null): Transfer
{
return current($this->payCart(Cart::make()->addItem($product), $force));
return current($this->payCart(app(MakeService::class)->makeCart()->addItem($product), $force));
}

/**
Expand All @@ -50,7 +50,7 @@ public function pay(Product $product, bool $force = null): Transfer
*/
public function forcePay(Product $product): Transfer
{
return current($this->forcePayCart(Cart::make()->addItem($product)));
return current($this->forcePayCart(app(MakeService::class)->makeCart()->addItem($product)));
}

/**
Expand All @@ -61,7 +61,7 @@ public function forcePay(Product $product): Transfer
*/
public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool
{
return $this->safeRefundCart(Cart::make()->addItem($product), $force, $gifts);
return $this->safeRefundCart(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts);
}

/**
Expand All @@ -73,7 +73,7 @@ public function safeRefund(Product $product, bool $force = null, bool $gifts = n
*/
public function refund(Product $product, bool $force = null, bool $gifts = null): bool
{
return $this->refundCart(Cart::make()->addItem($product), $force, $gifts);
return $this->refundCart(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts);
}

/**
Expand All @@ -84,7 +84,7 @@ public function refund(Product $product, bool $force = null, bool $gifts = null)
*/
public function forceRefund(Product $product, bool $gifts = null): bool
{
return $this->forceRefundCart(Cart::make()->addItem($product), $gifts);
return $this->forceRefundCart(app(MakeService::class)->makeCart()->addItem($product), $gifts);
}

/**
Expand All @@ -94,7 +94,7 @@ public function forceRefund(Product $product, bool $gifts = null): bool
*/
public function safeRefundGift(Product $product, bool $force = null): bool
{
return $this->safeRefundGiftCart(Cart::make()->addItem($product), $force);
return $this->safeRefundGiftCart(app(MakeService::class)->makeCart()->addItem($product), $force);
}

/**
Expand All @@ -105,7 +105,7 @@ public function safeRefundGift(Product $product, bool $force = null): bool
*/
public function refundGift(Product $product, bool $force = null): bool
{
return $this->refundGiftCart(Cart::make()->addItem($product), $force);
return $this->refundGiftCart(app(MakeService::class)->makeCart()->addItem($product), $force);
}

/**
Expand All @@ -115,7 +115,7 @@ public function refundGift(Product $product, bool $force = null): bool
*/
public function forceRefundGift(Product $product): bool
{
return $this->forceRefundGiftCart(Cart::make()->addItem($product));
return $this->forceRefundGiftCart(app(MakeService::class)->makeCart()->addItem($product));
}

}
3 changes: 2 additions & 1 deletion src/Traits/CartPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\MakeService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Throwable;
Expand Down Expand Up @@ -229,7 +230,7 @@ public function forceRefundGiftCart(Cart $cart): bool
*/
public function paid(Product $product, bool $gifts = null): ?Transfer
{
return current(Cart::make()->addItem($product)->alreadyBuy($this, $gifts)) ?: null;
return current(app(MakeService::class)->makeCart()->addItem($product)->alreadyBuy($this, $gifts)) ?: null;
}

}
5 changes: 3 additions & 2 deletions src/Traits/HasGift.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Bavix\Wallet\Interfaces\Product;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\LockService;
use Bavix\Wallet\Services\MakeService;
use Bavix\Wallet\Services\WalletService;
use Illuminate\Support\Facades\DB;
use Throwable;
Expand Down Expand Up @@ -86,7 +86,8 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
->getWallet($to);

$transfers = $commonService->assemble([
(new Bring())
app(MakeService::class)
->makeBring()
->setStatus(Transfer::STATUS_GIFT)
->setDeposit($deposit)
->setWithdraw($withdraw)
Expand Down
1 change: 0 additions & 1 deletion src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\WalletService;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Throwable;
Expand Down
Loading

0 comments on commit f59eaf4

Please sign in to comment.