Skip to content

Commit

Permalink
Merge pull request #108 from bavix/extends
Browse files Browse the repository at this point in the history
#106 #105 add example
  • Loading branch information
rez1dent3 committed Sep 19, 2019
2 parents f59eaf4 + 613fbd9 commit 1c59b95
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 98 deletions.
10 changes: 8 additions & 2 deletions .phpstorm.meta.php
Expand Up @@ -6,14 +6,20 @@
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Objects\EmptyLock;
use Bavix\Wallet\Objects\Operation;
use Bavix\Wallet\Services\CommonService;
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,
Cart::class => Cart::class,
Bring::class => Bring::class,
Operation::class => Operation::class,
EmptyLock::class => EmptyLock::class,
ExchangeService::class => ExchangeService::class,
CommonService::class => CommonService::class,
ProxyService::class => ProxyService::class,
Expand Down
13 changes: 11 additions & 2 deletions config/config.php
@@ -1,8 +1,11 @@
<?php

use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Objects\EmptyLock;
use Bavix\Wallet\Objects\Operation;
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 @@ -50,7 +53,13 @@
'proxy' => ProxyService::class,
'wallet' => WalletService::class,
'lock' => LockService::class,
'make' => MakeService::class,
],

'objects' => [
'bring' => Bring::class,
'cart' => Cart::class,
'emptyLock' => EmptyLock::class,
'operation' => Operation::class,
],

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/Cart.php
Expand Up @@ -25,7 +25,7 @@ class Cart implements Countable

/**
* @return static
* @deprecated use app(MakeService::class)->makeCart()
* @deprecated use app(Cart::class)
*/
public static function make(): self
{
Expand Down
10 changes: 4 additions & 6 deletions src/Services/CommonService.php
Expand Up @@ -9,6 +9,7 @@
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 @@ -52,8 +53,7 @@ public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $met
->getWallet($from);

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

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

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

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 @@ -87,8 +88,7 @@ protected function lockProvider($self, string $name, int $seconds): Lock
}
// @codeCoverageIgnoreEnd

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

}
42 changes: 0 additions & 42 deletions src/Services/MakeService.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/Traits/CanExchange.php
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,8 +62,7 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
->deposit($to, $amount * $rate, $meta);

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

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

trait CanPay
Expand All @@ -19,7 +19,7 @@ trait CanPay
*/
public function payFree(Product $product): Transfer
{
return current($this->payFreeCart(app(MakeService::class)->makeCart()->addItem($product)));
return current($this->payFreeCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force)) ?: null;
return current($this->safePayCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force));
return current($this->payCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product)));
return current($this->forcePayCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts);
return $this->safeRefundCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts);
return $this->refundCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $gifts);
return $this->forceRefundCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force);
return $this->safeRefundGiftCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product), $force);
return $this->refundGiftCart(app(Cart::class)->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(app(MakeService::class)->makeCart()->addItem($product));
return $this->forceRefundGiftCart(app(Cart::class)->addItem($product));
}

}
3 changes: 1 addition & 2 deletions src/Traits/CartPay.php
Expand Up @@ -7,7 +7,6 @@
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 @@ -230,7 +229,7 @@ public function forceRefundGiftCart(Cart $cart): bool
*/
public function paid(Product $product, bool $gifts = null): ?Transfer
{
return current(app(MakeService::class)->makeCart()->addItem($product)->alreadyBuy($this, $gifts)) ?: null;
return current(app(Cart::class)->addItem($product)->alreadyBuy($this, $gifts)) ?: null;
}

}
5 changes: 2 additions & 3 deletions src/Traits/HasGift.php
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,8 +86,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer
->getWallet($to);

$transfers = $commonService->assemble([
app(MakeService::class)
->makeBring()
app(Bring::class)
->setStatus(Transfer::STATUS_GIFT)
->setDeposit($deposit)
->setWithdraw($withdraw)
Expand Down
20 changes: 15 additions & 5 deletions src/WalletServiceProvider.php
Expand Up @@ -7,10 +7,13 @@
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Objects\Bring;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Objects\EmptyLock;
use Bavix\Wallet\Objects\Operation;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\ExchangeService;
use Bavix\Wallet\Services\LockService;
use Bavix\Wallet\Services\MakeService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Services\WalletService;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -79,15 +82,22 @@ public function register(): void

// Bind eloquent models to IoC container
$this->app->singleton(Rateable::class, config('wallet.package.rateable'));
$this->app->singleton(Transaction::class, config('wallet.transaction.model'));
$this->app->singleton(Transfer::class, config('wallet.transfer.model'));
$this->app->singleton(Wallet::class, config('wallet.wallet.model'));
$this->app->singleton(ExchangeService::class, config('wallet.services.exchange'));
$this->app->singleton(CommonService::class, config('wallet.services.common'));
$this->app->singleton(ProxyService::class, config('wallet.services.proxy'));
$this->app->singleton(WalletService::class, config('wallet.services.wallet'));
$this->app->singleton(LockService::class, config('wallet.services.lock'));
$this->app->singleton(MakeService::class, config('wallet.services.make'));

// models
$this->app->bind(Transaction::class, config('wallet.transaction.model'));
$this->app->bind(Transfer::class, config('wallet.transfer.model'));
$this->app->bind(Wallet::class, config('wallet.wallet.model'));

// object's
$this->app->bind(Bring::class, config('wallet.objects.bring'));
$this->app->bind(Cart::class, config('wallet.objects.cart'));
$this->app->bind(EmptyLock::class, config('wallet.objects.emptyLock'));
$this->app->bind(Operation::class, config('wallet.objects.operation'));
}

}
1 change: 1 addition & 0 deletions tests/BalanceTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Bavix\Wallet\Test;

use Bavix\Wallet\Models\Wallet;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Services\CommonService;
use Bavix\Wallet\Services\ProxyService;
use Bavix\Wallet\Test\Models\Buyer;
Expand Down

0 comments on commit 1c59b95

Please sign in to comment.