Skip to content

Commit 5c0a2fd

Browse files
authored
Merge pull request #35 from lloricode/enhance/implementation
Add implementation
2 parents c2a1949 + bf155ce commit 5c0a2fd

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

config/config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
'table' => 'vouchers',
99

10+
/*
11+
* Model to use
12+
*/
13+
'model' => BeyondCode\Vouchers\Models\Voucher::class,
14+
1015
/*
1116
* Database pivot table name for vouchers and users relation
1217
*/
@@ -48,4 +53,4 @@
4853
* The user model that belongs to vouchers.
4954
*/
5055
'user_model' => \App\User::class,
51-
];
56+
];

src/Facades/Vouchers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8-
* @see \BeyondCode\Vouchers\VouchersClass
8+
* @mixin \BeyondCode\Vouchers\Vouchers
99
*/
1010
class Vouchers extends Facade
1111
{

src/Traits/CanRedeemVouchers.php

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

33
namespace BeyondCode\Vouchers\Traits;
44

5-
use Vouchers;
5+
use BeyondCode\Vouchers\Facades\Vouchers;
66
use BeyondCode\Vouchers\Models\Voucher;
77
use BeyondCode\Vouchers\Events\VoucherRedeemed;
88
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
@@ -57,4 +57,4 @@ public function vouchers()
5757
{
5858
return $this->belongsToMany(Voucher::class)->withPivot('redeemed_at');
5959
}
60-
}
60+
}

src/Traits/HasVouchers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait HasVouchers
1414
*/
1515
public function vouchers()
1616
{
17-
return $this->morphMany(Voucher::class, 'model');
17+
return $this->morphMany(config('vouchers.model', Voucher::class), 'model');
1818
}
1919

2020
/**

src/Vouchers.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ class Vouchers
1111
{
1212
/** @var VoucherGenerator */
1313
private $generator;
14+
/** @var \BeyondCode\Vouchers\Models\Voucher */
15+
private $voucherModel;
1416

1517
public function __construct(VoucherGenerator $generator)
1618
{
1719
$this->generator = $generator;
20+
$this->voucherModel = app(config('vouchers.model', Voucher::class));
1821
}
1922

2023
/**
@@ -47,7 +50,7 @@ public function create(Model $model, int $amount = 1, array $data = [], $expires
4750
$vouchers = [];
4851

4952
foreach ($this->generate($amount) as $voucherCode) {
50-
$vouchers[] = Voucher::create([
53+
$vouchers[] = $this->voucherModel->create([
5154
'model_id' => $model->getKey(),
5255
'model_type' => $model->getMorphClass(),
5356
'code' => $voucherCode,
@@ -67,7 +70,7 @@ public function create(Model $model, int $amount = 1, array $data = [], $expires
6770
*/
6871
public function check(string $code)
6972
{
70-
$voucher = Voucher::whereCode($code)->first();
73+
$voucher = $this->voucherModel->whereCode($code)->first();
7174

7275
if (is_null($voucher)) {
7376
throw VoucherIsInvalid::withCode($code);
@@ -86,7 +89,7 @@ protected function getUniqueVoucher(): string
8689
{
8790
$voucher = $this->generator->generateUnique();
8891

89-
while (Voucher::whereCode($voucher)->count() > 0) {
92+
while ($this->voucherModel->whereCode($voucher)->count() > 0) {
9093
$voucher = $this->generator->generateUnique();
9194
}
9295

0 commit comments

Comments
 (0)