Skip to content

Commit 7772d6a

Browse files
committed
draft config
1 parent 07b4461 commit 7772d6a

File tree

14 files changed

+102
-28
lines changed

14 files changed

+102
-28
lines changed

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Bavix\Wallet\Internal\Repository\TransferRepository;
2020
use Bavix\Wallet\Internal\Repository\WalletRepository;
2121
use Bavix\Wallet\Internal\Service\ClockService;
22+
use Bavix\Wallet\Internal\Service\ConfigService;
2223
use Bavix\Wallet\Internal\Service\DatabaseService;
2324
use Bavix\Wallet\Internal\Service\DispatcherService;
2425
use Bavix\Wallet\Internal\Service\JsonService;
@@ -80,6 +81,7 @@
8081
*/
8182
'internal' => [
8283
'clock' => ClockService::class,
84+
'config' => ConfigService::class,
8385
'database' => DatabaseService::class,
8486
'dispatcher' => DispatcherService::class,
8587
'json' => JsonService::class,

deptrac.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ parameters:
201201
- RepositoryInterface
202202
- TransformInterface
203203
- InternalException
204-
- ServiceInterface # json service only
204+
- ServiceInterface # json&config service only
205205
- QueryInterface
206206
- DtoInterface
207207
- UIException

src/Internal/Repository/WalletRepository.php

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

77
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
88
use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException;
9+
use Bavix\Wallet\Internal\Service\ConfigServiceInterface;
910
use Bavix\Wallet\Models\Wallet;
1011
use Illuminate\Database\Eloquent\ModelNotFoundException as EloquentModelNotFoundException;
1112

1213
final class WalletRepository implements WalletRepositoryInterface
1314
{
1415
public function __construct(
16+
private ConfigServiceInterface $configService,
1517
private Wallet $wallet
1618
) {
1719
}
@@ -91,7 +93,7 @@ public function getBySlug(string $holderType, int|string $holderId, string $slug
9193
public function findDefaultAll(string $holderType, array $holderIds): array
9294
{
9395
return $this->wallet->newQuery()
94-
->where('slug', config('wallet.wallet.default.slug', 'default'))
96+
->where('slug', $this->configService->getString('wallet.wallet.default.slug', 'default'))
9597
->where('holder_type', $holderType)
9698
->whereIn('holder_id', $holderIds)
9799
->get()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
final class ConfigService implements ConfigServiceInterface
8+
{
9+
public function getClass(string $name, string $default): string
10+
{
11+
$value = $this->getString($name, $default);
12+
assert(class_exists($value));
13+
14+
return $value;
15+
}
16+
17+
public function getString(string $name, string $default): string
18+
{
19+
$value = config($name, $default);
20+
assert(is_string($value));
21+
22+
return $value;
23+
}
24+
25+
/**
26+
* @return array<mixed>
27+
*/
28+
public function getArray(string $name): array
29+
{
30+
$value = config($name, []);
31+
assert(is_array($value));
32+
33+
return $value;
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
interface ConfigServiceInterface
8+
{
9+
/**
10+
* @param class-string $default
11+
* @return class-string
12+
*/
13+
public function getClass(string $name, string $default): string;
14+
15+
public function getString(string $name, string $default): string;
16+
17+
/**
18+
* @return array<mixed>
19+
*/
20+
public function getArray(string $name): array;
21+
}

src/Models/Transaction.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace Bavix\Wallet\Models;
66

77
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Internal\Service\ConfigServiceInterface;
89
use Bavix\Wallet\Internal\Service\MathServiceInterface;
910
use Bavix\Wallet\Models\Wallet as WalletModel;
1011
use Bavix\Wallet\Services\CastServiceInterface;
11-
use function config;
1212
use Illuminate\Database\Eloquent\Model;
1313
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1414
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -61,7 +61,7 @@ class Transaction extends Model
6161
public function getTable(): string
6262
{
6363
if ((string) $this->table === '') {
64-
$this->table = config('wallet.transaction.table', 'transactions');
64+
$this->table = app(ConfigServiceInterface::class)->getString('wallet.transaction.table', 'transactions');
6565
}
6666

6767
return parent::getTable();
@@ -74,7 +74,9 @@ public function payable(): MorphTo
7474

7575
public function wallet(): BelongsTo
7676
{
77-
return $this->belongsTo(config('wallet.wallet.model', WalletModel::class));
77+
return $this->belongsTo(
78+
app(ConfigServiceInterface::class)->getClass('wallet.wallet.model', WalletModel::class)
79+
);
7880
}
7981

8082
public function getAmountIntAttribute(): int

src/Models/Transfer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Bavix\Wallet\Models;
66

7-
use function config;
7+
use Bavix\Wallet\Internal\Service\ConfigServiceInterface;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1010
use Illuminate\Database\Eloquent\Relations\MorphTo;
@@ -67,7 +67,7 @@ class Transfer extends Model
6767
public function getTable(): string
6868
{
6969
if ((string) $this->table === '') {
70-
$this->table = config('wallet.transfer.table', 'transfers');
70+
$this->table = app(ConfigServiceInterface::class)->getString('wallet.transfer.table', 'transfers');
7171
}
7272

7373
return parent::getTable();

src/Models/Wallet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1414
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
1515
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
16+
use Bavix\Wallet\Internal\Service\ConfigServiceInterface;
1617
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1718
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1819
use Bavix\Wallet\Services\AtomicServiceInterface;
@@ -21,7 +22,6 @@
2122
use Bavix\Wallet\Traits\CanExchange;
2223
use Bavix\Wallet\Traits\CanPayFloat;
2324
use Bavix\Wallet\Traits\HasGift;
24-
use function config;
2525
use Illuminate\Database\Eloquent\Model;
2626
use Illuminate\Database\Eloquent\Relations\MorphTo;
2727
use Illuminate\Database\RecordsNotFoundException;
@@ -83,7 +83,7 @@ class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchan
8383
public function getTable(): string
8484
{
8585
if ((string) $this->table === '') {
86-
$this->table = config('wallet.wallet.table', 'wallets');
86+
$this->table = app(ConfigServiceInterface::class)->getString('wallet.wallet.table', 'wallets');
8787
}
8888

8989
return parent::getTable();

src/Services/AtomicServiceInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ interface AtomicServiceInterface
1414
{
1515
/**
1616
* @template T
17-
* @param Wallet $object
1817
* @param callable(): T $callback
1918
* @return T
2019
*

src/Services/WalletService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Bavix\Wallet\Internal\Assembler\WalletCreatedEventAssemblerInterface;
88
use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException;
99
use Bavix\Wallet\Internal\Repository\WalletRepositoryInterface;
10+
use Bavix\Wallet\Internal\Service\ConfigServiceInterface;
1011
use Bavix\Wallet\Internal\Service\DispatcherServiceInterface;
1112
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1213
use Bavix\Wallet\Models\Wallet;
@@ -21,14 +22,15 @@ public function __construct(
2122
private WalletCreatedEventAssemblerInterface $walletCreatedEventAssembler,
2223
private UuidFactoryServiceInterface $uuidFactoryService,
2324
private DispatcherServiceInterface $dispatcherService,
24-
private WalletRepositoryInterface $walletRepository
25+
private WalletRepositoryInterface $walletRepository,
26+
private ConfigServiceInterface $configService
2527
) {
2628
}
2729

2830
public function create(Model $model, array $data): Wallet
2931
{
3032
$wallet = $this->walletRepository->create(array_merge(
31-
config('wallet.wallet.creating', []),
33+
$this->configService->getArray('wallet.wallet.creating'),
3234
[
3335
'uuid' => $this->uuidFactoryService->uuid4(),
3436
],

0 commit comments

Comments
 (0)