Skip to content

Commit c16c047

Browse files
committed
update readme,migrations,contracts
1 parent 15a4f75 commit c16c047

10 files changed

+68
-50
lines changed

README.md

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ laravel-wallet - Easy work with virtual wallet.
2828

2929
### Support Policy
3030

31-
| Version | Laravel | PHP | Fixes |
32-
|------------|----------------|-----------|------------|
33-
| 7.x | ^6.0,^7.0,^8.0 | ^7.4,^8.0 | 6 Sep 2022 |
34-
| 8.x | ^9.0 | ^8.0 | 1 Jun 2022 |
35-
| 9.x [LTS] | ^9.0 | ^8.0 | 8 Aug 2023 |
31+
| Version | Laravel | PHP | End of improvements | End of support |
32+
|------------|----------------|-------------|---------------------|----------------|
33+
| 7.x | ^6.0,^7.0,^8.0 | 7.4,8.0,8.1 | 1 Mar 2022 | 6 Sep 2022 |
34+
| 8.x | ^9.0 | 8.0,8.1 | 1 May 2022 | 1 Jun 2022 |
35+
| 9.x [LTS] | ^9.0 | 8.0,8.1 | 1 Feb 2023 | 6 Nov 2023 |
3636

3737
### Upgrade Guide
3838

3939
To perform the migration, you will be [helped by the instruction](https://bavix.github.io/laravel-wallet/#/upgrade-guide).
4040

4141
### Extensions
4242

43-
| Extension | Description |
44-
| ----- | ----- |
45-
| [Swap](https://github.com/bavix/laravel-wallet-swap) | Addition to the laravel-wallet library for quick setting of exchange rates |
46-
| [Warm Up](https://github.com/bavix/laravel-wallet-warmup) | Addition to the laravel-wallet library for refresh balance wallets |
43+
| Extension | Description |
44+
|-----------------------------------------------------------|----------------------------------------------------------------------------|
45+
| [Swap](https://github.com/bavix/laravel-wallet-swap) | Addition to the laravel-wallet library for quick setting of exchange rates |
46+
| [Warm Up](https://github.com/bavix/laravel-wallet-warmup) | Addition to the laravel-wallet library for refresh balance wallets |
4747

4848
### Usage
4949
Add the `HasWallet` trait and `Wallet` interface to model.
@@ -87,19 +87,52 @@ class User extends Model implements Customer
8787
}
8888
```
8989

90-
Add the `HasWallet` trait and `Product` interface to `Item` model.
90+
Add the `HasWallet` trait and interface to `Item` model.
91+
92+
Starting from version 9.x there are two product interfaces:
93+
- For an unlimited number of products (`ProductLimitedInterface`);
94+
- For a limited number of products (`ProductInterface`);
95+
96+
An example with an unlimited number of products:
97+
```php
98+
use Bavix\Wallet\Traits\HasWallet;
99+
use Bavix\Wallet\Interfaces\Customer;
100+
use Bavix\Wallet\Interfaces\ProductInterface;
101+
102+
class Item extends Model implements ProductInterface
103+
{
104+
use HasWallet;
105+
106+
public function getAmountProduct(Customer $customer)
107+
{
108+
return 100;
109+
}
110+
111+
public function getMetaProduct(): ?array
112+
{
113+
return [
114+
'title' => $this->title,
115+
'description' => 'Purchase of Product #' . $this->id,
116+
];
117+
}
118+
}
119+
```
120+
121+
Example with a limited number of products:
91122
```php
92123
use Bavix\Wallet\Traits\HasWallet;
93-
use Bavix\Wallet\Interfaces\Product;
94124
use Bavix\Wallet\Interfaces\Customer;
125+
use Bavix\Wallet\Interfaces\ProductLimitedInterface;
95126

96-
class Item extends Model implements Product
127+
class Item extends Model implements ProductLimitedInterface
97128
{
98129
use HasWallet;
99130

100131
public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool
101132
{
102133
/**
134+
* This is where you implement the constraint logic.
135+
*
103136
* If the service can be purchased once, then
104137
* return !$customer->paid($this);
105138
*/
@@ -121,6 +154,10 @@ class Item extends Model implements Product
121154
}
122155
```
123156

157+
I do not recommend using the limited interface when working with a shopping cart.
158+
If you are working with a shopping cart, then you should override the `PurchaseServiceInterface` interface.
159+
With it, you can check the availability of all products with one request, there will be no N-queries in the database.
160+
124161
Proceed to purchase.
125162

126163
```php
@@ -132,7 +169,7 @@ $user->pay($item); // If you do not have enough money, throw an exception
132169
var_dump($user->balance); // 0
133170

134171
if ($user->safePay($item)) {
135-
// try to buy again )
172+
// try to buy again
136173
}
137174

138175
var_dump((bool)$user->paid($item)); // bool(true)
@@ -144,7 +181,11 @@ var_dump((bool)$user->paid($item)); // bool(false)
144181
### Eager Loading
145182

146183
```php
184+
// When working with one wallet
147185
User::with('wallet');
186+
187+
// When using the multi-wallet functionality
188+
User::with('wallets');
148189
```
149190

150191
### How to work with fractional numbers?

database/2018_11_06_222923_create_transactions_table.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
use Illuminate\Database\Schema\Blueprint;
88
use Illuminate\Support\Facades\Schema;
99

10-
class CreateTransactionsTable extends Migration
11-
{
10+
return new class() extends Migration {
1211
public function up(): void
1312
{
1413
Schema::create($this->table(), function (Blueprint $table) {
@@ -42,4 +41,4 @@ private function table(): string
4241
{
4342
return (new Transaction())->getTable();
4443
}
45-
}
44+
};

database/2018_11_07_192923_create_transfers_table.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use Illuminate\Database\Schema\Blueprint;
99
use Illuminate\Support\Facades\Schema;
1010

11-
class CreateTransfersTable extends Migration
12-
{
11+
return new class() extends Migration {
1312
public function up(): void
1413
{
1514
Schema::create($this->table(), function (Blueprint $table) {
@@ -70,4 +69,4 @@ private function transactionTable(): string
7069
{
7170
return (new Transaction())->getTable();
7271
}
73-
}
72+
};

database/2018_11_15_124230_create_wallets_table.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use Illuminate\Database\Schema\Blueprint;
99
use Illuminate\Support\Facades\Schema;
1010

11-
class CreateWalletsTable extends Migration
12-
{
11+
return new class() extends Migration {
1312
public function up(): void
1413
{
1514
Schema::create($this->table(), function (Blueprint $table) {
@@ -63,4 +62,4 @@ private function transactionTable(): string
6362
{
6463
return (new Transaction())->getTable();
6564
}
66-
}
65+
};

database/2021_11_02_202021_update_wallets_uuid_table.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
use Illuminate\Support\Collection;
1010
use Illuminate\Support\Facades\Schema;
1111

12-
class UpdateWalletsUuidTable extends Migration
13-
{
12+
return new class() extends Migration {
1413
public function up(): void
1514
{
1615
if (Schema::hasColumn($this->table(), 'uuid')) {
1716
return;
1817
}
1918

2019
// upgrade from 6.x
21-
Schema::table($this->table(), function (Blueprint $table) {
20+
Schema::table($this->table(), static function (Blueprint $table) {
2221
$table->uuid('uuid')
2322
->after('slug')
2423
->nullable()
@@ -49,4 +48,4 @@ protected function table(): string
4948
{
5049
return (new Wallet())->getTable();
5150
}
52-
}
51+
};

src/Internal/Repository/WalletRepository.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
88
use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException;
9-
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
109
use Bavix\Wallet\Models\Wallet;
1110
use Illuminate\Database\Eloquent\ModelNotFoundException as EloquentModelNotFoundException;
1211

1312
final class WalletRepository implements WalletRepositoryInterface
1413
{
1514
public function __construct(
16-
private UuidFactoryServiceInterface $uuidFactoryService,
1715
private Wallet $wallet
1816
) {
1917
}
@@ -69,7 +67,7 @@ public function getById(int $id): Wallet
6967
public function getByUuid(string $uuid): Wallet
7068
{
7169
return $this->getBy([
72-
'uuid' => $this->uuidFactoryService->cast($uuid),
70+
'uuid' => $uuid,
7371
]);
7472
}
7573

src/Internal/Service/StorageService.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,16 @@ public function get(string $key): string
4444
return $this->mathService->round($value);
4545
}
4646

47-
/**
48-
* @param float|int|string $value
49-
*/
50-
public function sync(string $key, $value): bool
47+
public function sync(string $key, float|int|string $value): bool
5148
{
5249
return $this->cacheRepository->set($key, $value);
5350
}
5451

5552
/**
56-
* @param float|int|string $value
57-
*
5853
* @throws LockProviderNotFoundException
5954
* @throws RecordNotFoundException
6055
*/
61-
public function increase(string $key, $value): string
56+
public function increase(string $key, float|int|string $value): string
6257
{
6358
return $this->lockService->block(
6459
$key.'::increase',

src/Internal/Service/StorageServiceInterface.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@ public function missing(string $key): bool;
1818
*/
1919
public function get(string $key): string;
2020

21-
/**
22-
* @param float|int|string $value
23-
*/
24-
public function sync(string $key, $value): bool;
21+
public function sync(string $key, float|int|string $value): bool;
2522

2623
/**
27-
* @param float|int|string $value
28-
*
2924
* @throws LockProviderNotFoundException
3025
* @throws RecordNotFoundException
3126
*/
32-
public function increase(string $key, $value): string;
27+
public function increase(string $key, float|int|string $value): string;
3328
}

src/Internal/Service/UuidFactoryService.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,4 @@ public function uuid4(): string
1919
->toString()
2020
;
2121
}
22-
23-
public function cast(string $uuid): string
24-
{
25-
return $uuid;
26-
}
2722
}

src/Internal/Service/UuidFactoryServiceInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77
interface UuidFactoryServiceInterface
88
{
99
public function uuid4(): string;
10-
11-
public function cast(string $uuid): string;
1210
}

0 commit comments

Comments
 (0)