Skip to content

Commit

Permalink
Merge pull request #35 from lloricode/enhance/implementation
Browse files Browse the repository at this point in the history
Add implementation
  • Loading branch information
mpociot authored Apr 12, 2021
2 parents c2a1949 + bf155ce commit 5c0a2fd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
7 changes: 6 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
'table' => 'vouchers',

/*
* Model to use
*/
'model' => BeyondCode\Vouchers\Models\Voucher::class,

/*
* Database pivot table name for vouchers and users relation
*/
Expand Down Expand Up @@ -48,4 +53,4 @@
* The user model that belongs to vouchers.
*/
'user_model' => \App\User::class,
];
];
2 changes: 1 addition & 1 deletion src/Facades/Vouchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @see \BeyondCode\Vouchers\VouchersClass
* @mixin \BeyondCode\Vouchers\Vouchers
*/
class Vouchers extends Facade
{
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/CanRedeemVouchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BeyondCode\Vouchers\Traits;

use Vouchers;
use BeyondCode\Vouchers\Facades\Vouchers;
use BeyondCode\Vouchers\Models\Voucher;
use BeyondCode\Vouchers\Events\VoucherRedeemed;
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
Expand Down Expand Up @@ -57,4 +57,4 @@ public function vouchers()
{
return $this->belongsToMany(Voucher::class)->withPivot('redeemed_at');
}
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasVouchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait HasVouchers
*/
public function vouchers()
{
return $this->morphMany(Voucher::class, 'model');
return $this->morphMany(config('vouchers.model', Voucher::class), 'model');
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/Vouchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class Vouchers
{
/** @var VoucherGenerator */
private $generator;
/** @var \BeyondCode\Vouchers\Models\Voucher */
private $voucherModel;

public function __construct(VoucherGenerator $generator)
{
$this->generator = $generator;
$this->voucherModel = app(config('vouchers.model', Voucher::class));
}

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

foreach ($this->generate($amount) as $voucherCode) {
$vouchers[] = Voucher::create([
$vouchers[] = $this->voucherModel->create([
'model_id' => $model->getKey(),
'model_type' => $model->getMorphClass(),
'code' => $voucherCode,
Expand All @@ -67,7 +70,7 @@ public function create(Model $model, int $amount = 1, array $data = [], $expires
*/
public function check(string $code)
{
$voucher = Voucher::whereCode($code)->first();
$voucher = $this->voucherModel->whereCode($code)->first();

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

while (Voucher::whereCode($voucher)->count() > 0) {
while ($this->voucherModel->whereCode($voucher)->count() > 0) {
$voucher = $this->generator->generateUnique();
}

Expand Down

0 comments on commit 5c0a2fd

Please sign in to comment.