Skip to content

Commit 2e3e7b9

Browse files
committed
fix problem on reference generator - refactor
1 parent 19a56b1 commit 2e3e7b9

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

config/pay-pocket.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
return [
1616
'log_reference_length' => 12,
17-
'log_reference_prefix' => null,
18-
'log_reference_generator' => null,
17+
'log_reference_prefix' => '',
18+
'log_reference_generator_class' => Illuminate\Support\Str::class,
19+
'log_reference_generator_method' => 'random',
1920
];

src/Models/WalletsLog.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
* HPWebdeveloper\LaravelPayPocket\Models\WalletsLog
1111
*
1212
* @property string $status
13+
* @property int|float $from
14+
* @property int|float $to
15+
* @property string $type
16+
* @property string $ip
17+
* @property int|float $value
18+
* @property string $wallet_name
19+
* @property string $notes
20+
* @property string $reference
1321
*/
1422
class WalletsLog extends Model
1523
{

src/Traits/BalanceOperation.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace HPWebdeveloper\LaravelPayPocket\Traits;
44

5-
use Illuminate\Support\Str;
5+
use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog;
6+
use InvalidArgumentException;
67

78
trait BalanceOperation
89
{
9-
protected $createdLog;
10+
protected WalletsLog $createdLog;
1011

1112
/**
1213
* Check if Balance is more than zero.
@@ -43,29 +44,37 @@ protected function createLog(string $logType, int|float $value, ?string $notes =
4344

4445
$newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value;
4546

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 */
5848
$this->createdLog = $this->logs()->create([
5949
'wallet_name' => $this->type->value,
6050
'from' => $currentBalance,
6151
'to' => $newBalance,
6252
'type' => $logType,
63-
'ip' => \Request::ip(),
53+
'ip' => request()->ip(),
6454
'value' => $value,
6555
'notes' => $notes,
66-
'reference' => $reference,
56+
'reference' => $this->generateReference(),
6757
]);
6858

6959
$this->createdLog->changeStatus('Done');
7060
}
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+
}
7180
}

0 commit comments

Comments
 (0)