|
2 | 2 |
|
3 | 3 | namespace HPWebdeveloper\LaravelPayPocket\Traits;
|
4 | 4 |
|
5 |
| -use Illuminate\Support\Str; |
| 5 | +use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog; |
| 6 | +use InvalidArgumentException; |
6 | 7 |
|
7 | 8 | trait BalanceOperation
|
8 | 9 | {
|
9 |
| - protected $createdLog; |
| 10 | + protected WalletsLog $createdLog; |
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * Check if Balance is more than zero.
|
@@ -43,29 +44,37 @@ protected function createLog(string $logType, int|float $value, ?string $notes =
|
43 | 44 |
|
44 | 45 | $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value;
|
45 | 46 |
|
46 |
| - $refGen = config('pay-pocket.log_reference_generator', [ |
47 |
| - Str::class, 'random', [config('pay-pocket.log_reference_length', 12)], |
48 |
| - ]); |
49 |
| - $refGen = [ |
50 |
| - Str::class, 'random', [config('pay-pocket.log_reference_length', 12)], |
51 |
| - ]; |
52 |
| - |
53 |
| - $reference = config('pay-pocket.reference_string_prefix', ''); |
54 |
| - $reference .= isset($refGen[0], $refGen[1]) |
55 |
| - ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) |
56 |
| - : Str::random(config('pay-pocket.log_reference_length', 12)); |
57 |
| - |
| 47 | + /** @var \Illuminate\Database\Eloquent\Model $this */ |
58 | 48 | $this->createdLog = $this->logs()->create([
|
59 | 49 | 'wallet_name' => $this->type->value,
|
60 | 50 | 'from' => $currentBalance,
|
61 | 51 | 'to' => $newBalance,
|
62 | 52 | 'type' => $logType,
|
63 |
| - 'ip' => \Request::ip(), |
| 53 | + 'ip' => request()->ip(), |
64 | 54 | 'value' => $value,
|
65 | 55 | 'notes' => $notes,
|
66 |
| - 'reference' => $reference, |
| 56 | + 'reference' => $this->generateReference(), |
67 | 57 | ]);
|
68 | 58 |
|
69 | 59 | $this->createdLog->changeStatus('Done');
|
70 | 60 | }
|
| 61 | + |
| 62 | + /** |
| 63 | + * @throws InvalidArgumentException |
| 64 | + */ |
| 65 | + protected function generateReference(): string |
| 66 | + { |
| 67 | + $className = config('pay-pocket.log_reference_generator_class'); |
| 68 | + $methodName = config('pay-pocket.log_reference_generator_method'); |
| 69 | + $length = config('pay-pocket.log_reference_length'); |
| 70 | + $prefix = config('pay-pocket.log_reference_prefix'); |
| 71 | + |
| 72 | + if (!is_callable([$className, $methodName])) { |
| 73 | + throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.'); |
| 74 | + } |
| 75 | + |
| 76 | + $reference = call_user_func([$className, $methodName], $length); |
| 77 | + |
| 78 | + return $prefix . $reference; |
| 79 | + } |
71 | 80 | }
|
0 commit comments